Ann*_*oid 6 android view bitmap
问题描述:
我已经测试了以下两种方法,以便从View创建一个位图snaphot(例如在这种情况下为RelativeLayout).这两种方法都适用于视图,其尺寸(例如宽度和高度)小于设备屏幕的尺寸(例如480x900).捕获视图的可见部分并将其写入位图.不幸的是,在View的不可见部分中,位图是黑色的.
问题:
我如何捕捉视图中不可见的部分?
码:
public class NewRelativeLayout extends RelativeLayout{
private Bitmap bmp;
public NewRelativeLayout(Context context){
super(context);
this.setLayoutParams(new RelativeLayout.LayoutParams(2000,2000));
// add here methods to configure the RelativeLayout or to add children
}
//This is the first method
public void makeSnapshot_Method1(){
this.setDrawingCacheEnabled(true);
bmp = Bitmap.createBitmap(this.getDrawingCache());
this.setDrawingCacheEnabled(false);
}
//This is the second method
public void makeSnapshot_Method2(){
bmp = Bitmap.createBitmap(2000, 2000, Bitmap.Config.ARGB_8888);
Canvas helpCanvas = new Canvas(bmp);
this.draw(helpCanvas);
}
Run Code Online (Sandbox Code Playgroud)
}
取决于您想要捕获哪个图像,视图背景或前景的图像。无论哪种方式,您都必须从它们获取 Drawable 对象,将它们转换为 BitmapDrawable 并从它们获取 Bitmap。代码:-
BitmapDrawable bd=(BitmapDrawable) view.getBackground(); //if you need background image
BitmapDrawable bd=(BitmapDrawable) view.getDrawable(); //if you need foreground image (Only available to some views who have an android:src xml property)
Bitmap bm=bd.getBitmap();
Run Code Online (Sandbox Code Playgroud)
我希望这对你有用。
| 归档时间: |
|
| 查看次数: |
1900 次 |
| 最近记录: |