2015-08-16 80 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);