亲宝软件园·资讯

展开

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

ScalpelD 人气:0
想了解Android自定义View实现闪耀字体效果的相关内容吗,ScalpelD在本文为您仔细讲解Android闪耀字体效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,view,闪耀字体,下面大家一起来学习吧。

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

效果图如下

加载全部内容

相关教程
猜你喜欢
用户评论