Android 自定义控件-------弹幕
Demo下载地址 https://download.csdn.net/download/liangyalong_1314/10432766
话不多说先看效果图
其实这一个控件,方便好用,可以用于视频的弹幕,可以自定义弹幕样式,自己学习备用View
控件内容配置就这么多 如何需要更换UI 可以修改 这个自定义控件
myBarrageView = ((MYBarrageView) findViewById(R.id.xcView)); String[] mStrItems = { "111111", "222222", "333333", "66666", "888888", "UZI牛逼", }; myBarrageView.initDanmuItemViews(mStrItems); myBarrageView.start();
获得过数据后 可以修改这个布局的UI效果,可以更换传输内容
点击事件 如果需要可以做下回调,不过我这个项目没有用到,可以自己写一个
public void createDanmuView(int index,String content,boolean reset){ View inflate = inflate(mContext, R.layout.index_barrage_item, null); LinearLayout viewById = inflate.findViewById(R.id.beijing); TextView textname=inflate.findViewById(R.id.tv); int r = mRandom.nextInt(100) % mRowNum; viewById.setBackgroundResource(mBgResIds[r]); textname.setText(content); LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); int row = mRandom.nextInt(100) % mRowNum; while(row == lastRow){ row = mRandom.nextInt(100)% mRowNum; } int pos = mRandom.nextInt(100)% mRowNum; lp.topMargin = row * mRowPos[pos]; inflate.setLayoutParams(lp); inflate.setPadding(0, 2, 80, 2); this.addView(inflate); if(reset){ mChildList.set(index,inflate); }else{ mChildList.add(index,inflate); } }
之后就是开启滚动的动画 带上代码
private Handler mHandler = new Handler() { @Override public void handleMessage(final Message msg) { super.handleMessage(msg); final int pos = msg.what; ViewPropertyAnimator animator; if(mDirection == XCDirection.FROM_RIGHT_TO_LEFT){ animator = mChildList.get(msg.what).animate() .translationXBy(-(mScreenWidth + mChildList.get(msg.what).getWidth())); }else{ animator = mChildList.get(msg.what).animate() .translationXBy(mScreenWidth + mChildList.get(msg.what).getWidth()); } Random random = new Random(System.currentTimeMillis()); int index = random.nextInt(100) % mSpeeds.length; animator.setDuration(mSpeeds[index]); animator.setInterpolator(new LinearInterpolator()); animator.setListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animator) { } @Override public void onAnimationEnd(Animator animator) { MYBarrageView.this.removeView(mChildList.get(pos)); int index = mRandom.nextInt(100) % mStrContents.length; createDanmuView(pos, mStrContents[index], true); mHandler.sendEmptyMessageDelayed(pos, mDelayDuration); Log.v("czm", "size=" + mChildList.size()); } @Override public void onAnimationCancel(Animator animator) { } @Override public void onAnimationRepeat(Animator animator) { } }); animator.start(); } };
最后贴上全部代码
- public class MYBarrageView extends RelativeLayout {
- private int mWidth;
- private int mScreenWidth;
- private List
mChildList; - private boolean mIsWorking = false;
- private Context mContext;
- private int mMaxShowNum = 5;
- private int mRowNum = 3;
- private int[] mSpeeds = {
- 6000,6000,6000,6000
- };
- private int mDelayDuration = 1000;
- private int[] mBgResIds = {
- R.drawable.aaaa,
- R.drawable.aaaa,
- R.drawable.aaaa,
- R.drawable.aaaa
- };
- private int[] mRowPos = {
- 100,200,300,400
- };
- private Random mRandom;
- private String[] mStrContents;
- public static enum XCDirection{
- FROM_RIGHT_TO_LEFT,
- FORM_LEFT_TO_RIGHT
- }
- public enum XCAction{
- SHOW,HIDE
- }
- private XCDirection mDirection = XCDirection.FROM_RIGHT_TO_LEFT;
- private Handler mHandler = new Handler() {
- @Override
- public void handleMessage(final Message msg) {
- super.handleMessage(msg);
- final int pos = msg.what;
- ViewPropertyAnimator animator;
- if(mDirection == XCDirection.FROM_RIGHT_TO_LEFT){
- animator = mChildList.get(msg.what).animate()
- .translationXBy(-(mScreenWidth + mChildList.get(msg.what).getWidth()));
- }else{
- animator = mChildList.get(msg.what).animate()
- .translationXBy(mScreenWidth + mChildList.get(msg.what).getWidth());
- }
- Random random = new Random(System.currentTimeMillis());
- int index = random.nextInt(100) % mSpeeds.length;
- animator.setDuration(mSpeeds[index]);
- animator.setInterpolator(new LinearInterpolator());
- animator.setListener(new Animator.AnimatorListener() {
- @Override
- public void onAnimationStart(Animator animator) {
- }
- @Override
- public void onAnimationEnd(Animator animator) {
- MYBarrageView.this.removeView(mChildList.get(pos));
- int index = mRandom.nextInt(100) % mStrContents.length;
- createDanmuView(pos, mStrContents[index], true);
- mHandler.sendEmptyMessageDelayed(pos, mDelayDuration);
- Log.v("czm", "size=" + mChildList.size());
- }
- @Override
- public void onAnimationCancel(Animator animator) {
- }
- @Override
- public void onAnimationRepeat(Animator animator) {
- }
- });
- animator.start();
- }
- };
- public MYBarrageView(Context context) {
- this(context, null, 0);
- }
- public MYBarrageView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
- public MYBarrageView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- mContext = context;
- init();
- }
- public boolean isWorking(){
- return mIsWorking;
- }
- private void init() {
- mScreenWidth = getScreenWidth();
- mChildList = new ArrayList<>();
- mRandom = new Random();
- }
- public void setDirection(XCDirection direction){
- mDirection = direction;
- }
- int lastRow = 0;
- public void initDanmuItemViews(String[] strContents){
- mStrContents = strContents;
- for(int i = 0; i < mMaxShowNum; i ++){
- int index = mRandom.nextInt(100) % strContents.length;
- createDanmuView(i,strContents[index],false);
- }
- }
- public void createDanmuView(int index,String content,boolean reset){
- View inflate = inflate(mContext, R.layout.index_barrage_item, null);
- LinearLayout viewById = inflate.findViewById(R.id.beijing);
- TextView textname=inflate.findViewById(R.id.tv);
- // final TextView textView = new TextView(mContext);
- int r = mRandom.nextInt(100) % mRowNum;
- viewById.setBackgroundResource(mBgResIds[r]);
- textname.setText(content);
- LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT);
- int row = mRandom.nextInt(100) % mRowNum;
- while(row == lastRow){
- row = mRandom.nextInt(100)% mRowNum;
- }
- int pos = mRandom.nextInt(100)% mRowNum;
- lp.topMargin = row * mRowPos[pos];
- // lastRow = row;
- inflate.setLayoutParams(lp);
- inflate.setPadding(0, 2, 80, 2);
- this.addView(inflate);
- if(reset){
- mChildList.set(index,inflate);
- }else{
- mChildList.add(index,inflate);
- }
- }
- boolean isFirst = true;
- public void start(){
- switchAnimation(XCAction.SHOW);
- if(isFirst){
- for(int i =0;i< mChildList.size();i++){
- mHandler.sendEmptyMessageDelayed(i,i * mDelayDuration);
- }
- isFirst = false;
- }
- mIsWorking = true;
- }
- public void hide(){
- switchAnimation(XCAction.HIDE);
- mIsWorking =false;
- }
- public void stop(){
- this.setVisibility(View.GONE);
- for(int i =0;i< mChildList.size();i++){
- mChildList.get(i).clearAnimation();
- mHandler.removeMessages(i);
- }
- mIsWorking =false;
- }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- super.onLayout(changed, l, t, r, b);
- int childCount = this.getChildCount();
- for(int i=0;i
- View view = getChildAt(i);
- LayoutParams lp = (LayoutParams) view.getLayoutParams();
- if(lp.leftMargin <= 0){
- if(mDirection == XCDirection.FORM_LEFT_TO_RIGHT){
- view.layout(-view.getMeasuredWidth(), lp.topMargin,
- 0,lp.topMargin + view.getMeasuredHeight());
- }else{
- view.layout(mScreenWidth,lp.topMargin,mScreenWidth+view.getMeasuredWidth(),
- lp.topMargin+view.getMeasuredHeight());
- }
- }else{
- continue;
- }
- }
- }
- private void switchAnimation(final XCAction action){
- AlphaAnimation animation;
- if(action == XCAction.HIDE){
- animation = new AlphaAnimation(1.0f,0.0f);
- animation.setDuration(400);
- }else{
- animation = new AlphaAnimation(0.0f,1.0f);
- animation.setDuration(1000);
- }
- MYBarrageView.this.startAnimation(animation);
- animation.setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- if(action == XCAction.HIDE){
- MYBarrageView.this.setVisibility(View.GONE);
- }else{
- MYBarrageView.this.setVisibility(View.VISIBLE);
- }
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- }
- });
- }
- private int getScreenWidth(){
- WindowManager mWm = (WindowManager) this.getContext()
- .getSystemService(Context.WINDOW_SERVICE);
- DisplayMetrics dm = new DisplayMetrics();
- // 获取屏幕信息
- mWm.getDefaultDisplay().getMetrics(dm);
- return dm.widthPixels;
- }
- }
Demo下载地址 https://download.csdn.net/download/liangyalong_1314/10432766