-1

I am trying to declare and initialize a variable globally as final, this variable should holde thewidth and the height of an element in the drawable, how to achieve that. I want smothing like the followinf "ofcourse it is not working, but it is roughly what i am trying to achieve:

private final int w = R.drawable.element.getwidth

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • http://stackoverflow.com/questions/3591784/android-get-width-returns-0 – nobalG Jul 23 '14 at 11:46
  • I don't understand what you mean with "element in the drawable". Could you elaborate? – Xaver Kapeller Jul 23 '14 at 11:47
  • @XaverKapeller I have 52 graphical element in the drawable, all of the same width and height. I want to declare a variable that hold the height and width of a single card – Amrmsmb Jul 23 '14 at 11:53
  • Could you show us the `drawable` you are talking about? You can use ids to get a specific `drawable` from a composed `drawable`. – Xaver Kapeller Jul 23 '14 at 11:54
  • @XaverKapeller I appreciate your attempt to help. but i know how to reference the drawable by ids, but how to get the width of and item in the drawable – Amrmsmb Jul 23 '14 at 11:56
  • Are you just generally asking how to get the size of a `drawable`? Or is there something else. I should tell you that your question doesn't really make sense, by definition you have to tell a drawable how big it should be. They do not have a defined size. – Xaver Kapeller Jul 23 '14 at 11:59
  • @XaverKapeller the drawable has 52 element. how to the width and height of a single element in the drawable? – Amrmsmb Jul 23 '14 at 12:02
  • What kind of elements are you talking about? You have to tell me what kind of `Drawable` you are using and what kind of elements are in there. But again as I have told you: `Drawables` have no size. They just represent something that can be drawn. You tell the drawable how big it should be and it will draw itself that way. You can call `getIntrinsicHeight()` or `getIntrinsicWidth()`, but you shouldn't think of the values returned by those two methods as width and height of the `Drawable`, because as I already told you: a `Drawable` has no predefined size. – Xaver Kapeller Jul 23 '14 at 12:06

1 Answers1

0

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)
Community
  • 1
  • 1
withparadox2
  • 422
  • 4
  • 20