【问题标题】:ScaleDrawable doesn't seem to workScaleDrawable 似乎不起作用
【发布时间】:2017-07-18 22:58:25
【问题描述】:

我查看了很多关于如何更改可绘制对象大小的主题,但似乎没有一个有效。

位图给我一个类转换异常,甚至设置 ScaleDrawable 的级别也不起作用。

怎么了?我不是在寻找 xml 解决方案,因为我想根据屏幕大小更改其他项目的大小。

谢谢。

//set seekbar thumb to height of bar
        Drawable thumb = mSeekBarSpeed.getThumb();
        thumb = new ScaleDrawable(thumb, 0, thumb.getIntrinsicWidth(), mSeekBarSpeed.getHeight());
        thumb.setLevel(10000);
        thumb.setBounds(0, 0, thumb.getIntrinsicWidth(), thumb.getIntrinsicHeight());
//        Bitmap thumbBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.seekbar_thumb);
//        Bitmap scaled = Bitmap.createScaledBitmap(thumbBitmap, thumb.getIntrinsicWidth(), mSeekBarSpeed.getHeight(), true);
////
////        Bitmap thumb = Bitmap.createBitmap(50,50, Bitmap.Config.ARGB_8888);
////        Canvas canvas=new Canvas(thumb);
////        canvas.drawBitmap(bitmap,new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()),
////                new Rect(0,0,thumb.getWidth(),thumb.getHeight()),null);
////        Drawable drawable = new BitmapDrawable(getResources(),thumb);
////        setThumb(drawable);
//
//        //Drawable thumb = mSeekBarSpeed.getThumb();
////        thumb.setBounds(0,0, thumb.getIntrinsicWidth(), mSeekBarSpeed.getHeight());
////        Bitmap orig = ((BitmapDrawable)thumb).getBitmap();
////        Bitmap scaled = Bitmap.createScaledBitmap(orig, thumb.getIntrinsicWidth(), mSeekBarSpeed.getHeight(), true);
//        Drawable newThumb = new BitmapDrawable(getResources(), scaled);
//        newThumb.setBounds(0,0,newThumb.getIntrinsicWidth(),newThumb.getIntrinsicHeight());
        mSeekBarSpeed.setThumb(thumb);

【问题讨论】:

    标签: android drawable android-drawable


    【解决方案1】:

    找到了一种使用位图使其工作的方法。

        Drawable thumb = mSeekBarSpeed.getThumb(); //get thumb of seekBar
    
        Bitmap thumbBitmap; //try to find bitmap of thumb
        if(thumb instanceof BitmapDrawable) {
            thumbBitmap  = ((BitmapDrawable)thumb).getBitmap();
        }else{
            Bitmap bitmap = Bitmap.createBitmap(thumb.getIntrinsicWidth(),thumb.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            thumb.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            thumb.draw(canvas);
            thumbBitmap = bitmap;
        }
    
        //scale the bitmap
        float scale =  (float)1.00 * mSeekBarSpeed.getHeight() / thumb.getIntrinsicHeight();
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(thumbBitmap,
                Math.round(thumb.getIntrinsicWidth() * scale),
                Math.round(thumb.getIntrinsicHeight() * scale), true);
    
        //make is a drawable
        Drawable newThumb = new BitmapDrawable(getResources(), scaledBitmap);
    
        //set thumb to new thumb
        mSeekBarSpeed.setThumb(newThumb);
    

    来源:Get All Installed Application icons in Android : java.lang.ClassCastException 有一些关于位图的 Class Cast Exception 的信息。

    【讨论】:

      猜你喜欢
      • 2011-07-27
      • 2016-11-29
      • 2016-02-01
      • 2020-09-23
      • 2010-12-05
      • 2011-06-14
      • 2015-01-10
      • 2016-02-24
      • 2011-01-18
      相关资源
      最近更新 更多