Java 一旦选定图片,Android的性能就很糟糕
我有一个图像拾取方法,允许用户从他们的图库中选择图像 一旦选择了图像,应用程序就会变得非常滞后,页面上有3个文本框,当你点击它们时,键盘会在大约3秒钟内出现,并且会在滞后阶段出现 LogCat警告:Java 一旦选定图片,Android的性能就很糟糕,java,android,performance,Java,Android,Performance,我有一个图像拾取方法,允许用户从他们的图库中选择图像 一旦选择了图像,应用程序就会变得非常滞后,页面上有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. 因此,我尝试
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.
因此,我尝试将图像选择器放在一个新线程上,但是有太多的方法需要单独封装
对于如此简单的任务,低性能的常见问题/解决方案是什么
代码:
您应该将位图创建放在单独的线程(即getPath(…)和rotateBitmap(…)上),而不是选择照片(pickPhoto(View))的意图
此外,与其通过创建新位图来旋转位图,为什么不在ImageView上设置位图并使用View.setRotation(float x)?这样,您就不会每次旋转图像时都创建新位图。pickPhoto中的线程是无意义的,您可以删除它(活动不会在后台线程中启动,您调用的方法只是发布一个启动它的请求)另一方面,
getPath
。你需要将内容解析器/光标的内容放到一个单独的线程上。就像@zapl所说的,在pickPhoto中使用它是没有任何意义的。太好了-我没有意识到它的线程太多。我会尝试一下
@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;
}