简体   繁体   English

资源是颜色还是可绘制的?

[英]Is resource a color or a drawable?

I have create a little custom view that extends ImageView. 我创建了一个扩展ImageView的自定义视图。 My custom view provides a method showError(int) , where I can pass a resource id, which should be displayed as the image views content. 我的自定义视图提供了一个方法showError(int) ,我可以在其中传递资源ID,该资源ID应显示为图像视图内容。 It would be great if I could pass a simple color resource id or a drawable resource id. 如果我可以传递简单的颜色资源ID或可绘制的资源ID,那将是很好的。

My problem is: how do I determine if the passed resource id is a Drawable or a Color? 我的问题是:如何确定传递的资源ID是Drawable还是Color?

My current approach is something like this: 我目前的做法是这样的:

class MyImageView extends ImageView{

     public void showError(int resId){

        try{
            int color = getResources().getColor(resId);
            setImageDrawable(new ColorDrawable(color));
        }catch(NotFoundException e){
            // it was not a Color resource so it must be a drawable
            setImageDrawable(getResources().getDrawable(resId));
        }

     }
}

Is it safe to do that? 这样做安全吗? My assumption is, that a resource id really unique. 我的假设是,资源ID真的很独特。 I mean not unique in R.drawable or R.color, but completely unique in R 我的意思是在R.drawable或R.color中不是唯一的,但在R完全独特的

So that there is no 所以没有

R.drawable.foo_drawable = 1;
R.color.foo_color = 1;

Is it correct that the id 1 will be assigned only to one of this resources but not to both? 是否正确将id 1分配给其中一个资源但不分配给两个资源?

You probably want to look up a TypedValue from the resources, to allow you to determine whether the value is a Color or a Drawable . 您可能希望从资源中查找TypedValue ,以便确定该值是Color还是Drawable Something like this should work without needing to throw and catch an exception: 像这样的东西应该工作,而不需要抛出并捕获异常:

TypedValue value = new TypedValue();
getResources().getValue(resId, value, true); // will throw if resId doesn't exist

// Check whether the returned value is a color or a reference
if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
    // It's a color
    setImageDrawable(new ColorDrawable(value.data));
} else if (value.type == TypedValue.TYPE_REFERENCE) {
    // It's a reference, hopefully to a drawable
    setImageDrawable(getResources().getDrawable(resId));
}

First of all, everything you get from getResources are drawables. 首先,你从getResources得到的一切都是drawables。 ColorDrawable is just a subclass of Drawable as are BitMapDrawable and many others ( http://developer.android.com/guide/topics/resources/drawable-resource.html ). ColorDrawable只是Drawable的子类,BitMapDrawable和其他许多子类( http://developer.android.com/guide/topics/resources/drawable-resource.html )。

Furthermore Android makes sure all values in the R file are unique (so it is impossible to get the same values like you described, even when they would of different instances). 此外,Android确保R文件中的所有值都是唯一的(因此不可能像您描述的那样获得相同的值,即使它们具有不同的实例)。 The only case it would return identical values is when the resource has not been found (it will return 0). 返回相同值的唯一情况是未找到资源时(它将返回0)。 Find the part about unique ID here 此处查找有关唯一ID的部分

Hope this helps 希望这可以帮助

Take a look inside your R.java file. 查看您的R.java文件。 You'll see that all the resource ids are defined in there, each one having a unique 32 bit number. 您将看到所有资源ID都在那里定义,每个资源都具有唯一的32位数。 They are also grouped together by type. 它们也按类型分组。 For example you should see all your drawable ids in a group: 例如,您应该在组中看到所有可绘制的ID:

public static final class drawable {
    public static final int my_drawable_1=0x7f020000;
    public static final int my_drawable_2=0x7f020001;

The resource id is of the form PPTTNNNN where PP is always 0x7f and TT is the type. 资源ID的格式为PPTTNNNN,其中PP始终为0x7f,TT为类型。 I expect all your drawables use '02' for the type, but worth checking your own file. 我希望你的所有drawable都使用'02'作为类型,但值得检查你自己的文件。 In that case, if the id is between 0x7f020000 and 0x7f029999, you can assume it is a drawable. 在这种情况下,如果id在0x7f020000和0x7f029999之间,您可以假设它是可绘制的。

You can do in this way too 你也可以这样做

 TypedValue value = new TypedValue();

 context.getResources().getValue(resId, value, true);

// Check if it is a reference
if (value.type == TypedValue.TYPE_REFERENCE) {
    ....
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM