Android 图片旋转

拍照后的照片有时被系统旋转,纠正步骤如下:

1.先读取图片文件被旋转的角度:

/**
     * 通过ExifInterface类读取图片文件的被旋转角度
     * @param path : 图片文件的路径
     * @return 图片文件的被旋转角度
     */
    public static int readPicDegree(String path) {
        int degree = 0;

        // 读取图片文件信息的类ExifInterface
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(path);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (exif != null) {
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;

            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;

            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
            }
        }

        return degree;
    }

2.再将上述角度作为参数,传递给下面函数纠正:

    /**
     * 将图片纠正到正确方向
     * 
     * @param degree : 图片被系统旋转的角度
     * @param bitmap : 需纠正方向的图片
     * @return 纠向后的图片
     */
    public static Bitmap rotateBitmap(int degree, Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Android中的属性动画(Property Animation)来实现图片旋转动画。具体实现步骤如下: 1.在res/anim文件夹下创建rotate.xml文件,内容如下: ```xml <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:repeatCount="0" android:duration="1000" /> ``` 其中,fromDegrees和toDegrees分别表示旋转的起点角度和终点角度;pivotX和pivotY表示旋转中心点的位置;repeatCount表示动画重复次数,这里设置为0表示不重复;duration表示动画的持续时间,这里设置为1000毫秒。 2.在代码中找到要进行旋转动画的ImageView控件,使用如下代码进行动画设置: ```java ImageView imageView = findViewById(R.id.image_view); ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0, 360); rotationAnimator.setDuration(1000); rotationAnimator.setRepeatCount(ValueAnimator.INFINITE); rotationAnimator.setInterpolator(new LinearInterpolator()); rotationAnimator.start(); ``` 其中,R.id.image_view表示ImageView控件的ID;ObjectAnimator是属性动画中的旋转动画,ofFloat方法中的第一个参数表示要进行动画的控件,第二个参数表示动画类型(这里是旋转),第三个参数表示旋转起始角度,第四个参数表示旋转终止角度;setDuration方法表示动画持续时间;setRepeatCount方法设置动画重复次数,这里设置为无限循环;setInterpolator方法设置动画插值器,这里设置为线性插值器;start方法开始动画。 这样就可以实现图片旋转动画了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值