首页 > 系统 > Android > 正文

Android自定义View实现闪耀字体效果

2019-12-12 04:04:41
字体:
来源:转载
供稿:网友

本文实例为大家分享了闪耀字体效果的具体代码,供大家参考,具体内容如下

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" />

效果图如下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表