• android的Shader


    public class

    Shader

    extends Object
    java.lang.Object
       ↳ android.graphics.Shader
    Known Direct Subclasses

    通过为Paint设置shader实现效果,paint.setShaer(Shader shader);

    例子:

    1.LinearGradient 线性渐变shader

    效果,github有shimmer的开源库,https://github.com/RomainPiel/Shimmer-android

    代码:自己简单实现的一个闪亮效果的button

    package com.example.myapp.view;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.LinearGradient;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.Shader;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.widget.Button;
    
    /**
     * Created by zyr
     * DATE: 16-3-2
     * Time: 上午10:39
     * Email: yanru.zhang@renren-inc.com
     */
    public class MyShimmerButton extends Button {
        private Paint mPaint;
        private int mWdith,mHeight,mTranslateX,deltaX;
        private LinearGradient mLinearGradient;
        private Matrix mMatrix;
        private boolean isShimmer = false;
    
    
        public MyShimmerButton(Context context) {
            super(context);
        }
    
        public MyShimmerButton(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyShimmerButton(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
            Log.d("zyr","onFinishInflate");
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            Log.d("zyr", "onMeasure");
    
        }
    
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
            Log.d("zyr", "onLayout");
    
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            Log.d("zyr", "onSizeChanged");
    
            if(w!=oldw || h!=oldh){
                isShimmer = true;
                mWdith = w;
                mHeight = h;
                mPaint = getPaint();
                mLinearGradient = new LinearGradient(0,0,mWdith,mHeight,new int[]{0x33ffffff, 0xffffffff, 0x33ffffff} ,  new float[] { 0, 0.5f, 1 }, Shader.TileMode.CLAMP);
                mMatrix = new Matrix();
                mLinearGradient.setLocalMatrix(mMatrix);
                deltaX = mWdith / 10;
                mPaint.setShader(mLinearGradient);
            }
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if(isShimmer){
                if(mTranslateX > mWdith*2){
                    mTranslateX = -mWdith;
                } else{
                    mTranslateX = mTranslateX + deltaX;
                }
    
                mMatrix.setTranslate(mTranslateX,0);
                mLinearGradient.setLocalMatrix(mMatrix);
                postInvalidateDelayed(50);
            }
        }
    }
    public class

    Shader

    extends Object
    java.lang.Object
       ↳ android.graphics.Shader
    Known Direct Subclasses
  • 相关阅读:
    Java读取压缩文件信息
    中国菜刀 一句话木马
    java中equals方法和hashcode方法的区别和联系,以及为什么要重写这两个方法,不重写会怎样
    Hadoop搭建全程
    instr模糊查询使用及注意事项
    idea离线安装SonarLint
    Previous operation has not finished; run 'cleanup' if it was interrupted] 的排错过程
    vue中excel文件上传总结
    IDEA 2019.1离线安装lombok
    @Data注解失效解决办法
  • 原文地址:https://www.cnblogs.com/bbglz/p/5234129.html
Copyright © 2020-2023  润新知