android Drawable-getConstantState.newDrawable()与mutate() [英] android Drawable - getConstantState.newDrawable() vs mutate()

查看:57
本文介绍了android Drawable-getConstantState.newDrawable()与mutate()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android中,我读了一些有关可绘制对象如何共享恒定状态的文章.因此,如果对可绘制对象进行更改,它将影响所有相同的位图.例如,假设您有一个星形可绘制对象列表.更改一个上的alpha将会更改所有星形可绘制对象的alpha.但是您可以使用mutate来获取自己的可绘制对象的副本,而没有共享状态.
我正在阅读的文章是此处

In android I have read a few articles on how drawables share a constant state. So if you make a change to a drawable it affects all the same bitmaps. For example lets say you had a list of star drawables. changing the alpha on one will change all the star drawables alpha. but you can use mutate to get your own copy of a drawable with no shared state.
The article I was reading is here

现在我的问题:

以下两个android调用之间有何区别:

What is the difference between the following two calls in android:

Drawable clone = drawable.getConstantState().newDrawable();

// vs

Drawable clone = (Drawable) drawable.getDrawable().mutate();

对我来说,他们俩都在克隆可绘制对象,因为它们都返回没有共享状态的可绘制对象.我想念什么吗?

For me they are both cloning a drawable as they both return a drawable that has no shared state. Am I missing something ?

推荐答案

正如@ 4castle在注释中指出的那样, mutate()方法返回具有复制常量可绘制状态的可绘制实例.Docs说

As @4castle pointed in comments mutate() method returns same instance of the drawable with copied constant drawable state. Docs says that

保证可变的可绘制对象不会与其他任何可绘制对象共享状态

A mutable drawable is guaranteed to not share its state with any other drawable

因此可以安全地更改可绘制对象而不影响具有相同状态的可绘制对象

So it is safe to change a drawable without affecting drawables with the same state

让我们玩这个可绘制对象-黑色形状

Lets play with this drawable - a black shape

 <!-- shape.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@android:color/black" />
</shape>


view1.setBackgroundResource(R.drawable.shape); // set black shape as a background
view1.getBackground().mutate().setTint(Color.CYAN); // change black to cyan
view2.setBackgroundResource(R.drawable.shape); // set black shape background to second view


相反的方法是 newDrawable().它创建一个新的可绘制但具有相同恒定状态的对象.例如.看看 BitmapDrawable.BitmapState :


The opposite method is newDrawable(). It creates a new drawable but with the same constant state. E.g. look at BitmapDrawable.BitmapState:

    @Override
    public Drawable newDrawable() {
        return new BitmapDrawable(this, null);
    }

更改为新的可绘制对象不会影响当前的可绘制对象,但会更改状态:

Changes to new drawable will not affect current drawable, but will change a state:

view1.setBackgroundResource(R.drawable.shape); // set black shape as background
Drawable drawable = view1.getBackground().getConstantState().newDrawable();
drawable.setTint(Color.CYAN); // view still black
view1.setBackground(drawable); // now view is cyan
view2.setBackgroundResource(R.drawable.shape); // second view is cyan also

这篇关于android Drawable-getConstantState.newDrawable()与mutate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆