Gratzi
Gratzi

Reputation: 4693

Create drawable from layout

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.

Upvotes: 3

Views: 6686

Answers (1)

Alex Orlov
Alex Orlov

Reputation: 18107

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);
    }

Upvotes: 10

Related Questions