【问题标题】:Overlay a color over a view在视图上叠加颜色
【发布时间】:2013-11-13 12:38:48
【问题描述】:

对于自定义 AdapterView 实现,我想为所选项目添加颜色叠加。如何向我的 AdapterView 的子项添加颜色叠加?

【问题讨论】:

  • 我不想改变子元素的背景。我想添加一个颜色叠加。这也应该是一个通用的独奏,因为我不知道孩子代表什么类型的视图。
  • 然后将CustomViewImageView 放在您的列表视图项目上。选中后,只需更改CustomViewImageView 的颜色即可。

标签: android events overlay


【解决方案1】:

如果您使用自己的布局,只需将容器更改为RelativeLayout(或继承自RelativeLayout 的布局类),并将颜色叠加层放在主布局之后。您只需要添加一个View 并设置它的背景和alpha。 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% alpha 的绿色层。

【讨论】:

    【解决方案2】:

    好的,我做了更多研究。这就是我最终想到的:

    // 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多