Android自定义View实现闪耀字体效果
程序员文章站
2024-02-19 17:45:52
本文实例为大家分享了闪耀字体效果的具体代码,供大家参考,具体内容如下
import android.content.context;
import androi...
本文实例为大家分享了闪耀字体效果的具体代码,供大家参考,具体内容如下
import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.lineargradient; import android.graphics.matrix; import android.graphics.paint; import android.graphics.shader; import android.util.attributeset; import android.widget.textview; /** * 闪耀字体 */ public class shiningfontview extends textview { private int mviewwidth; private paint mpaint; private lineargradient mlineargradient; private matrix mgradientmatrix; private int mtranslate; public shiningfontview(context context) { super(context); } public shiningfontview(context context, attributeset attrs) { super(context, attrs); } public shiningfontview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); if (mviewwidth == 0) { mviewwidth = getmeasuredwidth(); if (mviewwidth > 0) { mpaint = getpaint(); mlineargradient = new lineargradient(0, 0, mviewwidth, 0, new int[]{color.blue, 0xffffffff, color.blue}, null, shader.tilemode.clamp); mpaint.setshader(mlineargradient); mgradientmatrix = new matrix(); } } } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if (mgradientmatrix != null) { mtranslate += mviewwidth / 5; if (mtranslate > 2 * mviewwidth) { mtranslate = -mviewwidth; } mgradientmatrix.settranslate(mtranslate, 0); mlineargradient.setlocalmatrix(mgradientmatrix); postinvalidatedelayed(100); } } }
使用方式如下
<com.hz56pt.huipaotui.view.shiningfontview//包裹起来就行 android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_marginleft="@dimen/margin_10" android:layout_torightof="@id/iv_tips1_icon" android:text="@string/tips1" />
效果图如下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。