【问题标题】:Android - Unable to show rescaled bitmap properly in a canvasAndroid - 无法在画布中正确显示重新缩放的位图
【发布时间】:2013-08-23 03:31:17
【问题描述】:

我有一个应用,有两个RelativeView,一个是Matchparent,250dp,一个是300dp,200dp。

它们的初始化如下:

//These are global variables for the views
View mView;
View resizedView;

//These lines allow the user to draw to the RelativeViews, this code is on the onCreate
//SignatureView class courtesy of Square

mView = new SignatureView(activity, null);
resizedView = new SignatureView(activity, null);

layout.addView(mView, new LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT));

resized.addView(resizedView, new LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT));

我想要发生的是,在用户在较大的视图 (mView) 上绘制并按下按钮后,该缩放版本会出现在较小的视图 (resizedView) 中。

这是我迄今为止在按钮的 onClick 功能中所拥有的,但它不起作用。

//this code fetches the drawing from the larger view
Bitmap bitmap = Bitmap.createBitmap(mView.getWidth(), 
    mView.getHeight(), Bitmap.Config.ARGB_8888);

//this code resized it accordingly to a new bitmap
Bitmap resizedImage = Bitmap.createScaledBitmap(bitmap, 399, 266, true);

//this is my sad attempt on creating a canvas with the resized image
Canvas c = new Canvas(resizedImage);
c.drawColor(Color.WHITE);
c.drawBitmap(resizedImage, 0, 0, null);

//this is where I attempt to attach the scaled image to the smaller view
resizedView.draw(c);

//then I go resize it because I need to save/print it
ByteArrayOutputStream stream = new ByteArrayOutputStream();
resizedImage.compress(Bitmap.CompressFormat.PNG, 90, stream);

我在这里做错了什么?

【问题讨论】:

    标签: android canvas bitmap scale


    【解决方案1】:

    问题在于,调整大小图像的位图是在创建获得用户输入的位图之后立即实例化的。这是我的有效代码。

    但是,请注意,我创建了一个 imageView 来保存调整大小的图像。

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
    
        //get the user input and store in a bitmap
        Bitmap bitmap = Bitmap.createBitmap(mView.getWidth(), mView.getHeight(), Bitmap.Config.ARGB_8888);
    
    
        //Create a canvas containing white background and draw the user input on it
        //this is necessary because the png format thinks that white is transparent
        Canvas c = new Canvas(bitmap);
        c.drawColor(Color.WHITE);
        c.drawBitmap(bitmap, 0, 0, null);
    
        //redraw 
        mView.draw(c);
    
        //create resized image and display
        Bitmap resizedImage = Bitmap.createScaledBitmap(bitmap, 399, 266, true);
        imageView.setImageBitmap(resizedImage);
    

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多