2015-08-16 21 views
2

反正是有通过类来传递一个Drawable像这样:如何将Drawable传递给类?

public loadImage(Context context, int drawable, int x, int y, int width, int height) { 
    this.drawable = context.getResources().getDrawable(drawable); 
    this.x = x; 
    this.y = y; 
    this.width = width; 
    this.height = height; 
} 

试图将Drawable传递到类:

candyImg = new loadImage(getContext(), getResources().getDrawable(R.drawable.candy), 0, 0, 50, 50); 

它说getResources().getDrawable(drawable);已被弃用。那么如何通过Drawable这样的课程。

+0

这是否回答你的问题? http://stackoverflow.com/questions/10070974/how-to-pass-drawable-using-parcelable – MidasLefko

+1

你的'loadImage'方法需要'int'作为第二个参数,而不是'Drawable' – pskink

回答

1

首先,将int drawable参数更改为Drawable drawable

由于getResources().getDrawable(drawable);已弃用,因此您需要将其替换为ContextCompat.getDrawable(context, R.drawable.my_drawable)

由于Context context参数是多余的,你可以将其删除:

public loadImage(Drawable drawable, int x, int y, int width, int height) { 
    this.drawable = drawable; 
this.x = x; 
this.y = y; 
this.width = width; 
this.height = height; 
} 

然后,尝试把它传递到类:

candyImg = new loadImage(ContextCompat.getDrawable(this, R.drawable.my_drawable), 0, 0, 50, 50); 
相关问题