android-如何剪切图像的某些部分并在imageview中显示

android-如何剪切图像的某些部分并在imageview中显示,android,imageview,scale,Android,Imageview,Scale,我只想在imageview中显示图像的一部分。请参见下图。 同样的例子也可以在google+应用程序中找到,你可以看到所有带有图片的帖子 任何链接,代码将是有益的。 谢谢使用此代码 int width = bitmapOrg.width(); int height = bitmapOrg.height(); int newWidth = 200; int newHeight = 200; // calculate the scale - in this case = 0.4f float s

我只想在imageview中显示图像的一部分。请参见下图。

同样的例子也可以在google+应用程序中找到,你可以看到所有带有图片的帖子

任何链接,代码将是有益的。 谢谢使用此代码

int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;

// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;

// createa 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); 

// make a Drawable from Bitmap to allow to set the BitMap 
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

ImageView imageView = new ImageView(this);

// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);

如果有人希望从底部剪切图像

// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, x, y, 
                  width, height, matrix, true); 
用此逻辑替换以下参数

y:(bitmapOrg.getHeight()-1)-(问题中红色框的高度)

高度:(问题中红色方框的高度)

这样可以避免异常,例如(对于x,您需要相应地修改)


IllegalArgumentException:x+宽度必须是如何将newWidth设置为等于设备宽度,以便根据设备屏幕宽度缩放图像?int newWidth=getWindowManager().getDefaultDisplay().getWidth();顺便说一句,有一个问题-它是拉伸图像我不想拉伸图像,我想剪切图像的一部分,如我所示的例子中的问题。。更常见的是,我想裁剪它:)这幅图像是在将newWidth设置为设备宽度之后拉伸还是之前拉伸?我需要首先使用宽度等于设备宽度的ur代码调整图像大小,然后使用ZuzooVn的代码裁剪图像,将其高度剪切为100px。我已经尝试过了,但我无法根据设备屏幕大小设置新图像宽度,请参阅deepika answerDisplay=((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()上的注释1;int screenWidth=display.getWidth();
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, x, y, 
                  width, height, matrix, true); 
IllegalArgumentException: x + width must be <= bitmap.width() in android

IllegalArgumentException: y + width must be <= bitmap.height() in android