Palette
使用Palette的 API,能够让我们从Bitmap中获取对应的色调,修改当前的主题色调。
需要添加依赖implementation 'androidx.palette:palette:1.0.0’
//创建Palette对象
Palette.from(getImageBit(findViewById(R.id.palette_iv))).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(@Nullable Palette palette) {
if (palette!=null){
Window window=getWindow();
//设置色调
window.setStatusBarColor(palette.getDarkMutedColor(Color.RED));
}
}
});
private Bitmap getImageBit(ImageView view){
return ((BitmapDrawable)(view.getDrawable())).getBitmap();
}
视图与阴影
View的属性:X,Y,Z
Z= clevation +translationZ
android:elevation="20sp"
Button ev_tv=findViewById(R.id.ev_tv);
ev_tv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
ev_tv.animate().translationZ(20);
break;
case MotionEvent.ACTION_UP:
ev_tv.animate().translationZ(0);
break;
}
return false;
}
});
Tinting(着色)
通过设置不同的tint与tintMode来实现不同的效果
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_gravity="center"
android:elevation="5dp"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:layout_gravity="center"
android:elevation="5dp"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tint="@android:color/holo_blue_bright" />
<ImageView
android:layout_gravity="center"
android:elevation="5dp"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tint="@android:color/holo_blue_bright"
android:tintMode="add"/>
<ImageView
android:layout_gravity="center"
android:elevation="5dp"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tint="@android:color/holo_blue_bright"
android:tintMode="multiply"/>
</LinearLayout>
Clipping(裁剪)
Clipping可以让我们改变一个视图的外形。要使用Clipping ,首先需要使用ViewOutlineProvider来修改outline,然后再通过setOutlineProvider将outline 作用给视图。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:background="@color/colorPrimary"
android:id="@+id/tv_rect"
android:layout_gravity="center"
android:layout_marginTop="20sp"
android:elevation="1dp"
android:layout_width="100dp"
android:layout_height="100dp"/>
<TextView
android:background="@color/colorAccent"
android:id="@+id/tv_circle"
android:layout_gravity="center"
android:layout_marginTop="20sp"
android:elevation="1dp"
android:layout_width="100dp"
android:layout_height="100dp"/>
</LinearLayout>
TextView tv_rect=findViewById(R.id.tv_rect);
TextView tv_circle=findViewById(R.id.tv_circle);
ViewOutlineProvider rect=new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
//修改outline为指定形状
outline.setRoundRect(0,0,view.getHeight(),view.getWidth(),30);
}
};
ViewOutlineProvider circle=new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0,0,view.getWidth(),view.getHeight());
}
};
tv_circle.setOutlineProvider(circle);
tv_rect.setOutlineProvider(rect);
tv_rect.setClipToOutline(true);//开启裁剪
tv_circle.setClipToOutline(true);//开启裁剪