Java的Andr​​oid的 - 正在与图像缩放/裁剪编程缩放、在与、图像、Java

2023-09-04 03:38:38 作者:为你千千万

嗯,这件事折磨我要长几个星期,我设置了图像高标度到170像素227像素,即使我想,只要我做到这一点被WRAP_CONTENT。

Well, all this thing tortures me for long weeks, I set an Image 227 pixels high in scales it to 170 pixels even if I want it to be wrap_content whenever I do.

确定。在这里,我把我的图片这是1950年像素长(我把这里它的一部分,所以你可以把它理解应该是什么样子)。

Ok. Here I take My Image which is 1950 pixels long (I put here a part of it so you can understand how it should look like).

首先,我要缩放回227像素高,因为这是它的设计,它应该如何

First, I want to scale it back to 227 pixels high because that's how it was designed and how it should be

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ver_bottom_panel_tiled_long);
            int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        int newWidth = 200; //this should be parent's whdth later
        int newHeight = 227;

        // calculate the scale
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                          width, height, matrix, true); 


        BitmapDrawable dmpDrwbl=new BitmapDrawable(resizedBitmap);

    verbottompanelprayer.setBackgroundDrawable(dmpDrwbl);

所以...它不是一个裁剪后的图像在所有 - 不,这是pssed为200像素像素1950年$ P $。

so... it's not a cropped image at all - no, it's 1950 pixels pressed into 200 pixels.

不过,我想只是削减任何东西,除了这200像素或什么的宽度,我会设置 - 裁剪,而不是preSS所有这漫长的图像转换成200像素面积

But I want just cut anything besides this 200 pixels or whatever width I'll set - crop it and not press all this long image into 200 pixels area.

此外,BitmapDrawable(位图位图);和imageView.setBackgroundDrawable(绘制);在德precated - ?我怎样才能改变这种状况

Also, BitmapDrawable(Bitmap bitmap); and imageView.setBackgroundDrawable(drawable); are deprecated - how can I change that?

推荐答案

据我看到的,你创建新的大小(200x227)的位图,所以我不知道你所期望的。你甚至写在你缩放意见和种植无字...

according to what i see, you create a bitmap of the new size (200x227) , so i'm not sure what you expected. you've even written in the comments that you scale and no word on cropping...

你可以做的是:

如果空气污染指数是至少10个(姜饼),你可以使用的 Bitma pregionDe codeR ,使用德codeRegion :

如果该API是太旧了,需要去code大位图,然后裁剪成一个新的位图,使用 Bitmap.createBitmap

if the API is too old, you need to decode the large bitmap, and then crop it into a new bitmap, using Bitmap.createBitmap

是这样的:

final Rect rect =...
if (VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD_MR1)
  {
  BitmapRegionDecoder decoder=BitmapRegionDecoder.newInstance(imageFilePath, true);
  croppedBitmap= decoder.decodeRegion(rect, null);
  decoder.recycle();
  }
else 
  {
  Bitmap bitmapOriginal=BitmapFactory.decodeFile(imageFilePath, null);
  croppedBitmap=Bitmap.createBitmap(bitmapOriginal,rect.left,rect.top,rect.width(),rect.height());
  }