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学习之5.0三姐妹调色板palette的使用
- Android开发之palette的使用(安卓调色板)
- android Palette调色板的使用方法
- android Palette调色板使用详解
- Qt编程18:Qt调色板QPalette的使用
- Android 5.0学习之使用Palette
- android 5.X之使用Palette
- 【译】Android主题动态切换开源库Prism基本原理3-搭配Palette使用
- ym—— Android 5.0学习之使用Palette
- Android 5.x(一) 新控件之RecyclerView,CardView,Palette的使用
- Qt编程18:Qt调色板QPalette的使用
- Android 5.0 Palette的使用小结
- Android RecyclerView、CardView、Palette的使用
- Android5.x 新控件之RecyclerView,CardView,Palette的使用
- Android使用Palette把drawable转为bitmap图像大小改变的问题
- Android5.x 新控件之RecyclerView,CardView,Palette的使用
- Qt编程18:Qt调色板QPalette的使用
- V7兼容包——调色板Palette的使用
- Android 5.0学习之使用Palette
- 使用SeekBar办Android调色板