从布局创建drawable

时间:2011-05-10 13:42:03

标签: android

有没有办法从布局生成可绘制对象?

事实上,我需要初始布局的裁剪部分,我的想法是将布局转换为可绘制对象,然后裁剪可绘制。

1 个答案:

答案 0 :(得分:9)

简单版本:

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