0

我有一个应用有两个RelativeViews,一个是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);

我在这里做错了什么?

4

1 回答 1

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);
于 2013-08-24T08:45:49.000 回答