自定义ImageView加载位图时,位图旋转-90degree

时间:2014-07-28 12:02:12

标签: android imageview android-bitmap

我编写了一个自定义ImageView,它将从库中选择的图像添加为圆形ImageView。我的代码有问题,每当从设备相机以纵向拍摄照片时,选择加载为位图的照片时它会旋转-90度,但是从设备拍摄的照片正确显示。我无法找到问题所在。 这是代码。

public class DragImageView extends ImageView {

    private float mLastTouchX;
    private float mLastTouchY;

    private float mDeltaX;
    private float mDeltaY;
    private Bitmap bmpImg;
    Context mContext;
    float oldDist = 1f;
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int height = 200;
    int width = 240;
    int ZoomCounter = 20;
    int mode = NONE;

    public DragImageView(Context context, Bitmap bmpImg) {
        super(context);
        this.bmpImg = bmpImg;
        this.mContext = context;
        init();
        // TODO Auto-generated constructor stub
    }

    public DragImageView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        // TODO Auto-generated method stub

        Bitmap resized = Bitmap.createScaledBitmap(bmpImg, 180, 200, true);
        // Bitmap conv_bm = getCroppedBitmap(resized, 100);
        // Bitmap conv_bm = getRoundedCornerBitmap(resized);
        Bitmap conv_bm = getRoundedShape(resized);
        setImageBitmap(conv_bm);
        LayoutInflater layoutInflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(
                R.layout.popup_image_adjustment, null);
        final PopupWindow popupWindow = new PopupWindow(popupView,
                android.app.ActionBar.LayoutParams.WRAP_CONTENT,
                android.app.ActionBar.LayoutParams.WRAP_CONTENT);
        final Button img_delete = (Button) popupView
                .findViewById(R.id.btn_delete_img);
        final Button img_rotate = (Button) popupView
                .findViewById(R.id.btn_rotate_img);
        final SeekBar sb_zoom = (SeekBar) popupView
                .findViewById(R.id.seekBar_zoom);

        final Button btn_close_window = (Button) popupView
                .findViewById(R.id.btn_close);

        setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(final View imgView, MotionEvent event) {

                btn_close_window.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        popupWindow.dismiss();
                    }
                });

                sb_zoom.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub

                    }

                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub

                    }

                    public void onProgressChanged(SeekBar seekBar,
                            int progress, boolean fromUser) {
                        // TODO Auto-generated method stub

                        if (progress >= 100) {
                            final RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
                            getLayoutParams().height = progress;
                            getLayoutParams().width = progress + 40;
                            setLayoutParams(params);
                            invalidate();
                        }
                    }
                });
                img_delete.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        ViewGroup parentView = (ViewGroup) imgView.getParent();
                        parentView.removeView(imgView);
                        if (popupWindow.isShowing()) {
                            popupWindow.dismiss();
                        }
                    }
                });

                img_rotate.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        final RotateAnimation rotateAnim = new RotateAnimation(
                                0.0f, 90, RotateAnimation.RELATIVE_TO_SELF,
                                0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                        rotateAnim.setDuration(0);
                        rotateAnim.setFillAfter(true);
                        imgView.startAnimation(rotateAnim);
                    }
                });

                final int action = event.getAction();

                mLastTouchX = event.getRawX();
                mLastTouchY = event.getRawY();

                switch (action) {
                case MotionEvent.ACTION_DOWN: {
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) getLayoutParams();
                    mDeltaX = mLastTouchX - lParams.leftMargin;
                    mDeltaY = mLastTouchY - lParams.topMargin;
                    // popup.show();
                    if (popupWindow.isShowing()) {
                        popupWindow.dismiss();
                    }

                    break;
                }
                case MotionEvent.ACTION_MOVE: {

                    mLastTouchX = event.getRawX();
                    mLastTouchY = event.getRawY();

                    final RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
                    params.leftMargin = (int) (mLastTouchX - mDeltaX);
                    params.topMargin = (int) (mLastTouchY - mDeltaY);
                    setLayoutParams(params);
                }
                    break;
                case MotionEvent.ACTION_UP:
                    popupWindow.showAsDropDown(imgView, 10, 0);

                }
                invalidate();
                // popupWindow.dismiss();
                return true;
            }
        });
    }

    public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
        int targetWidth = 180;
        int targetHeight = 200;
        Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
                Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);
        Path path = new Path();
        path.addCircle(((float) targetWidth - 1) / 2,
                ((float) targetHeight - 1) / 2,
                (Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
                Path.Direction.CCW);

        canvas.clipPath(path);
        Bitmap sourceBitmap = scaleBitmapImage;
        canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
                sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
                targetHeight), null);
        return targetBitmap;
    }

    /**
     * Function for circle shaped bitmap
     * 
     * @param bitmap
     * @param pixels
     * @return
     */
    public Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
        Bitmap result = null;
        try {
            result = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);

            int color = 0xff424242;
            Paint paint = new Paint();
            Rect rect = new Rect(0, 0, 200, 200);

            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawCircle(50, 50, 50, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);

        } catch (NullPointerException e) {
        } catch (OutOfMemoryError o) {
        }
        return result;
    }

    /**
     * Function for rounded rectangle shape bitmaps
     * 
     * @param bitmap
     * @return
     */
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = 12;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }

}

1 个答案:

答案 0 :(得分:0)

  

尝试检查默认方向并使用设置方向   矩阵如下.....

    ExifInterface exif=null;
    try {
        exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION, 0);
        Log.d("EXIF", "Exif: " + orientation);
        Matrix matrix = new Matrix();
        if (orientation == 6) {
                    matrix.postRotate(90);
                    Log.d("EXIF", "Exif: " + orientation);
        } else if (orientation == 3) {
                   matrix.postRotate(180);
                    Log.d("EXIF", "Exif: " + orientation);
        } else if (orientation == 8) {
                    matrix.postRotate(270);
                    Log.d("EXIF", "Exif: " + orientation);
        }
        scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                        scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
                        true);
  } catch (IOException e) {
                e.printStackTrace();
  }