我想绘制一个大于我的屏幕的位图,然后保存它。
图像尺寸(横向)为1080 x 1776(Nexus 5),而位图图像为3264 x 2448。
使用我的活动xml文件正确缩放图像以适应屏幕:
<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" />
如果我在屏幕中心画一个点,绘制的点不在我的手指下,但它是一些像素左/上。
绘制一个矩形(从屏幕的边缘到边缘)会产生缩放的矩形,如图像附加:
似乎有一些比例问题,但我无法弄明白,这是代码的相关部分。
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;
}
我想我遇到了数学问题,如何在图像上精确绘制?