Android高级ui绘制之Palette

MaterialDesign_Palette

Palette:可以在一张图片里面分析出一些色彩特性:主色调、鲜艳的颜色、柔和颜色等等……
Activity:


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.support.v7.graphics.Palette.PaletteAsyncListener;
import android.support.v7.graphics.Palette.Swatch;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

	private ImageView iv;
	private TextView tv_title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv = (ImageView)findViewById(R.id.iv);
		
		tv_title = (TextView)findViewById(R.id.tv_title);
		final TextView tv1 = (TextView)findViewById(R.id.tv1);
		final TextView tv2 = (TextView)findViewById(R.id.tv2);
		final TextView tv3 = (TextView)findViewById(R.id.tv3);
		final TextView tv4 = (TextView)findViewById(R.id.tv4);
		final TextView tv5 = (TextView)findViewById(R.id.tv5);
		final TextView tv6 = (TextView)findViewById(R.id.tv6);
		
		BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
		Bitmap bitmap = drawable.getBitmap();
		//得到bitmap里面的的一些色彩信息---通过Palette类分析出来的
//		Palette palette = Palette.generate(bitmap);
		//异步任务---可能分析的图片会比较大或者颜色分布比较复杂,会耗时比较久,防止卡死主线程。
		Palette.from(bitmap).generate(new PaletteAsyncListener() {
			
			@Override
			public void onGenerated(Palette palette) {
				//暗、柔和的颜色
				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);
				//获取某种特性颜色的样品
//				Swatch lightVibrantSwatch = palette.getLightVibrantSwatch();
				Swatch lightVibrantSwatch = palette.getVibrantSwatch();
				//谷歌推荐的:图片的整体的颜色rgb的混合值---主色调
				int rgb = lightVibrantSwatch.getRgb();
				//谷歌推荐:图片中间的文字颜色
				int bodyTextColor = lightVibrantSwatch.getBodyTextColor();
				//谷歌推荐:作为标题的颜色(有一定的和图片的对比度的颜色值)
				int titleTextColor = lightVibrantSwatch.getTitleTextColor();
				//颜色向量
				float[] hsl = lightVibrantSwatch.getHsl();
				//分析该颜色在图片中所占的像素多少值
				int population = lightVibrantSwatch.getPopulation();
				
				
				
				tv_title.setBackgroundColor(getTranslucentColor(0.6f,rgb));
				tv_title.setTextColor(titleTextColor);
				
				tv1.setBackgroundColor(darkMutedColor);
				tv1.setText("darkMutedColor");
				tv2.setBackgroundColor(lightMutedColor);
				tv2.setText("lightMutedColor");
				tv3.setBackgroundColor(darkVibrantColor);
				tv3.setText("darkVibrantColor");
				tv4.setBackgroundColor(lightVibrantColor);
				tv4.setText("lightVibrantColor");
				tv5.setBackgroundColor(mutedColor);
				tv5.setText("mutedColor");
				tv6.setBackgroundColor(vibrantColor);
				tv6.setText("vibrantColor");
				
			}
		});
	}
	protected int getTranslucentColor(float percent, int rgb) {
		// 10101011110001111
		int blue = Color.blue(rgb);
		int green = Color.green(rgb);
		int red = Color.red(rgb);
		int alpha = Color.alpha(rgb);
//		int blue = rgb & 0xff;
//		int green = rgb>>8 & 0xff;
//		int red = rgb>>16 & 0xff;
//		int alpha = rgb>>>24;
		
		alpha = Math.round(alpha*percent);
		Toast.makeText(this, "alpha:"+alpha+",red:"+red+",green:"+green, 1).show();
		return Color.argb(alpha, red, green, blue);
	}
}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ricky.materialdesign.palette.MainActivity" >

    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="230dp">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/hehe" />

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:background="#5fff"
            android:gravity="center"
            android:text="倩倩靓照" />
    </RelativeLayout>
    
        <TextView
            android:id="@+id/tv1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="text" />
        <TextView
            android:id="@+id/tv2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="text" />
        <TextView
            android:id="@+id/tv3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="text" />
        <TextView
            android:id="@+id/tv4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="text" />
        <TextView
            android:id="@+id/tv5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="text" />
        <TextView
            android:id="@+id/tv6"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="text" />

</LinearLayout>

运行截图:
在这里插入图片描述

发布了61 篇原创文章 · 获赞 0 · 访问量 896

猜你喜欢

转载自blog.csdn.net/qq_36828822/article/details/103481724