【问题标题】:How do I in an Android Imageview, replace one colour with another (black to white) in the xml?如何在 Android Imageview 中用 xml 中的另一种颜色(从黑到白)替换一种颜色?
【发布时间】:2015-01-16 10:51:11
【问题描述】:

我如何在 Android Imageview 中将xml 中的一种颜色替换为另一种颜色(黑色到白色)?

我在水平linearlayout中放置了3张图片,每张图片都有一个透明的背景,但主要是黑色。我想在 Android xml 中将每个图像的颜色替换为白色。

我不知道该怎么做,你建议如何做到这一点?

到目前为止我的代码如下:

 <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="230dp">

                    <ImageView
                        android:id="@+id/imageview_se"
                        android:layout_marginTop="5dp"
                        android:layout_marginLeft="10dp"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/search"
                        android:backgroundTint=""/>

                    <ImageView
                        android:id="@+id/imageview_hea"
                        android:layout_marginTop="5dp"
                        android:layout_marginLeft="10dp"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/heart"/>

                    <ImageView
                        android:id="@+id/imageview_menu"
                        android:layout_marginTop="5dp"
                        android:layout_marginLeft="10dp"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/menu" />



                    </LinearLayout>

我的图标:

【问题讨论】:

    标签: java android xml android-layout android-activity


    【解决方案1】:

    您是在谈论将 ImageView 的源 (android:src) 更改为白色,对吗?不是背景色?如果是这样,您不能在 xml 中执行此操作,您必须以编程方式更改颜色。

    ImageView seImageView = (ImageView) findViewById(R.id.imageview_se);
    ImageView heaImageView = (ImageView) findViewById(R.id.imageview_hea);
    ImageView menuImageView = (ImageView) findViewById(R.id.imageview_menu);
    
    seImageView.setColorFilter(Color.parseColor("#ffffff"));
    heaImageView.setColorFilter(Color.parseColor("#ffffff"));
    menuImageView.setColorFilter(Color.parseColor("#ffffff"));
    

    【讨论】:

    • 到目前为止,当我尝试这样做时。整个 Imageview 被白色(“#ffffff”)取代,而不仅仅是图标
    • 不应该。你的图标是透明的纯黑色png吗?可以发图吗?
    • 为了好玩,我把你的图标放到了我自己的一个项目中,并对它们应用了颜色过滤器。结果完全符合预期。我仍然可以建议的唯一一件事是查看您的代码并检查您是否也没有在 ImageView 的某处设置背景颜色。
    【解决方案2】:

    使用此方法将您的可绘制对象转换为白色或任何其他颜色,并将该可绘制对象应用于您的图像视图,

    public static Drawable convertColorDrawable(int resource, int color, Context context) {
            final Drawable drawable = context.getResources().getDrawable(resource);
    
            drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    
            return drawable.mutate();
    }
    

    例子:

    ImageView imageView1 = (ImageView) findViewById(R.id.imageview_se);
    
    seImageView.setImageDrawable(convertColorDrawable(R.drawable.search, android.R.color.white, context));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2017-10-05
      • 2019-01-29
      • 2014-05-07
      • 1970-01-01
      • 2013-06-09
      • 2012-02-18
      相关资源
      最近更新 更多