简体   繁体   English

如何将画布保存到位图中

[英]How to save a canvas into a bitmap

I'm trying to save a background from a ViewPagerParallax which can be found here : link 我正在尝试从ViewPagerParallax保存背景,可以在这里找到它: 链接

When i move, the background changes, and i want to take this "part" of the background and pass it to another activity. 当我移动时,背景会发生变化,我想将背景的这一“部分”传递给另一个活动。

To pass it from one to another activty i can : 要将其从一项活动传递到另一项活动,我可以:

 Intent intent = new Intent(context, Activity2.class);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            //Bitmap bmp = pager.getSavedBitmap().getBitmap();
            Bitmap bmp = pager.getBitmap();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            intent.putExtra("image",byteArray);
            context.startActivity(intent);

    byte[] byteArray = getIntent().getByteArrayExtra("image");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    @SuppressWarnings("deprecation")
    Drawable d = new BitmapDrawable(bmp);
    layout.setBackground(d);

But know to take the part of the background i want is difficult, i'm trying to take it from the onDraw method like this : 但是知道要获得我想要的背景部分是困难的,我正试图从像这样的onDraw方法中获取它:

 canvas.drawBitmap(saved_bitmap, src, dst, null);
        if(canvas != null){         
            my_bitmap = new BitmapDrawable();
            my_bitmap.draw(canvas);
        }

But when i use getBitmap : 但是当我使用getBitmap时:

public Bitmap getBitmap(){
    return my_bitmap.getBitmap();
}

the image is not scaled like it was in the first activity. 图像没有像第一个活动那样缩放。

Doesn't this post have a similar issue? 这篇帖子没有类似的问题吗? Android Save Canvas into Bitmap Android将画布保存到位图

Perhaps it might help. 也许有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM