stephenteh stephenteh - 3 years ago 192
Android Question

Android scaledrawable doesn't seems to work

I'm trying out the android ScaleDrawable

But it doesn't seem to work... I created the xml as in the documentation example and I have a logo.png in the drawable folder

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android=""
android:scaleWidth="80%" />

I saved this as logo2.xml in the drawable folder then I have a LinearLayout with an ImageView and set the src of the ImageView to "@drawable/logo2" but the logo doesn't appear...Am I doing something wrong here? How do you actually use the scale element?

Answer Source

Just today I faced the same problem. The ScaleDrawable does not seem to work. After some investigation I've found the solution:

If you look at the documentation you can find this phrase: "A Drawable that changes the size of another Drawable based on its current level value." That's it.

It became clear after I found the onBoundsChange() function uses the strange level variable.

For you the code would be something like this:

Resources res = getResources();
ScaleDrawable sd = (ScaleDrawable) res.getDrawable(R.drawable.logo2);
Drawable d = sd.getDrawable();


ImageView iv = new ImageView(this);
iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));


Update: I think the problem in the draw() function. Because after an initialization the level variable is 0:

public void draw(Canvas canvas) {
    if (mScaleState.mDrawable.getLevel() != 0)

Update: Maybe then this code may help you:

private void useScaledImage() {
    Resources res = getResources();
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(R.drawable.sun);
    Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(),
                         (int) (bd.getIntrinsicHeight() * 0.7),
                         (int) (bd.getIntrinsicWidth() * 0.7),

    LinearLayout l = new LinearLayout(this);
    ImageView iv = new ImageView(this);

    iv.setImageDrawable(new BitmapDrawable(b));
    iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download