【问题标题】:Add Image overlay on ImageView with custom color使用自定义颜色在 ImageView 上添加图像叠加层
【发布时间】:2016-10-25 11:52:16
【问题描述】:

我有一个 ImageView。我想在它上面添加一个透明覆盖,但我不知道该怎么做。例如这是我的图片:

我想在上面添加我的叠加层,所以它看起来像这样:

正如你所看到的,上面有一个黑色的覆盖层,关键是我想让这个覆盖层可定制并且它的颜色必须改变。我什至不知道我应该搜索哪些关键字才能找到好的结果。

【问题讨论】:

  • 我不确定你的意思......但如果你使用带有 2 个图像视图的 FrameLayout。那么第二个将在第一个之上
  • @RoeeN 感谢您的回答,事实上我可以有两个 imageViews 但我如何在 android 中制作不同颜色的叠加层?
  • 你可以给它一个背景颜色。并设置应该给你一些着色效果的 alpha 级别。
  • @RoeeN 非常感谢,但它似乎涵盖了我所有的其他图像,如你所见,在我的问题中,我需要像这里的东西 i.stack.imgur.com/EH5fM.jpg
  • 我想我现在明白你的意思了......我发布了一个答案

标签: java android xml imageview overlay


【解决方案1】:

shader.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#00000000"
        android:centerColor="#7d000000"
        android:endColor="#7d000000"
        android:dither="true"
        />
</shape>


<ImageView

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/bla2" />

<View
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shader"/>

我认为这就是您的意思,您必须稍微调整一下渐变才能获得与您想要的完全相同的方式

【讨论】:

  • 谢谢..是的,这正是我需要的。我可以通过编程更改着色器颜色吗?
  • 从未尝试过...您可能可以通过编程方式创建并更新它,请查看stackoverflow.com/questions/6115715/…
【解决方案2】:
   <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
          <bitmap android:src="@drawable/android_red"
            android:gravity="center" />
        </item>
        <item android:top="10dp" android:left="10dp">
          <bitmap android:src="@drawable/android_green"
            android:gravity="center" />
        </item>
        <item android:top="20dp" android:left="20dp">
          <bitmap android:src="@drawable/android_blue"
            android:gravity="center" />
        </item>

或以编程方式

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
            Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), 

    bmp1.getHeight(), bmp1.getConfig());
                Canvas canvas = new Canvas(bmOverlay);
                canvas.drawBitmap(bmp1, new Matrix(), null);
                canvas.drawBitmap(bmp2, 0, 0, null);
                return bmOverlay;
            }

或者使用FrameLayout

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:background="#000000">
   <FrameLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:padding="20dp"
       android:background="@android:color/holo_red_dark">
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:padding="20dp"
          android:orientation="vertical"
          android:background="@android:color/holo_purple">

          <ImageView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              ...
              ">

          <ImageView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
               ...
               />
      </LinearLayout>

    </LinearLayout>
  </FrameLayout>
 </LinearLayout>

【讨论】:

  • 非常感谢,但实际上我使用的是 Glide,所以看来我必须使用两个单独的 imageViews..问题是如何创建不同颜色的覆盖层?
猜你喜欢
  • 2011-11-06
  • 1970-01-01
  • 2016-10-18
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多