对于自定义AdapterView实现,我想为所选项添加颜色叠加。如何为AdapterView的子项添加颜色叠加?
答案 0 :(得分:9)
如果您使用自己的布局,只需将容器更改为RelativeLayout
(或继承自RelativeLayout
的布局类),并将颜色叠加层放在主布局之后。您只需要添加View
并设置它的背景和alpha。设置为值1的alpha
将激活此叠加层,而将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%的alpha覆盖在布局之前的其他视图中。
答案 1 :(得分:5)
// 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);
}
}