自己写的的一个android弹幕代码:
// 弹幕系统 str为弹幕的内容
public void Createdanmu(Context context, String str) {
final TextView tv = new TextView(context);
tv.setText(str);
//随机颜色
int r, g, b;
Random random2 = new Random();
r = random2.nextInt(255);
g = random2.nextInt(255);
b = random2.nextInt(255);
tv.setTextColor(Color.rgb(r, g, b));
int wight = ScreenUtils.getWight(context);
int height = ScreenUtils.getHeight(context);
int textsize = adjustFontSize(wight,height);
//字体大小由屏幕大小决定
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,textsize);
//弹幕重左到又的动画
TranslateAnimation animation = new TranslateAnimation(0, danmucontainer.getMeasuredWidth(), ranheight,
ranheight);
animation.setAnimationListener(null);
animation.setDuration(4000);// 设置动画持续时间
animation.setRepeatCount(0);// 设置重复次数
animation.setRepeatMode(Animation.REVERSE);// 设置反方向执行
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
removetv(tv);//动画结束后去掉弹幕
}
});
tv.setAnimation(animation);
//danmucontainer为装载弹幕的布局容器控件
danmucontainer.addView(tv);
animation.startNow();
}
public void removetv(TextView tv) {
tv.setVisibility(View.GONE);
}
//適應屏幕大小設置字體用
public static int adjustFontSize(int screenWidth, int screenHeight){
if (screenWidth <= 240) { // 240X320 屏幕
return 10;
}else if (screenWidth <= 320){ // 320X480 屏幕
return 14;
}else if (screenWidth <= 480){ // 480X800 或 480X854 屏幕
return 24;
}else if (screenWidth <= 540){ // 540X960 屏幕
return 26;
}else if(screenWidth <= 800){ // 800X1280 屏幕
return 30;
}else{ // 大于 800X1280
return 40;
}
}
以上内柔就是我个人弄得一个简易的弹幕系统