I have an application with two RelativeViews, the dimension of one is Matchparent, 250dp, and the other one is 300dp, 200dp.
They are initialized as follows:
//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));
What I want to happen is that after the user draws on the larger View (mView) and presses a button, a scaled version of that appears in the smaller View (resizedView).
This is what I have so far in that onClick function of the button but it does not work.
//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);
What am I doing wrong here?
The problem is that the Bitmap of the resized image is instantiated right after the creation of the bitmap that gets the user input. This was my code that worked.
However, note that I made an imageView to hold the resized image.
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);