Android scaledrawable doesn't seems to work

前端 未结 5 1213
旧时难觅i
旧时难觅i 2020-12-09 01:58

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 fol

相关标签:
5条回答
  • 2020-12-09 02:23

    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 讨论(0)
  • 2020-12-09 02:24

    ScaleDrawable not only allows you to scale a drawable, it also allows you to control the scale using setLevel on the ScaleDrawable itself. But the ScaleDrawable doesn't change the intrinsic width and height of the drawable As @m039 pointed out, the ScaleDrawable source actually does a strange thing: when the original drawable's level is set to 0, the scaleDrawable itself won't draw.

    To take effect of the scale, you have to do something like this:

    ScaleDrawable scaleDrawable = (ScaleDrawable) imageView.getDrawable();
    scaleDrawable.setLevel(10000);
    or 
    scaleDrawable.setLevel(5000);
    
    0 讨论(0)
  • 2020-12-09 02:28

    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();
    
    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 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)
            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);
    }
    
    0 讨论(0)
  • 2020-12-09 02:28

    You need to add level in your xml.

    android:level="1"
    
    0 讨论(0)
  • 2020-12-09 02:31

    To answer to the initial question, you just have to initialize your ScaleDrawable's Level with 10 000 in your onCreate. You get your ImageView, ImageButton's drawable using getIcon or getBackground (depends if you set it as a source or a background) and then call setLevel on it. For a MenuItem, the 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 running animation that changes the size of the drawable. For your usecase, I definitely prefer managing the size of the component not the drawable displayed by the component.

    0 讨论(0)
提交回复
热议问题