You can define a function getDrawableWidth
private int getDrawableWidth(Resources resources, int id){
Bitmap bitmap = BitmapFactory.decodeResource(resources, id);
return bitmap.getWidth();
}
Then call it as below:
private final int w = getDrawableWidth(getResources(), R.drawable.element)
===Edit===
define a class to get global context(from here):
public class App extends Application{
private static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
}
public static Context getContext(){
return mContext;
}
}
then change getDrawableWidth
to another format:
private int getDrawableWidth(int id){
Bitmap bitmap = BitmapFactory.decodeResource(App.getContext().getResources(), id);
return bitmap.getWidth();
}
use it any where:
private final int w = getDrawableWidth(R.drawable.element)