一旦选择了图片,Android的性能非常糟糕

时间:2014-12-03 19:14:05

标签: 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.

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

如此简单的任务对于低性能有哪些常见问题/解决方案?

代码:

    @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;
    }

1 个答案:

答案 0 :(得分:1)

你应该把你的Bitmap创建放在一个单独的线程上(即getPath(...)和rotateBitmap(...),而不是你的Intent来选择一张照片(pickPhoto(View view)。

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