【问题标题】:Generate bitmap from view从视图生成位图
【发布时间】:2015-06-18 13:08:32
【问题描述】:

当我从视图的 DrawingCache 中获取位图时,我没有收到任何错误,但我也没有看到有效的位图。我究竟做错了什么?

我用来生成位图的代码:

SharePhotoView sharePhotoView = SharePhotoView_.build(this);
sharePhotoView.bind(mCatch);

sharePhotoView.setDrawingCacheEnabled(true);
sharePhotoView.buildDrawingCache();
Bitmap bitmap = sharePhotoView.getDrawingCache();

catchImage.setImageBitmap(bitmap);

我用来制作视图的代码:

@EViewGroup(R.layout.share_photo)
public class SharePhotoView extends LinearLayout{

    @ViewById
    ImageView catchImage;

    public SharePhotoView(Context context) {
        super(context);
    }

    public void bind(Catch catchItem) {
        Bitmap bitmap = BitmapFactory.decodeFile(catchItem.getImage().getPath());

        catchImage.setImageBitmap(bitmap);

    }
}

【问题讨论】:

    标签: android bitmap android-annotations


    【解决方案1】:

    将此代码用于视图中的位图:

    Bitmap bitmap;
                try {
                    bitmap = Bitmap.createBitmap(YOUR_VIEW.getWidth(), YOUR_VIEW.getHeight(),
                            Bitmap.Config.ARGB_8888);
    
                    Canvas canvas = new Canvas(bitmap);
                    YOUR_VIEW.draw(canvas);
    
                    File root = Environment.getExternalStoragePublicDirectory(Environment.PICTURES);
    
                    String fname = "NAME_OF_FILE.jpg";
                    file = new File(root, fname);
    
                    try {
                        if (!root.exists()) {
                        root.mkdir();
                    }
                        FileOutputStream out = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                        out.flush();
                        out.close();
                        YOUR_VIEW.destroyDrawingCache();
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                } catch (Exception e) {
                }
    

    【讨论】:

    • 不保存不就可以了吗?
    • 是的,您在我的代码中有位图,我保存它以了解这是否是您想要的结果,只需在您的代码中保存 setImageBitmap(bitmap)。
    • 但我不能使用 getHeight 和 getWidth 因为我认为它没有被渲染。正如您在我发布的第二个代码中看到的那样,我只是从布局文件中获取视图。
    • 好的,我看不到您发布的完整代码和logcat,但我认为您在创建视图之前调用了您的绘制视图方法,您需要创建视图并在完成后绘制它在屏幕上绘制。
    【解决方案2】:

    在某个地方找到了一个可以解决问题的方法:

    public static Bitmap getScreenViewBitmap(View v) {
        v.setDrawingCacheEnabled(true);
    
        // this is the important code :)
        // Without it the view will have a dimension of 0,0 and the bitmap will be null
        v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    
        v.buildDrawingCache(true);
        Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
        v.setDrawingCacheEnabled(false); // clear drawing cache
    
        return b;
    }
    

    来源:http://www.jattcode.com/getting-bitmap-from-a-view-visible-invisible-oncreate/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-29
      • 2016-11-04
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多