Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create drawable from layout

Tags:

android

Is there a way to generate a drawable object from a layout?

In fact, I need a cropped part of my initial layout, and my idea is to transform the layout into a drawable object and then to crop drawable.

like image 717
Gratzi Avatar asked Aug 13 '26 15:08

Gratzi


1 Answers

A simple version:

Bitmap snapshot = null;
    Drawable drawable = null;
    yourView.setDrawingCacheEnabled(true);
    yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW); //Quality of the snpashot
    try {
        snapshot = Bitmap.createBitmap(yourView.getDrawingCache(), sizes and stuff); // You can tell how to crop the snapshot and whatever in this method
        drawable = new BitmapDrawable(snapshot)
    } finally {
        yourView.setDrawingCacheEnabled(false);
    }
like image 52
Alex Orlov Avatar answered Aug 15 '26 08:08

Alex Orlov