android:Palette使用簡例
阿新 • • 發佈:2018-12-06
- 效果
- 引入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); } }