gpt4 book ai didi

java - 选择图片后 Android 性能很糟糕

转载 作者:行者123 更新时间:2023-12-01 12:11:06 25 4
gpt4 key购买 nike

我有一个图像选取方法,它允许用户从他们的图库中选择图像。

一旦选择了图像,应用程序就会变得非常滞后,页面上有 3 个文本框,当您单击它们时,键盘大约需要 3 秒才能出现,并且会出现滞后阶段。

LogCat 警告:

12-03 19:03:35.536  10842-10842/.com. I/Choreographer﹕ Skipped 58 frames!  The 
application may be doing too much work on its main thread.

所以,我尝试将图像选择器放在一个新线程上,但是方法太多,无法单独封装。

像这样简单的任务,性能低下的常见问题/解决方案是什么?

代码:

    @Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_submit);

imageView = (ImageView) findViewById(R.id.submitImageButton);
button = (ImageView) findViewById(R.id.submitImageButton);

}

public void pickPhoto(View view) {

new Thread(new Runnable() {
public void run() {

// launch the photo picker
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
}).start();

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
try {
Bitmap bitmap = getPath(data.getData());
Log.i("Bitmap", "Bmp: " + data.getData());
imageView.setImageBitmap(bitmap);
}catch (Exception e){
Log.e("Error", "Error with setting the image. See stack trace.");
e.printStackTrace();
}
}
}

private Bitmap getPath(Uri uri) {


button.setEnabled(false);

String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

cursor.moveToFirst();
String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
// Convert file path into bitmap image using below line.



Bitmap bitmap = BitmapFactory.decodeFile(filePath);

filePathForUpload = filePath;

try {
ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
bitmap = rotateBitmap(bitmap, orientation);
}catch (Exception e){
Log.d("Error", "error with bitmap!");
e.printStackTrace();
}

return bitmap;
}


public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return bitmap;
}

Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return bmRotated;
}

最佳答案

您应该将位图创建放在单独的线程上(即 getPath(...) 和rotateBitmap(...),而不是您选择照片的 Intent (pickPhoto(View view))。

此外,为什么不直接在 ImageView 上设置位图并使用 View.setRotation(float x) 来旋转位图,而不是通过创建新位图来旋转位图?这样,您就不会在每次旋转图像时创建新的位图。

关于java - 选择图片后 Android 性能很糟糕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27279787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com