cardview和Palette,ActionBar颜色随图改变
CardView是一个控件,Palette是取色工具(工具类),本文会对他们进行比较细致的介绍,相信机制的各位看完一定轻而易举地实现ActionBar随图改变的特效。
首先看一下效果图:
Gradle(Module:app)配置
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:cardview-v7:24.0.+'
compile 'com.android.support:palette-v7:24.0.+'
}
Cardview
cardview可以理解成带圆角,内边距,阴影的FrameLayout,它本身也是继承自FrameLayout,使用的时候内部再嵌套一个自己喜欢的布局文件,就可以随意使用了,记得在Xml布局开头加这一段话:
xmlns:app="http://schemas.android.com/apk/res-auto"
cardview的可选属性:
- app:cardBackgroundColor这是设置背景颜色
- app:cardCornerRadius这是设置圆角大小
- app:cardElevation这是设置z轴的阴影
- app:cardMaxElevation这是设置z轴的最大高度值
- app:cardUseCompatPadding是否使用CompatPadding
- app:cardPreventCornerOverlap是否使用
- app:contentPadding 设置内容的padding
- app:contentPaddingLeft 设置内容的左padding
- app:contentPaddingTop 设置内容的上padding
- app:contentPaddingRight 设置内容的右padding
- app:contentPaddingBottom 设置内容的底padding
card_view:cardUseCompatPadding 设置内边距,V21+的版本和之前的版本仍旧具有一样的计算方式
card_view:cardPreventConrerOverlap 在V20和之前的版本中添加内边距,这个属性为了防止内容和边角的重叠
Palette
相当于一个图片的颜料吸管,使用的时候传入一个Bitmap 对象,至于取什么颜色就让代码处理了,也有可能会取不到颜色,使用的时候注意判断即可。
创建Palette实例
1. 第一种方式:
public void initPalette2(int imgId, final View view){
Bitmap bitmap = BitmapFactory.decodeResource( getResources(), imgId);
Palette.from( bitmap ).generate( new Palette.PaletteAsyncListener() {
@Override
public void onGenerated( Palette palette ) {
//在这里调用Palette对象
//view.setBackgroundColor(.......);
}
});
}
2. 第二种方式:
private Palette mPalette;
public void initPalette(int imgId) {
//对象实例化
Bitmap bm = BitmapFactory.decodeResource(getResources(), imgId);
mPalette = Palette.from(bm).generate();
}
3. 最实用的方式
获取Drawable对象,
如果是ImageView就这样:img.getDrawable();
如果是Button就这样:btn.getBackground(),
然后把获取的Drawable传入下面这个方法,关于Drawable转Bitmap文章后面还会介绍:
private Palette mPalette;
public void initPalette(Drawable drawable) {
//对象实例化
mPalette = Palette.from(((BitmapDrawable) drawable).getBitmap()).generate();
}
Palette这个类中提取颜色的方式:
需要注意的是getVibrantSwatch()返回一个swatch对象,也可能会返回一个null值,所以检查一下是必须的,一共6种方式:
- Vibrant (有活力)Palette.getVibrantSwatch()
- Vibrant dark(有活力 暗色)Palette.getDarkVibrantSwatch()
- Vibrant light(有活力 亮色)Palette.getLightVibrantSwatch()
- Muted (柔和)Palette.getMutedSwatch()
- Muted dark(柔和 暗色)Palette.getDarkMutedSwatch()
- Muted light(柔和 亮色)Palette.getLightMutedSwatch()
更加实用的方法:
上面的6种方法都有一个与之对应的GetColor的方法,参数是颜色的默认值(int类型),取不到颜色的时候就用默认值
Palette.getDarkMutedColor(Color.GREEN)
swatch对象有以下方法:
- getPopulation(): 像素总数 the amount of pixels which this swatch
represents. - getRgb(): the RGB value of this color.
- getHsl(): 色相(H)、饱和度(S)、明度(L),the HSL value of this color.
- getBodyTextColor(): the RGB value of a text color which can be
displayed on top of this color. - getTitleTextColor(): the RGB value of a text color which can be
displayed on top of this color.
TextView 有个背景图片,要想让字体颜色能够和背景图片匹配,则使用getBodyTextColor()比较合适,getTitleTextColor()其实应该和getBodyTextColor()差不多。
获取全部swatch
你还可以使用如下方法一次性获得所有的swatch:
List<Palette.Swatch> swatches = palette.getSwatches();
在上面的代码中,你可能注意到了可以设置palette的size。size越大,花费的时间越长,而越小,可以选择的色彩也越小。最佳的选择是根据image的用途:头像之类的,size最好在24-32之间;风景大图之类的 size差不多在8-16;默认是16.
- maximunColorCount(int
numOfSwatches)允许你改变从bitmap中生成的Swatch的数量,默认的数量为16.
生成越多的Swatch对象需要耗费越多的时间去生成Palette对象. - resizeBitmapSize(int maxDimension)重新调整bitmap的大小,因此它最大的尺寸只能是此方法定义的最大值.
bitmap越大,生成Palette对象的时间就越长. 同样的,更小的bitmap将会处理得更快些,但是这样的话你可能将会失去颜色的精度.
Drawable换Bitmap
简单的强转了一下,将Drawable转换为Bitmap
public Bitmap drawableToBitamp(Drawable drawable) {
BitmapDrawable bd = (BitmapDrawable) drawable;
return bd.getBitmap();
}
繁琐的drawable转Bitamp方法,使用Canvas重新绘制了一个Bitmap,速度较慢
public Bitmap drawable2Bitamp(Drawable drawable) {
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
Bitmap.Config config =
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
//注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
源码:
Java文件:
public class TestCardView extends AppCompatActivity {
private Palette mPalette;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_view);
this.init();
}
public void init() {
ImageView img= (ImageView) findViewById(R.id.car_view_img);
initPalette(img.getDrawable());
TextView tv1 = (TextView) findViewById(R.id.car_view_txt_1);
TextView tv2 = (TextView) findViewById(R.id.car_view_txt_2);
TextView tv3 = (TextView) findViewById(R.id.car_view_txt_3);
TextView tv4 = (TextView) findViewById(R.id.car_view_txt_4);
TextView tv5 = (TextView) findViewById(R.id.car_view_txt_5);
TextView tv6 = (TextView) findViewById(R.id.car_view_txt_6);
TextView tv7 = (TextView) findViewById(R.id.car_view_txt_7);
TextView tv8 = (TextView) findViewById(R.id.car_view_txt_8);
//设置Actionbar的颜色
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(mPalette.getLightVibrantSwatch().getTitleTextColor()));
//使用Swatch对象取色
if (mPalette.getVibrantSwatch() != null)
tv1.setBackgroundColor(mPalette.getVibrantSwatch().getRgb());
if (mPalette.getDarkVibrantSwatch() != null)
tv2.setBackgroundColor(mPalette.getDarkVibrantSwatch().getRgb());
if (mPalette.getLightVibrantSwatch() != null)
tv3.setBackgroundColor(mPalette.getLightVibrantSwatch().getRgb());
if (mPalette.getMutedSwatch() != null)
tv4.setBackgroundColor(mPalette.getMutedSwatch().getRgb());
if (mPalette.getDarkMutedSwatch() != null)
tv5.setBackgroundColor(mPalette.getDarkMutedSwatch().getRgb());
if (mPalette.getLightMutedSwatch() != null)
tv6.setBackgroundColor(mPalette.getLightMutedSwatch().getRgb());
//使用指定默认值的取色方法
tv8.setBackgroundColor(mPalette.getDarkMutedColor(Color.GREEN));
//通过背景更换字体颜色,原先指定为红色,运行之后为灰色
initPalette(tv7.getBackground());
if (mPalette.getVibrantSwatch() != null)
tv7.setTextColor(mPalette.getVibrantSwatch().getBodyTextColor());
}
public void initPalette(Drawable drawable) {
//对象实例化
mPalette = Palette.from(((BitmapDrawable) drawable).getBitmap()).generate();
}
}
XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".souce.TestCardView">
<android.support.v7.widget.CardView
android:layout_width="150dp"
android:layout_height="150dp"
card:cardBackgroundColor="#FFF"
card:cardCornerRadius="75dp"
card:cardElevation="25dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="@+id/car_view_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:src="@drawable/card_view_img" />
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:background="#669900"
android:text="@string/app_name" />
</LinearLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/car_view_txt_1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Vibrant" />
<TextView
android:id="@+id/car_view_txt_2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Vibrant Dark" />
<TextView
android:id="@+id/car_view_txt_3"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Vibrant Light" />
<TextView
android:id="@+id/car_view_txt_4"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Muted" />
<TextView
android:id="@+id/car_view_txt_5"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Muted Dark" />
<TextView
android:id="@+id/car_view_txt_6"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Muted Light" />
<TextView
android:id="@+id/car_view_txt_7"
android:layout_width="300dp"
android:textColor="#F00"
android:background="@drawable/common_ic_googleplayservices"
android:layout_height="wrap_content"
android:text="GetBodyTextColor" />
<TextView
android:id="@+id/car_view_txt_8"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="GetTitleTextColor" />
</LinearLayout>
cardview和Palette,ActionBar颜色随图改变的更多相关文章
- Android L 之 RecyclerView 、CardView 、Palette
转: http://blog.csdn.net/xyz_lmn/article/details/38735117 <Material Design>提到,Android L版本中新增了Re ...
- Android L中间RecyclerView 、CardView 、Palette使用
RecyclerView CardView Palette <Material Design>提到,Android L版本号中新增了RecyclerView.CardView .Palet ...
- Android L中的RecyclerView 、CardView 、Palette的使用
<Material Design>提到,Android L版本中新增了RecyclerView.CardView .Palette.RecyclerView.CardView为用于显示复杂 ...
- [转] AE之分级颜色专题图渲染
原文 AE之分级颜色专题图渲染 参考代码1 private void 分级渲染ToolStripMenuItem_Click(object sender, EventArgs e) { //值分级 I ...
- Palette状态栏颜色提取,写的不错就分享了
Palette 说Palette之前先说下前面提到的Pager.ViewPager是什么大家应该都是知道的了,一般ViewPager.xxxTabStrip.Fragment三个好基友是一起出现的.这 ...
- IOS - 6\7下UINavigationBar的颜色的方法改变 ——转载http://www.th7.cn/Program/IOS/201310/155057.shtml
IOS7下设置UINavigationBar的颜色的方法已经改变(当然如果是用自定义图片的话请忽略---) 首先是区别iOS7和之前版本的方法如下: //如果是iOS7以前的话if (floor(NS ...
- android——字体颜色跟随状态改变
TextView的字体颜色也可以和ImageView的background一样,跟随状态发生改变.只需要自定义一下字体颜色.在color文件夹下面,新建一个颜色文件的xml. OK ,这就完成 了. ...
- echarts 饼状图 改变折线长度
$(function (){ //ups部分 var myChart = echarts.init(document.getElementById('result')) var option = { ...
- 利用NSMutableAttributedString实现label上字体大小颜色行间距的改变
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.si ...
随机推荐
- mysql主从复制的异步复制与同步复制
异 步复制:MySQL本身支持单向的.异步的复制.异步复制意味着在把数据从一台机器拷贝到另一台机器时有一个延时 – 最重要的是这意味着当应用系统的事务提交已经确认时数据并不能在同一时刻拷贝/应用到从机 ...
- 我的第一个python web开发框架(10)——工具函数包说明(一)
PS:原先是想直接进入功能开发,要用到什么函数时再创建,这样也容易熟悉每个函数的由来和使用方法,但考虑到这样操作,到时会经常在不同文件间切换,不好描述,容易造成混乱,所以还是使用函数库这种方式来说明. ...
- Linux入门(10)——Ubuntu16.04使用pip3和pip安装numpy,scipy,matplotlib等第三方库
安装Python3第三方库numpy,scipy,matplotlib: sudo apt install python3-pip pip3 install numpy pip3 install sc ...
- MySQL比like语句更高效的写法locate position instr find_in_set
使用内部函数instr,可代替传统的like方式查询,并且速度更快. instr函数,第一个参数是字段,第二个参数是要查询的串,返回串的位置,第一个是1,如果没找到就是0. 例如, select na ...
- USACO奶牛博览会(DP)
Description 奶牛想证明他们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N头奶牛进行了面试,确定了每头奶牛的智商和情商. 贝西有权选择让哪些奶牛参加展览.由于负的智商或情商会造成 ...
- mysql 中select for update 锁表的范围备注
mysql的锁表范围测试 1.主键明确时,行级锁: 解释:指定主键并且数据存在时,仅锁定指定的行,其它行可以进行操作 实例:指定了锁定id=1的行且数据存在①,在更新1时lock wait超时②,但是 ...
- JAVA 编码解码
涉及编码的地方一般都在从字符到字节或是从字节到字符间的转换上 1.在IO中存在的编码,主要是 FileOutputStream 和 FileInputStream,在使用时需要指定字符集,而不是使用系 ...
- [Java第一课]环境变量的配置以及eclipse一些常用快捷键
1.环境变量的配置(这里对xp系统电脑来说:) 首先安装jdk软件. 然后在我的电脑(右键)-->属性-->高级-->环境变量-->系统变量(注意)-->新建(新建两个p ...
- 65、django之模型层(model)--添加、单表查询、修改基础
上篇带大家简单做了一下图书表的创建.简单的查看和删除,今天会先简单介绍添加和修改,因为添加和修改与删除一样都很简单,本篇会相对多介绍一点单表查询,大家都知道数据库中查询是最重要的一部分,毕竟无论是修改 ...
- PHP的重载及魔术方法
首先你要知道什么是php的魔术方法,它不是变魔术的,如果你想学习变魔术来错地方了哦! 定义:PHP 将所有以 __(两个下划线)开头的类方法保留为魔术方法.所以在定义类方法时,除了上述魔术方法,建议不 ...