博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义控件 闪烁效果的TextView
阅读量:4356 次
发布时间:2019-06-07

本文共 2164 字,大约阅读时间需要 7 分钟。

使用
 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="普通的TextView"
        android:textColor="#00f"
        android:textSize="30sp" />
    <com.bqt.myview.MyShimmerTextView
        android:id="@+id/shimmer_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#000"
        android:gravity="center"
        android:text="闪烁效果的TextView"
        android:textColor="#00f"
        android:textSize="30sp" />
</LinearLayout>
代码
 
 
public class MyShimmerTextView extends TextView {
    /**渲染器,用于显示本例中的颜色效果*/
    private LinearGradient mLinearGradient;
    /**矩阵,用于确定渲染范围*/
    private Matrix mGradientMatrix;
    /**渲染的起始位置*/
    private int mViewWidth = 0;
    /**渲染的终止距离*/
    private int mTranslate = 0;
    /**是否启动动画*/
    private boolean mAnimating = true;
    /**多少毫秒刷新一次*/
    private int speed = 50;
    private Paint mPaint;
    public MyShimmerTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = getPaint();
        mGradientMatrix = new Matrix();
    }
    @SuppressLint("DrawAllocation")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mViewWidth = getMeasuredWidth();
        //可以尝试一下,使用不同的模式可以得到不同的效果
        mLinearGradient = new LinearGradient(0, 0, mViewWidth, 0, new int[] { Color.BLACK, Color.WHITE, Color.BLACK }, null, TileMode.CLAMP);
        mPaint.setShader(mLinearGradient);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mAnimating && mGradientMatrix != null) {
            mTranslate += mViewWidth / 10;//每次移动屏幕的1/10宽
            if (mTranslate > 2 * mViewWidth) {
                mTranslate = -mViewWidth;
            }
            mGradientMatrix.setTranslate(mTranslate, 0);
            mLinearGradient.setLocalMatrix(mGradientMatrix);//在指定矩阵上渲染
            postInvalidateDelayed(speed);
        }
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i("bqt", w + "--" + h);
    }
}

转载于:https://www.cnblogs.com/baiqiantao/p/5468958.html

你可能感兴趣的文章
001---进程
查看>>
视频人脸检测——OpenCV版(三)
查看>>
php获取来访者在搜索引擎搜索某个关键词,进入网站
查看>>
物联网架构成长之路(8)-EMQ-Hook了解、连接Kafka发送消息
查看>>
2018-2019-1 20165234 20165236 实验二 固件程序设计
查看>>
IDEA的GUI连接数据库写入SQL语句的问题总结
查看>>
Xpath在选择器中正确,在代码中返回的是空列表问题
查看>>
leecode第一百九十八题(打家劫舍)
查看>>
【BZOJ 1233】 [Usaco2009Open]干草堆tower (单调队列优化DP)
查看>>
07-3. 数素数 (20)
查看>>
写一个欢迎页node统计接口Py脚本(邮件,附件)-py
查看>>
计算两个日期之间的天数
查看>>
Android关于buildToolVersion与CompileSdkVersion的区别
查看>>
袋鼠云日志,日志分析没那么容易
查看>>
缓存穿透 缓存雪崩 缓存并发
查看>>
了解你的Linux系统:必须掌握的20个命令
查看>>
js setInterval 启用&停止
查看>>
knockoutJS学习笔记04:监控属性
查看>>
Linux下启动/关闭Oracle
查看>>
session和cookie的区别
查看>>