android - ImageView 使文本输入滞后
问题描述
我有一个系统,我让用户从设备库中选择图像,并在检查是否需要旋转图片后将其放入 ImageView 中,问题是当图像放入 imageview 时,Edittext 滞后并写入键盘输入后 2/3 秒的东西
这是我在用户图片之后执行的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE&& resultCode == RESULT_OK) //if the pick of an image is successful
{
imageuri = data.getData();
try{
ImageView img = (ImageView) findViewById(R.id.imageView2);
img.setClickable(true);
Bitmap bitmap = handleSamplingAndRotationBitmap(getApplicationContext(), imageuri, img.getWidth(), img.getHeight()); //extract the image and check if it needs to be rotated
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
img.setImageBitmap(bitmap);
}
catch(IOException e){
e.printStackTrace();
}
}
}
以下是检查图像是否需要旋转的函数:
public static Bitmap handleSamplingAndRotationBitmap(Context context, Uri selectedImage, int w, int h)
throws IOException {
int MAX_HEIGHT =h;
int MAX_WIDTH = w;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream imageStream = context.getContentResolver().openInputStream(selectedImage);
BitmapFactory.decodeStream(imageStream, null, options);
imageStream.close();
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, MAX_WIDTH, MAX_HEIGHT);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
imageStream = context.getContentResolver().openInputStream(selectedImage);
Bitmap img = BitmapFactory.decodeStream(imageStream, null, options);
img = rotateImageIfRequired(context, img, selectedImage);
return img;
}
private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee a final image
// with both dimensions larger than or equal to the requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
// This offers some additional logic in case the image has a strange
// aspect ratio. For example, a panorama may have a much larger
// width than height. In these cases the total pixels might still
// end up being too large to fit comfortably in memory, so we should
// be more aggressive with sample down the image (=larger inSampleSize).
final float totalPixels = width * height;
// Anything more than 2x the requested pixels we'll sample down further
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
}
return inSampleSize;
}
private static Bitmap rotateImageIfRequired(Context context, Bitmap img, Uri selectedImage) throws IOException {
InputStream input = context.getContentResolver().openInputStream(selectedImage);
ExifInterface ei;
if (Build.VERSION.SDK_INT > 23)
ei = new ExifInterface(input);
else
ei = new ExifInterface(selectedImage.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return RotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return RotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return RotateImage(img, 270);
default:
return img;
}
}
protected static Bitmap RotateImage(Bitmap img, float degree)
{
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
img.recycle();
return rotatedImg;
}
我尝试降低很多分辨率,但没有滞后,它需要设置一个如此低的分辨率,基本上是一个大像素
解决方案
好的,我设法找到了使用这个专门为大型位图制作的库的工作,现在我没有滞后