android:Palette使用简例

版权声明:本文为博主原创文章,转载请注明地址。 https://blog.csdn.net/huangxiaoguo1/article/details/83029233
  • 效果

在这里插入图片描述

在这里插入图片描述

  • 引入palette
   compile 'com.android.support:palette-v7:26.0.0-alpha1'
  • 详细代码(代码中有说明)
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.widget.ImageView;
import android.widget.TextView;

import tsou.cn.lib_hxgioc.HxgBind;
import tsou.cn.lib_hxgioc.HxgContentView;
import tsou.cn.lib_hxgioc.HxgViewUtils;

@HxgContentView(R.layout.activity_main2)
public class Main2Activity extends AppCompatActivity {

    @HxgBind(R.id.imageview)
    private ImageView mImageview;
    /**
     * StudyRecycler
     **/
    @HxgBind(R.id.textview)
    private TextView mTextview;
    /**
     * StudyRecycler
     **/
    @HxgBind(R.id.textview1)
    private TextView mTextview1;
    /**
     * StudyRecycler
     **/
    @HxgBind(R.id.textview2)
    private TextView mTextview2;
    /**
     * StudyRecycler
     **/
    @HxgBind(R.id.textview3)
    private TextView mTextview3;
    /**
     * StudyRecycler
     **/
    @HxgBind(R.id.textview4)
    private TextView mTextview4;
    /**
     * StudyRecycler
     **/
    @HxgBind(R.id.textview5)
    private TextView mTextview5;
    /**
     * StudyRecycler
     **/
    @HxgBind(R.id.textview6)
    private TextView mTextview6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        HxgViewUtils.getView().inject(this);
        BitmapDrawable drawable = (BitmapDrawable) mImageview.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        //得到bitmap里面的一些色彩信息——通过Palette类分析出来的
//        Palette palette = Palette.generate(bitmap);
        Palette.Builder builder = Palette.from(bitmap);
        builder.generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                /**
                 * Color.BLUE代表默认色值(分析不出来的时候的色值)
                 */
                //暗、柔和的颜色
                int darkMutedColor = palette.getDarkMutedColor(Color.BLUE);
                //亮、柔和
                int lightMutedColor = palette.getLightMutedColor(Color.BLUE);
                //暗、鲜艳
                int darkVibrantColor = palette.getDarkVibrantColor(Color.BLUE);
                //亮、鲜艳
                int lightVibrantColor = palette.getLightVibrantColor(Color.BLUE);
                //柔和
                int mutedColor = palette.getMutedColor(Color.BLUE);
                //鲜艳
                int vibrantColor = palette.getVibrantColor(Color.BLUE);
                /**
                 * 获取某种特性的颜色样品
                 */
                Palette.Swatch lightVibrantSwatch = palette.getVibrantSwatch();
                //google推荐的:图片的整体的颜色rgb的混合值---主色调
                int rgb = lightVibrantSwatch.getRgb();
                //google推荐的:图片中间的文字颜色
                int bodyTextColor = lightVibrantSwatch.getBodyTextColor();
                //google推荐的:作为标题的颜色(有一定的和图片的对比度颜色值)
                int titleTextColor = lightVibrantSwatch.getTitleTextColor();
                //颜色向里
                float[] hsl = lightVibrantSwatch.getHsl();
                //分析该颜色在图片中所占的像素多少值
                int population = lightVibrantSwatch.getPopulation();

                mTextview.setBackgroundColor(getTtanslucentColor(0.6f, rgb));
                mTextview.setText("标题");
                mTextview.setTextColor(titleTextColor);

                mTextview1.setBackgroundColor(darkMutedColor);
                mTextview1.setText("暗、柔和的颜色==>darkMutedColor");

                mTextview2.setBackgroundColor(lightMutedColor);
                mTextview2.setText("亮、柔和的颜色==>lightMutedColor");

                mTextview3.setBackgroundColor(darkVibrantColor);
                mTextview3.setText("暗、鲜艳的颜色==>darkVibrantColor");

                mTextview4.setBackgroundColor(lightVibrantColor);
                mTextview4.setText("亮、鲜艳的颜色==>lightVibrantColor");

                mTextview5.setBackgroundColor(mutedColor);
                mTextview5.setText("柔和的颜色==>mutedColor");

                mTextview6.setBackgroundColor(vibrantColor);
                mTextview6.setText("鲜艳的颜色==>vibrantColor");
            }
        });

    }

    /**
     * 设置背景半透明
     *
     * @param f
     * @param rgb
     * @return
     */
    private int getTtanslucentColor(float f, int rgb) {
        int blue = rgb & 0xff;
        int green = rgb >> 8 & 0xff;
        int red = rgb >> 16 & 0xff;
        int alpha = rgb >>> 24;
        alpha = Math.round(alpha * f);
        return Color.argb(alpha, red, green, blue);
    }
}

猜你喜欢

转载自blog.csdn.net/huangxiaoguo1/article/details/83029233