【问题标题】:Programmatically change solid color in an existing shape以编程方式更改现有形状中的纯色
【发布时间】:2016-10-25 12:34:49
【问题描述】:

我有一个带有自定义边框的圆形视图,使用CircularImageView 库实现并在RecyclerView 中膨胀。到目前为止,我已经在里面放了一张图片,但现在我想用一个单色的圆形和一个文字来替换它。

我创建了以下形状 (shape_colored_circle.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:height="70dp"
        android:width="70dp"/>
    <solid
        android:color="#00f"/>
</shape>

并在FrameLayout 中添加了TextViewCircularImageView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <com.mikhaellopez.circularimageview.CircularImageView
            android:id="@+id/card_thumbnail"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/icon"
            app:civ_border="true"
            app:civ_border_color="@color/border_color"
            app:civ_border_width="2dp"
            app:civ_shadow="true"
            app:civ_shadow_color="@color/grey"
            app:civ_shadow_radius="10"
            android:contentDescription="@string/card_thumbnail"/>

        <TextView
            android:id="@+id/card_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/app_name"
            android:textSize="18sp"
            android:textStyle="bold"/>

    </FrameLayout>

</LinearLayout>

我知道布局看起来有点奇怪(“为什么顶部 LinearLayout 是必要的?”),但没有它,视图根本不会出现在 RecyclerView 中,而这个布局(取自图书馆的例子)满足我们的需求。

onBindViewHolder 中,我将形状设置为CircularImageView 资源:

holder.thumbnail.setImageDrawable(activity.getResources().getDrawable(R.drawable.shape_colored_circle));

最后我看到了我所期待的。问题是我的drawable预先设置了特定的颜色。因此,我尝试以编程方式创建形状:

GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.RECTANGLE);
gd.setColor(colors[position]);             //Getting the color from a predefined array of colors
holder.thumbnail.setImageDrawable(gd);

但是视图只是变成了一个黑色矩形。我尝试了更多以编程方式创建形状的解决方案(所有这些都非常相似)但无济于事 - 我要么看到一个黑色矩形,要么什么都没有。

我做错了吗?是否有另一种创建形状的方法,或者在运行时更改现有形状的方法?或者也许是一种完全不同的方式来实现所需的行为?

谢谢!

【问题讨论】:

  • 几年前,当我尝试以编程方式设置颜色时,我遇到了一个问题,即我的可绘制对象的背景变黑了。也许有类似的问题? stackoverflow.com/questions/24581900/…
  • @0x5453 我添加了gd.setAlpha()。对于值 255,它是一个黑色矩形,对于值 0,它(显然)是不可见的。您认为您的解决方案与以编程方式创建的可绘制对象相关吗?

标签: java android android-imageview android-drawable


【解决方案1】:
   <com.mikhaellopez.circularimageview.CircularImageView
    android:id="@+id/card_thumbnail"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:background="@drawable/shape_colored_circle" //Note
    android:contentDescription="@string/card_thumbnail"
    android:src="@drawable/icon"
    app:civ_border="true"
    app:civ_border_color="@color/border_color"
    app:civ_border_width="2dp"
    app:civ_shadow="true"
    app:civ_shadow_color="@color/grey"
    app:civ_shadow_radius="10"/>`

GradientDrawable gd = (GradientDrawable) holder.thumbnail.getBackground()
if (gd != null) {
  gd.mutate();
  gd.setColor(colors[position]);
}

【讨论】:

  • 问题是CircularImageView背景是圆后面的整个矩形。因此我必须设置源而不是背景。
【解决方案2】:

感谢Vijay Pal Vishwakarma我设法解决了这个问题。

首先,我将预定义的形状设置为CircularImageView的来源:

<com.mikhaellopez.circularimageview.CircularImageView
            android:id="@+id/card_thumbnail"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/shape_colored_circle"      //Note this line
            app:civ_border="true"
            app:civ_border_color="@color/border_color"
            app:civ_border_width="2dp"
            app:civ_shadow="true"
            app:civ_shadow_color="@color/grey"
            app:civ_shadow_radius="10"
            android:contentDescription="@string/card_followee_large_thumbnail"/>

然后,在onBindViewHolder 中,我得到现有的可绘制对象并更改其颜色:

GradientDrawable gd = (GradientDrawable) holder.thumbnail.getDrawable();
if (gd != null) {
    gd.setColor(colors[position]);
}

【讨论】:

  • 这是我的方法的精确副本,不是吗?
  • 不,这里shape_colored_circle 用作来源而不是背景。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
  • 1970-01-01
  • 2016-10-11
  • 2014-01-09
  • 1970-01-01
相关资源
最近更新 更多