Android--利用Palette实现根据图标自动设置背景颜色的组件(银行卡背景)

先放效果图:
银行卡背景.jpg
首先看下布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical"
    tools:context=".CardActivity">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">

        <com.aruba.paletteapplication.MyCardLinearLayout
            android:id="@+id/ll_card"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/iv_card"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:src="@drawable/icon_seven" />

        </com.aruba.paletteapplication.MyCardLinearLayout>


    </android.support.v7.widget.CardView>


</LinearLayout>
这边使用了CardView实现卡片效果,然后自定义组件继承至LinearLayout
public class MyCardLinearLayout extends LinearLayout {
    private Bitmap bitmap;


    public MyCardLinearLayout(Context context) {
        this(context, null);
    }

    public MyCardLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyCardLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void attachImage(ImageView imageView) {
        if (imageView.getDrawable() instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
            bitmap = bitmapDrawable.getBitmap();
        }

        generateShader();
    }

    private void generateShader() {
        if (bitmap == null) {
            return;
        }

        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(@Nullable Palette palette) {
                //柔和而暗的颜色
                Palette.Swatch swatch = palette.getDarkMutedSwatch();
                if (swatch == null) {
                    for (Palette.Swatch swatchTemp : palette.getSwatches()) {
                        swatch = swatchTemp;
                        break;
                    }
                }
                //渐变颜色,由深色变浅色
                int colors[] = new int[]{swatch.getRgb(), blurColor(swatch.getRgb())};
                setBackground(new ShaderDrawable(colors));
                invalidate();
            }
        });
    }

    /**
     * 将颜色变浅
     *
     * @param rgb
     * @return
     */
    private int blurColor(int rgb) {
        //三原色,每个原色站8个bit
        int red = rgb >> 16 & 0xff;
        int green = rgb >> 8 & 0xff;
        int bule = rgb & 0xff;

        //#000000为黑色,#FFFFFF为白色,所以值越小,颜色越深,反之,颜色越浅
        float ratdio = 1.5f;
        red = (int) Math.min(255, red * ratdio);
        green = (int) Math.min(255, green * ratdio);
        bule = (int) Math.min(255, bule * ratdio);

        return Color.argb(255, red, green, bule);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }


    class ShaderDrawable extends Drawable {
        private int colors[];
        private Paint mPaint = new Paint();

        public ShaderDrawable(int[] colors) {
            this.colors = colors;
            mPaint.setAntiAlias(true);
        }

        @Override
        public void draw(@NonNull Canvas canvas) {
            //画背景
            if (colors != null) {
                RectF rectF = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());

                LinearGradient linearGradient = new LinearGradient(rectF.left, 0, rectF.right, 0, colors, null, Shader.TileMode.CLAMP);
                mPaint.setShader(linearGradient);

                canvas.drawRect(rectF, mPaint);
            }
        }

        @Override
        public void setAlpha(int alpha) {

        }

        @Override
        public void setColorFilter(@Nullable ColorFilter colorFilter) {

        }

        /**
         * ~OPAQUE:便是完全不透明,遮盖在他下面的所有内容
         * ~TRANSPARENT:透明,完全不显示任何东西
         * ~TRANSLUCENT:只有绘制的地方才覆盖底下的内容。
         *
         * @return
         */
        @Override
        public int getOpacity() {
            return PixelFormat.OPAQUE;
        }
    }
}
最后在Activity中调用attachImage方法
public class CardActivity extends AppCompatActivity {
    private ImageView ivCard;
    private MyCardLinearLayout llCard;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card);

        llCard = findViewById(R.id.ll_card);
        ivCard = findViewById(R.id.iv_card);

        llCard.attachImage(ivCard);
    }
}
项目地址:https://gitee.com/aruba/PaletteApplication.git
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值