全部 文章 问答 分享 共找到2个相关内容
[文章] 【自定义View】自动滚动的TextView(跑马灯)——可获取完成一次滚动后的监听
AutoRollTextViewByRunnable类classAutoRollTextViewByRunnableextendsandroidx.appcompat.widget.AppCompatTextViewimplementsRunnable{StringTAG="MarqueeText";privateintcurrentScrollX=0;//当前滚动的位置privateinttextWidth;privateintviewWidth;privatebooleanisMeasure=false;privatebooleanisRoll=false;AgainListeneragainListener;//动画效果ObjectAnimatoranimator1;ObjectAnimatoranimator2;publicAutoRollTextViewByRunnable(Contextcontext){super(context);init();}publicAutoRollTextViewByRunnable(Contextcontext,AttributeSetattrs){super(context,attrs);init();}publicAutoRollTextViewByRunnable(Contextcontext,AttributeSetattrs,intdefStyle){super(context,attrs,defStyle);init();}voidinit(){setGravity(Gravity.CENTER_VERTICAL);setSingleLine();setEllipsize(TextUtils.TruncateAt.MARQUEE);startScroll();addTextChangedListener(textWatcher);//动画效果animator1=ObjectAnimator.ofFloat(this,"alpha",0f);animator2=ObjectAnimator.ofFloat(this,"alpha",1f);}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);if(!isMeasure){textWidth=getTextWidth();viewWidth=getWidth();isMeasure=true;}}TextWatchertextWatcher=newTextWatcher(){@OverridepublicvoidbeforeTextChanged(CharSequences,intstart,intcount,intafter){animator1.setDuration(0);animator2.setStartDelay(500);animator2.setDuration(500);animator2.setInterpolator(newLinearInterpolator());AnimatorSetanimatorSet=newAnimatorSet();animatorSet.playSequentially(animator1,animator2);animatorSet.start();}@OverridepublicvoidonTextChanged(CharSequences,intstart,intbefore,intcount){}@OverridepublicvoidafterTextChanged(Editables){textWidth=getTextWidth();}};@Overridepublicvoidrun(){if(textWidth>viewWidth){//需要滚动currentScrollX+=3;scrollTo(currentScrollX,0);if(getScrollX()>textWidth){againListener.Again();currentScrollX=-viewWidth;scrollTo(currentScrollX,0);}postDelayed(this,5);}else{//无需滚动scrollTo(0,0);postDelayed(this,500);}}publicvoidstartScroll(){post(this);}publicvoidstopScroll(){this.removeCallbacks(this);}/***获取文字宽度*/publicintgetTextWidth(){Paintpaint=this.getPaint();Stringstr=this.getText().toString();return(int)paint.measureText(str);}publicAgainListenergetAgainListener(){returnagainListener;}publicvoidsetAgainListener(AgainListeneragainListener){this.againListener=againListener;}interfaceAgainListener{voidAgain();}}2、使用(xml忽略)AutoRollTextViewByRunnablemarqueeText;//完成一次滚动的监听marqueeText.setAgainListener(newAutoRollTextViewByRunnable.AgainListener(){@OverridepublicvoidAgain(){Log.d(TAG,"Again:");}});
2020-07-28 15:57 · TextView / Android / 跑马灯 / 自定义View
[文章] 25000字总结Android优秀的第三方框架、各种学习资料(上)
简书花式封装使用SpannableStringGithub简单易用的TextView装饰库TextView特效Github滚动显示TextView的数字,支持自定义每个字符速度Github俗名:可垂直跑、可水平跑的跑马灯
2020-05-18 19:56 · android
  • 1