简体   繁体   English

ImageView旋转而不重新创建位图

[英]ImageView rotate without recreating the bitmap

I want to rotate an ImageView image. 我想旋转ImageView图像。 Here is my code: 这是我的代码:

Matrix mat = new Matrix();
mat.preRotate(90, ivImage.getWidth()/2, ivImage.getHeight()/2);
ivImage.setScaleType(ScaleType.MATRIX);
ivImage.setImageMatrix(mat);

but when I click on the rotate button, not only the image rotates, but it is scaled too, because the bitmap is larger then the ImageView . 但是当我点击旋转按钮时,不仅图像旋转,而且它也被缩放,因为位图比ImageView

I know I can use 我知道我可以使用

b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), mat, true);
ivImage.setImageBitmap(b);

but this is noticeably slower, and has some lag. 但这明显变慢了,并且有一些滞后。

So my question is: how to rotate the image without being scaled and without recreating the bitmap? 所以我的问题是:如何旋转图像而不进行缩放而不重新创建位图?

Here is the code that I have used for rotation of image to 90 degrees. 这是我用于将图像旋转到90度的代码。

         if(oldAngle == 0){
              newAngle = oldAngle + 90;
            }
            else if(oldAngle == 90)
                newAngle = oldAngle - 90;
              LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
              int centerX = layoutParams.leftMargin + (view.getWidth()/2);
              int centerY = layoutParams.topMargin + (view.getHeight()/2);
              RotateAnimation animation = new RotateAnimation(oldAngle, newAngle, centerX, centerY);
              animation.setDuration(0);
              animation.setRepeatCount(2);
              animation.setFillAfter(true);
              view.startAnimation(animation);

              oldAngle = newAngle;

Hope it will help you... 希望它能帮到你......

How about using an XML animation? 如何使用XML动画?

<?xml version="1.0" encoding="utf-8"?>
    <rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:interpolator="@android:anim/linear_interpolator"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="1000"
    android:startOffset="0"
    android:repeatCount="infinite"
    />

And then use it with something like; 然后使用类似的东西;

 rotation = AnimationUtils.loadAnimation(this, R.anim.clockwise_rotation);
 buttonRefresh.startAnimation(rotation);

ps. PS。 This infinitely spins around. 这无限旋转。 So needs some adjustments. 所以需要一些调整。

use this 用这个

final Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        final Matrix mtx = new Matrix();
        mtx.postRotate(n);
        rotator = Bitmap.createBitmap(all, 0, 0,
                all.getWidth(), all.getHeight(), mtx,
                true);
        view.setImageBitmap(rotator);

this will rotate the image randomly. 这将随机旋转图像。 just change .nextInt(n) to .nextInt(AnyNumberYouLife) its fast :) 只需将.nextInt(n)更改为.nextInt(AnyNumberYouLife)快:)

I found that for my purpose it was the best solution to use Canvas instead of ImageView . 我发现,就我的目的而言,它是使用Canvas而不是ImageView的最佳解决方案。 And for the rotating of the image I chose to use another library that can be found here 为了旋转图像,我选择使用另一个可以在这里找到的库

here's a nice solution for putting a rotated drawable for an imageView: 这是一个很好的解决方案,为imageView放置一个旋转的drawable:

Drawable getRotateDrawable(final Bitmap b, final float angle) {
    final BitmapDrawable drawable = new BitmapDrawable(getResources(), b) {
        @Override
        public void draw(final Canvas canvas) {
            canvas.save();
            canvas.rotate(angle, b.getWidth() / 2, b.getHeight() / 2);
            super.draw(canvas);
            canvas.restore();
        }
    };
    return drawable;
}

usage: 用法:

Bitmap b=...
float angle=...
final Drawable rotatedDrawable = getRotateDrawable(b,angle);
root.setImageDrawable(rotatedDrawable);

another alternative: 另一种选择:

private Drawable getRotateDrawable(final Drawable d, final float angle) {
    final Drawable[] arD = { d };
    return new LayerDrawable(arD) {
        @Override
        public void draw(final Canvas canvas) {
            canvas.save();
            canvas.rotate(angle, d.getBounds().width() / 2, d.getBounds().height() / 2);
            super.draw(canvas);
            canvas.restore();
        }
    };
}

also, if you wish to rotate the bitmap, but afraid of OOM, you can use an NDK solution i've made here 另外,如果你想旋转位图,但是害怕OOM,你可以使用我在这里制作的NDK解决方案

I've found the easiest way to rotate an ImageView is to do this: 我发现旋转ImageView最简单的方法是这样做:

view.animate().rotationBy(degrees);

Very easy to implement and android handles all the rest 很容易实现和Android处理所有其余的

Hope this helps! 希望这可以帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM