[ACCEPTED]-Android scaledrawable doesn't seems to work-android
Just today I faced the same problem. The 11 ScaleDrawable does not seem to work. After 10 some investigation I've found the solution:
If 9 you look at the documentation you can find this phrase: "A 8 Drawable that changes the size of another 7 Drawable based on its current level value." That's 6 it.
It became clear after I found the onBoundsChange() function 5 uses the strange level variable.
For you 4 the code would be something like this:
Resources res = getResources();
ScaleDrawable sd = (ScaleDrawable) res.getDrawable(R.drawable.logo2);
Drawable d = sd.getDrawable();
d.setLevel(1);
ImageView iv = new ImageView(this);
iv.setImageDrawable(sd);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView(iv);
Update: I 3 think the problem in the draw() function. Because 2 after an initialization the level variable 1 is 0:
public void draw(Canvas canvas) {
if (mScaleState.mDrawable.getLevel() != 0)
mScaleState.mDrawable.draw(canvas);
}
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),
false);
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));
l.addView(iv);
setContentView(l);
}
Use inset drawable instead of scale drawable
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/logo"
android:insetTop="2dp"
android:insetLeft="2dp"
android:insetRight="2dp"
android:insetBottom="2dp"
>
0
You need to add level in your xml.
android:level="1"
0
To answer to the initial question, you just 11 have to initialize your ScaleDrawable's 10 Level with 10 000 in your onCreate. You 9 get your ImageView, ImageButton's drawable 8 using getIcon or getBackground (depends 7 if you set it as a source or a background) and 6 then call setLevel on it. For a MenuItem, the 5 code looks like :
editMenuItem = menu.findItem(R.id.action_edit);
menuItemEdit_ScaleDrawable = (ScaleDrawable) editMenuItem.getIcon();
//when playing with scaleDrawable always initialize the level
menuItemEdit_ScaleDrawable.setLevel(10000);
I use ScaleDrawable when 4 running animation that changes the size 3 of the drawable. For your usecase, I definitely 2 prefer managing the size of the component 1 not the drawable displayed by the component.
ScaleDrawable not only allows you to scale 10 a drawable, it also allows you to control 9 the scale using setLevel on the ScaleDrawable 8 itself. But the ScaleDrawable doesn't change 7 the intrinsic width and height of the drawable As 6 @m039 pointed out, the ScaleDrawable source 5 actually does a strange thing: when the 4 original drawable's level is set to 0, the 3 scaleDrawable itself won't draw.
To take 2 effect of the scale, you have to do something 1 like this:
ScaleDrawable scaleDrawable = (ScaleDrawable) imageView.getDrawable();
scaleDrawable.setLevel(10000);
or
scaleDrawable.setLevel(5000);
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.