0

working on generate reflection for thumbnail in recent app menu

the code works very well with CM10 but since update to CM11 (kitkat) they moved to use Drawable instead of Bitmap for reduce memory usage

https://github.com/CyanogenMod/android_frameworks_base/commit/9926272f32868c858b24b45e048210cf3515741e

here should add the changes:

https://github.com/CyanogenMod/android_frameworks_base/blob/1e3c4a9e687b19cd7837fed51eb25e92a4f691c1/packages/SystemUI/src/com/android/systemui/recent/RecentsPanelView.java#L509

here my code:

        final int reflectionGap = 4;
        int width = thumbnail.getWidth();
        int height = thumbnail.getHeight();

        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        Bitmap reflectionImage = Bitmap.createBitmap(thumbnail, 0, height * 2 / 3, width, height/3, matrix, false);     
        Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/3), Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmapWithReflection);
        canvas.drawBitmap(thumbnail, 0, 0, null);
        Paint defaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

        Paint paint = new Paint(); 
        LinearGradient shader = new LinearGradient(0, thumbnail.getHeight(), 0, 
        bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, 
        TileMode.CLAMP); 
        paint.setShader(shader); 
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
        canvas.drawRect(0, height, width, 
        bitmapWithReflection.getHeight() + reflectionGap, paint); 

        h.thumbnailViewImage.setImageBitmap(bitmapWithReflection);

my question is :

how i can make this code works with Drawable instead of Bitmap

thanks

Hany Alsamman
  • 491
  • 6
  • 20

1 Answers1

0

try this:

Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, w, h);
Bitmap b = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas c = new Canvas(b);
c.translate(0, h);
c.scale(1, -1);
drawable.draw(c);

// test it
ImageView iv = new ImageView(this);
iv.setImageBitmap(b);
setContentView(iv);
pskink
  • 23,874
  • 6
  • 66
  • 77