我通过传递 customerid 作为参数来调用 Web 服务。该网络服务返回一些图像,我将在 Android 应用程序上使用这些图像。现在的问题是我无法使用该图像,即来自网络服务器的图像,因为它太大了。我需要裁剪它。如果有人有可以提供帮助的代码 fragment ,我真的会很感激。也欢迎任何替代方法。
try
{
byte[] imageis = getImage();
Bitmap bmp=BitmapFactory.decodeByteArray(imageis,0,imageis.length);
if (bmp!=null)
{
ImageView imgview= (ImageView)findViewById(R.id.vimg);
imgview.setImageBitmap(bmp);
}
}
getImage() 方法很好并且有效。所以不用担心。任何人都知道我在此之后需要做什么??
最佳答案
尝试:
.....
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length, options);
int width_tmp = options.outWidth, height_tmp = options.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < width || height_tmp / 2 < height)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
options = new BitmapFactory.Options();
options.inSampleSize = scale;
options.inPurgeable = true;
options.inInputShareable = true;
Bitmap result = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length, options);
....
其中宽度和高度 - ImageView 的大小
关于android - Android 中的图像裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9479295/