首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >图层列表中的色调位图

图层列表中的色调位图
EN

Stack Overflow用户
提问于 2014-10-23 17:05:08
回答 3查看 12K关注 0票数 23

不再有意义!这个问题与旧的Android4.x版本中的一个bug有关。android:tint现在应该可以正常工作,如下面的示例所示

我正在尝试将染色应用于< layer-list >中的位图

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape  xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
            <solid android:color="@color/grey" />
            <size android:width="45dp" android:height="45dp"/>
        </shape>
    </item>
    <item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp">
        <bitmap android:src="@drawable/ic_action" android:tint="#FF000000"   />
    </item>
</layer-list>

预览显示,它应该可以工作,从ANDROID-STUDIO:

但当部署到设备上时,它不会着色:

如果我在布局中使用ImageView,它可以正确着色,但使用layer-list失败。我相信我已经尝试了所有的tintMode,但都没有结果。

EN

回答 3

Stack Overflow用户

发布于 2017-06-03 07:29:35

对于其他任何可能遇到这个问题的人。这就是我所做的。

设置

  • 安卓2.3.2

Gradle

编译'com.android.support:appcompat-v7:25.3.1'

  • minSdkVersion

15

  • targetSdkVersion 25

图层可绘制我将android:id=@+id/bitmapID添加到包含位图的项目中

代码语言:javascript
复制
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="oval">
            <solid android:color="#9e9e9e" />
            <size android:width="45dp" android:height="45dp"/>
        </shape>
    </item>
    <item
        android:id="@+id/tintDrawableImg"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"
        android:bottom="5dp">
        <bitmap android:src="@mipmap/ic_pencil" android:tint="#860000"   />
    </item>
</layer-list>

活动布局我将可绘制的图层添加到ImageView

代码语言:javascript
复制
<ImageView
    android:id="@+id/tintLayerTest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/test_layer"/>

MainActivity在onCreate()方法中,我们可以使用findViewByID从可绘制的图层中定位位图

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {

    ImageView iv;
    LayerDrawable ld;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Declare ImageView containing LayerDrawable
        iv = (ImageView)findViewById(R.id.tintLayerTest);
        //Get LayerDrawable from ImageView
        ld = (LayerDrawable)iv.getDrawable();
        //Get specific Drawable/Bitmap from within LayerDrawable
        //by ID and pass it as an independent Drawable
        Drawable ldDrawable = ld.findDrawableByLayerId(R.id.tintDrawableImg);
        
        //Pass your new Drawable to DrawableCompat.setTint
        //and define your color int
        DrawableCompat.setTint(ldDrawable, ContextCompat.getColor(this, R.color.colorAccent));

    }
}

我希望这对遇到这个问题的其他人有所帮助。

票数 14
EN

Stack Overflow用户

发布于 2019-09-19 14:26:52

尝试使用位图标记进行xml可绘制

保存以下文件xml custom_image.xml并替换您的图标和色调颜色

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
    <bitmap
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:src="@drawable/iv_veg"
      android:tint="@color/red">
 </bitmap>
票数 0
EN

Stack Overflow用户

发布于 2021-03-03 07:40:43

正如@lucidbrot在评论中提到的那样,android:tint现在应该可以不受任何干扰地工作了。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26524697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档