android 文字的动画效果怎么做,Android自定义TextView实现动态的文字效果BlingBling

效果如图:

0818b9ca8b590ca3270a3433284dd417.png

实现方法:

package com.katherine.du.flashingtextviewdemo.view;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.LinearGradient;

import android.graphics.Matrix;

import android.graphics.Shader;

import android.text.TextPaint;

import android.util.AttributeSet;

import android.widget.TextView;

/**

* Created by du on 17/7/18.

*/

public class FlashingTextView extends TextView {

private int mViewWidth;

private TextPaint mPaint;

private LinearGradient mLinearGradient;

private Matrix mGradientMatrix;

private int mTranslate;

public FlashingTextView(Context context) {

super(context);

}

public FlashingTextView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public FlashingTextView(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();//获取当前用来绘制TextView的Paint对象

mLinearGradient = new LinearGradient(0, 0, mViewWidth, 0,

new int[]{Color.GREEN, Color.BLUE, Color.YELLOW, Color.GREEN},

null,

Shader.TileMode.CLAMP);//根据view的宽设置一个LinearGradient渐变渲染器

mPaint.setShader(mLinearGradient);//给Paint对象设置渲染器

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(200);

}

}

}

使用方法:

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:gravity="center"

android:text="Hello World !"

android:textSize="40sp" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值