对于自定义的AdapterView实现,我想给选中的项目添加颜色覆盖层。如何为我的AdapterView的子项添加颜色覆盖层?
RelativeLayout
(或继承自RelativeLayout
的Layout类),然后在主布局后放置颜色叠加层。您只需要添加一个View
并设置其背景和透明度。alpha
设置为值1将激活此叠加层,而将alpha
设置为0将返回到正常模式。<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- add your views here -->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8000FF00" />
</RelativeLayout>
这将在布局中排在它之前的其他视图上叠加一个50%透明度的绿色层。
好的,我进行了更多的研究。这是我最终得出的结论:
// prepare a gray filter setting saturation to 0, or ...
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
Paint paint = new Paint();
ColorFilter filter = new ColorMatrixColorFilter(cm);
// ... prepare a color filter
ColorFilter filter = new PorterDuffColorFilter(Color.rgb(34, 136, 201), PorterDuff.Mode.OVERLAY);
// create paint
paint.setColorFilter(filter);
// override dispatchDraw of the view
@Override
protected void dispatchDraw(Canvas canvas) {
if (isPressed()) {
canvas.saveLayer(null, paint, Canvas.ALL_SAVE_FLAG);
super.dispatchDraw(canvas);
canvas.restore();
} else {
super.dispatchDraw(canvas);
}
}
CustomView
或ImageView
。当选中时,只需更改CustomView
或ImageView
的颜色即可。 - Chintan Rathod