Draw bitmap larger than screen
I want to paint over a bitmap that is larger than my screen and then save it.
The image size (landscape) is 1080 x 1776 (Nexus 5) and the bitmap is 3264 x 2448.
The image is scaled correctly to fit the screen using my activity xml file:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:id="@+id/ivPicture"
android:src="@mipmap/ic_launcher"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
If I draw a dot in the center of the screen, the dot I drew is not under my finger, but it is a pixel left / up.
Drawing a rectangle (from edge to edge of the screen) results in a scaled rectangle, such as an attached image:
There seems to be some kind of aspect ratio problem, but I can't figure out if this is the relevant part of the code.
public void loadImage(Uri imageFileUri) {
/*
ivPicture = (ImageView) findViewById(R.id.ivPicture);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
displayWidth = size.x;
displayHeight = size.y;
screen size: 1794 x 1080
bitmap size: 3264 x 2448
* */
try {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
ContentResolver cr = getContentResolver();
InputStream is = cr.openInputStream(imageFileUri);
bmp = BitmapFactory.decodeStream(is, null, bmpFactoryOptions);
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Log.d(TAG, "screen size: " + displayWidth + " x " + displayHeight);
Log.d(TAG, "bitmap size: " + bmp.getWidth() + " x " + bmp.getHeight());
canvas = new Canvas(alteredBitmap);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(15);
matrix = new Matrix();
canvas.drawBitmap(bmp, matrix, paint);
ivPicture.setImageBitmap(alteredBitmap);
ivPicture.setOnTouchListener(this);
} catch (Exception e) {
Log.v("ERROR", e.toString());
}
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downx = event.getX();
downy = event.getY();
break;
case MotionEvent.ACTION_MOVE:
upx = event.getX();
upy = event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
ivPicture.invalidate();
downx = upx;
downy = upy;
break;
case MotionEvent.ACTION_UP:
upx = event.getX();
upy = event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
ivPicture.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}
I'm guessing I'm running into a math problem, how to draw exactly from the image?
+3
source to share
No one has answered this question yet
Check out similar questions:
eleven