很棒的Android彈幕效果實例
很多項目需要用到彈幕效果,尤其是在播放視頻的時候需要一起顯示別人發(fā)的彈幕,也包括自己的發(fā)的。
今天就試著寫了一下這個效果。
思路就是將從右往左的動畫效果,字體內容,字體大小,彈幕平移速度等屬性一起與TextView封裝成BarrageItem,并將控制效果與BarrageItem綁定在BarrageView進行顯示。思路還是比較簡單的。這里沒有考慮到帶有表情的彈幕,我會持續(xù)更新的。
先看效果:

項目目錄結構:

接下來定義Barrageitem.class : 這個類就將TextView與從右往左的動畫效果,字體內容,字體大小,彈幕平移速度等屬性綁定。
public class BarrageItem {
public TextView textView;
public int textColor;
public String text;
public int textSize;
// 移動速度
public int moveSpeed;
// 垂直方向顯示的位置
public int verticalPos;
// 字體顯示占據(jù)的寬度
public int textMeasuredWidth;
}
然后定義BarrageView,由于彈幕的字體顏色大小和移動速度都是隨機的,需要定義最大最小值來限定它們的范圍,然后通過產(chǎn)生隨機數(shù)來設置它們在這個范圍內的值。另外還需要定義彈幕的文本內容,這里是直接寫死的一些固定值。
BarrageView.class:
public class BarrageView extends RelativeLayout {
private Context mContext;
private BarrageHandler mHandler = new BarrageHandler();
private Random random = new Random(System.currentTimeMillis());
// 兩個彈幕的最小間隔時間
private static final long BARRAGE_GAP_MIN_DURATION = 1000;
// 兩個彈幕的最大間隔時間
private static final long BARRAGE_GAP_MAX_DURATION = 2000;
// 速度,ms
private int maxSpeed = 12000;
// 速度,ms
private int minSpeed = 8000;
// 文字最大值
private int maxSize = 50;
// 文字最小值
private int minSize = 10;
private int totalHeight = 0;
private int lineHeight = 0;// 每一行彈幕的高度
private int totalLine = 0;// 彈幕的行數(shù)
private String[] itemText = { "他們都說蔡睿智很帥,但他總覺得自己很丑",
"他們都說蔡睿智是男神,但他只覺得自己是男生", "蔡睿智不是男神,蔡睿智是男生", "蔡睿智貌似是gay", "蔡睿智是彎的",
"蔡睿智是彎的,還好現(xiàn)在掰回來了", "他承受了他這個年紀不該有的機智與帥氣,他好累",
"我恨自己的顏值,我覺得自己的才華才是吸引別人的地方", "他為什么對妹子不感興趣呢?為什么?", "他為什么不想談戀愛","他不會去愛別人,同時也不希望別人去愛他,他已經(jīng)習慣一個人了",
"他的心里是否住著一個蒼老的小孩", "他的世界一直就是他和他的影子,直到遇到她", "她引導他走出了自己的世界,改變他的很多看法",
"他漸漸的發(fā)現(xiàn)自己已經(jīng)離不開他,他選擇不再去壓抑自己", "因為他已經(jīng)不是那個無能為力的年紀","她經(jīng)常說他 高冷,現(xiàn)在越來越覺得他恨悶騷","開始他一直與她保持朋友距離,但他發(fā)現(xiàn)自己根本作不到"};
private int textCount;
public BarrageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
_init();
}
public BarrageView(Context context, AttributeSet attrs) {
this(context, null, 0);
}
public BarrageView(Context context) {
this(context, null);
}
private void _init() {
textCount = itemText.length;
int duration = (int) ((BARRAGE_GAP_MAX_DURATION - BARRAGE_GAP_MIN_DURATION) * Math
.random());
mHandler.sendEmptyMessageDelayed(0, duration);
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
totalHeight = getMeasuredHeight();
lineHeight = getLineHeight();
totalLine = totalHeight / lineHeight;
}
private void generateItem() {
BarrageItem item = new BarrageItem();
String tx = itemText[(int) (Math.random() * textCount)];
int sz = (int) (minSize + (maxSize - minSize) * Math.random());
item.textView = new TextView(mContext);
item.textView.setText(tx);
item.textView.setTextSize(sz);
item.textView.setTextColor(Color.rgb(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
item.textMeasuredWidth = (int) getTextWidth(item, tx, sz);
item.moveSpeed = (int) (minSpeed + (maxSpeed - minSpeed)
* Math.random());
if (totalLine == 0) {
totalHeight = getMeasuredHeight();
lineHeight = getLineHeight();
totalLine = totalHeight / lineHeight;
}
item.verticalPos = random.nextInt(totalLine) * lineHeight;
showBarrageItem(item);
}
private void showBarrageItem(final BarrageItem item) {
int leftMargin = this.getRight() - this.getLeft()
- this.getPaddingLeft();
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.topMargin = item.verticalPos;
this.addView(item.textView, params);
Animation anim = generateTranslateAnim(item, leftMargin);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
item.textView.clearAnimation();
BarrageView.this.removeView(item.textView);
}
});
item.textView.startAnimation(anim);
}
private TranslateAnimation generateTranslateAnim(BarrageItem item,
int leftMargin) {
TranslateAnimation anim = new TranslateAnimation(leftMargin,
-item.textMeasuredWidth, 0, 0);
anim.setDuration(item.moveSpeed);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.setFillAfter(true);
return anim;
}
/**
* 計算TextView中字符串的長度
*
* @param item
* @param text
* 要計算的字符串
* @param Size
* 字體大小
* @return TextView中字符串的長度
*/
public float getTextWidth(BarrageItem item, String text, float Size) {
Rect bounds = new Rect();
TextPaint paint;
paint = item.textView.getPaint();
paint.getTextBounds(text, 0, text.length(), bounds);
return bounds.width();
}
/**
* 獲得每一行彈幕的最大高度
*/
private int getLineHeight() {
BarrageItem item = new BarrageItem();
String tx = itemText[0];
item.textView = new TextView(mContext);
item.textView.setText(tx);
item.textView.setTextSize(maxSize);
Rect bounds = new Rect();
TextPaint paint;
paint = item.textView.getPaint();
paint.getTextBounds(tx, 0, tx.length(), bounds);
return bounds.height();
}
class BarrageHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
generateItem();
// 每個彈幕產(chǎn)生的時間隨機
int duration = (int) ((BARRAGE_GAP_MAX_DURATION - BARRAGE_GAP_MIN_DURATION) * Math
.random());
this.sendEmptyMessageDelayed(0, duration);
}
}
}
如果彈幕顯示的垂直位置是隨機的,就會出現(xiàn)垂直方向上彈幕重疊的情況,所以需要根據(jù)高度對垂直方向按照彈幕高度的最大值等分,然后讓彈幕在這些指定的垂直位置隨機分布。這個值在onWindowFocusChanged里計算,因為在這個方法中通過View的getMeasuredHeight()得到的高度不為空。
MainActivity.class:
<span style="font-size:18px;">public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}</span>
以上就是彈幕的源碼,其實我覺得這里少了自己發(fā)送彈幕的功能,我會在以后的更新上去的,共勉。
相關文章
Android編程實現(xiàn)自定義Dialog的大小自動控制方法示例
這篇文章主要介紹了Android編程實現(xiàn)自定義Dialog的大小自動控制方法,結合實例形式分析了Android自定義Dialog對話框的屬性操作技巧與大小動態(tài)控制實現(xiàn)方法,需要的朋友可以參考下2017-09-09
Android實現(xiàn)在子線程中更新Activity中UI的方法
這篇文章主要介紹了Android實現(xiàn)在子線程中更新Activity中UI的方法,涉及Android線程與activity操作的相關技巧,需要的朋友可以參考下2016-04-04
Android 使用Vitamio打造自己的萬能播放器(6)——在線播放(播放列表)
本文主要介紹Android Vitamino在線播放列表,這里給大家提供效果圖和實例代碼以便大家參考學習,希望能幫助開發(fā)Android視頻播放的朋友2016-07-07
Android實現(xiàn)自動變換大小的組件ViewPager2
這篇文章主要介紹了Android實現(xiàn)自動變換大小的組件ViewPager2,ViewPager2最顯著的特點是基于RecyclerView實現(xiàn),RecyclerView是目前Android端最成熟的AdapterView解決方案2023-03-03

