天天看點

Android--利用Palette實作根據圖示自動設定背景顔色的元件(銀行卡背景)

先放效果圖:

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