1:Palette(需要添加依赖:compile 'com.android.support:palette-v7:27.0.1')
Palette(调色板)中常用的api
palette.getVibrantSwatch();//有活力的
palette.getDarkVibrantSwatch();//有活力的,暗色
palette.getLightVibrantSwatch();//有活力的,亮色
palette.getMutedSwatch();//柔和的
palette.getDarkMutedSwatch();//柔和的,暗色
palette.getLightMutedSwatch();//柔和的,亮色
Swatch(样品)中常用的api
swatch.getPopulation(): 样本中的像素数量
swatch.getRgb(): 颜色的RBG值
swatch.getHsl(): 颜色的HSL值
swatch.getBodyTextColor(): 主体文字的颜色值
swatch.getTitleTextColor(): 标题文字的颜色值
示例代码如下:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.er);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onGenerated(@NonNull Palette palette) {//注意我第一次用的一个图片中有透明像素,获取到的样品(Swatch)一直为null,palette的中文意思是调色板Swatch的中文意思是样品
Palette.Swatch vibrant = palette.getVibrantSwatch();//有活力的
/* Palette.Swatch vibrantDark = palette.getDarkVibrantSwatch();//有活力的,暗色
Palette.Swatch vibrantLight = palette.getLightVibrantSwatch();//有活力的,亮色
Palette.Swatch muted = palette.getMutedSwatch();//柔和的
Palette.Swatch mutedDark = palette.getDarkMutedSwatch();//柔和的,暗色
Palette.Swatch mutedLight = palette.getLightMutedSwatch();//柔和的,亮色*/
if (vibrant != null) {
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(vibrant.getRgb()));
Window window = getWindow();
window.setStatusBarColor(vibrant.getRgb());
Log.d("CeShi", vibrant.toString());//输出结果为:Swatch [RGB: #ff2090d0] [HSL: [201.81818, 0.73333335, 0.47058824]] [Population: 1321] [Title Text: #89000000] [Body Text: #be000000]
}
}
});
参考文章:
https://blog.csdn.net/xiaochuanding/article/details/72983772
2:视图的阴影
在android5.0中,View的Z值由两部分组成,elevation和translationZ,elevation是静态的成员,translationZ可以在代码中使用来实现动画效果。它们的关系是:Z=elevation+translationZ。通过在XML布局中使用android:elevation="XXdp"来静态设置View的视图高度。在代码中可以使用view.setTranslationZ(XXX)来动态改变视图高度。但是我的手机上显示不出来阴影效果,在android studio中预览时可以看到效果,奇怪。
3:Tinting着色(ImageView中有tint和tintMode属性)
...
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/er"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/er"
android:tint="@color/colorAccent"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/er"
android:tint="@color/colorAccent"
android:tintMode="add"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/er"
android:tint="@color/colorAccent"
android:tintMode="multiply"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/er"
android:tint="@color/colorAccent"
android:tintMode="screen"/>
...
效果如下所示:

4:Ripple,点击后的波纹效果
代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50bbbbbb"
android:orientation="vertical">
<Button
android:layout_width="200dp"
android:layout_height="200dp"
android:background="?android:attr/selectableItemBackground"
android:text="有界波纹"/>
<Button
android:layout_width="200dp"
android:layout_height="200dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:text="无界波纹"/>
</LinearLayout>
同样也可以在XML文件中直接创建一个具有Ripple效果的XML文件,代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorAccent"
android:radius="45dp">
<item>
<shape android:shape="oval">
<solid android:color="#bbbbbb"/>
</shape>
</item>
</ripple>
效果图如下:

网友评论