zoukankan      html  css  js  c++  java
  • 自定义控件 闪烁效果的TextView


    使用


    <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);
        }
    }





  • 相关阅读:
    hello world之vivado程序解决方法
    FPGA的电源选择重要性分析
    RabbitMQ的简单使用
    RabbitMQ的相关概念
    Spring整合Quartz
    DisallowConcurrentExecution注解
    Quartz框架中的监听器
    JobStore使用
    quartz基本介绍
    java自定义注解
  • 原文地址:https://www.cnblogs.com/baiqiantao/p/5468958.html
Copyright © 2011-2022 走看看