我使用canvas.drawBitmap,
截取android屏幕并在捕获的图像(水印)上绘制另一个图像 当我在SD卡上复制Watermark(另一张图片 - 例如visit.jpg)图像时,所有这些都给出了正确的结果,捕获的图像宽度为500像素,其中visit.jpg的宽度为339像素,File file = new File( Environment.getExternalStorageDirectory() + "/visit.jpg");
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
但是我不想将水印图像复制到SD卡中,我希望res文件夹中的图像应该在捕获的图像上绘制,所以我使用
Bitmap visit = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.visit);
并在画布上绘制并保存,它会被保存,但水印图像(visit.jpg)会拉伸,其大小会超过339像素。为什么?
我的代码是:
View v1 = findViewById(android.R.id.content).getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap screenshot = Bitmap.createBitmap(v1.getDrawingCache());
Bitmap cropimg = Bitmap.createBitmap(screenshot, 50, 0, screenshot.getWidth()-100, 592);
v1.setDrawingCacheEnabled(false);
Bitmap Rbitmap = Bitmap.createBitmap(cropimg).copy(Config.ARGB_8888, true);
Bitmap visit = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.visit); // image accessed from drawable
Canvas canvas = new Canvas(Rbitmap);
File file = new File( Environment.getExternalStorageDirectory() + "/visit.jpg"); // the image copied on sd card
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
canvas.drawBitmap(visit, 0, 570, null); // if i put myBitmap instead of visit i.e. canvas.drawBitmap(myBitmap, 0, 570, null); then visit.jpg image gets draw properly with 339pixel width without strtch
canvas.save();
答案 0 :(得分:1)
Android会根据您的屏幕密度自动缩放资源图像。默认的可绘制文件夹对应于mdpi,因此如果您有hdpi设备,则图像将按比例放大。您可以将图像放入/ res / raw /文件夹,然后将其打开,如:
Uri imageUri = Uri.parse("android.resource://your.app.package/raw/visit");
InputStream is = context.getContentResolver().openInputStream(imageUri);
Bitmap bmp = BitmapFactory.decodeStream(is, null, opts);