您的位置:首页 > 移动开发 > Android开发

Android Palette 调色板 的使用

2017-11-30 14:17 411 查看

实现以下效果 《每点击一次,切换下面的6个颜色,没有为默认色》



先定义一个HTML布局

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/iv_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/timg2"/>

<TextView
android:id="@+id/tv1"
android:layout_marginTop="10dp"
android:text="1.活力颜色"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/darker_gray"/>

<TextView
android:id="@+id/tv2"
android:text="2.亮的活力颜色"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/darker_gray"/>
<TextView
android:id="@+id/tv3"
android:text="3.暗的活力颜色"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/darker_gray"/>
<TextView
android:id="@+id/tv4"
android:text="4.柔色"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/darker_gray"/>
<TextView
android:id="@+id/tv5"
android:text="5.亮的柔色"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/darker_gray"/>
<TextView
android:id="@+id/tv6"
android:text="6.暗的柔色"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/darker_gray"/>

<TextView
android:id="@+id/tv_gradient"
android:text="渐变效果"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/darker_gray"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>


常用方法

Palette.Swatch 返回的对象不同颜色可能为空哦

public void changeTextView(int imageRes){
//资源文件 转 bitmap
InputStream is = getResources().openRawResource(imageRes);
Bitmap bitmap = BitmapFactory.decodeStream(is);
//提取bitmap的颜色
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
//1.活力颜色
Palette.Swatch a = palette.getVibrantSwatch();
showTextView(0,a);
//2.亮的活力颜色
Palette.Swatch b = palette.getLightVibrantSwatch();
showTextView(1,b);
//3.暗的活力颜色
Palette.Swatch c = palette.getDarkVibrantSwatch();
showTextView(2,c);
//4.柔色
Palette.Swatch d = palette.getMutedSwatch();
showTextView(3,d);
//5.亮的柔色
Palette.Swatch e = palette.getLightMutedSwatch();
showTextView(4,e);
//6.暗的柔色
Palette.Swatch f = palette.getDarkMutedSwatch();
showTextView(5,f);
//修改渐变
//                changedImageViewShape(startColor,endColor,a.getTitleTextColor());
//                f.getRgb(); //rgb颜色
//                f.getTitleTextColor();//文本颜色
//
//                //返回float[],可以进行修改,后使用ColorUtils工具类进行转换
//                f.getHsl();
//                f.getBodyTextColor();//和文本颜色一样
}
});
}


/**
* 显示在TextView上
* @param position       显示在TextView数组 的下标
* @param swatch
*/
private void showTextView
4000
(int position,Palette.Swatch swatch){
if (swatch != null){
//rgb颜色
int rgb = swatch.getRgb();
mTextViews[position].setBackgroundColor(rgb);
//文本颜色
int titleTextColor = swatch.getTitleTextColor();
mTextViews[position].setTextColor(titleTextColor);
}else{
mTextViews[position].setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
}


/**
* 设置 渐变
* @param colorStart
* @param colorEnd
* @param titleTextColor
* @return
*/
private void changedImageViewShape(int colorStart, int colorEnd,int titleTextColor){
GradientDrawable shape = new GradientDrawable(GradientDrawable.Orientation.TL_BR,new int[]{colorStart,colorEnd});
shape.setShape(GradientDrawable.RECTANGLE);
//设置渐变方式
shape.setGradientType(GradientDrawable.LINEAR_GRADIENT);
//圆角
shape.setCornerRadius(8);
//设置颜色
mTvGradinet.setBackgroundDrawable(shape);
mTvGradinet.setTextColor(titleTextColor);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android Palette 调色版