我有一个带有两个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));
我想要发生的是,在用户绘制较大的View(mView)并按下按钮后,缩小版本会出现在较小的View(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);
我在这里做错了什么?
答案 0 :(得分: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);