简体   繁体   English

旋转后将Android ImageView转换为Bitmap

[英]Android ImageView to Bitmap after rotating

I want to create a bitmap from an imageView, but I first need to rotate my imageView before converting the bitmap. 我想从imageView创建位图,但是在转换位图之前,我首先需要旋转imageView。 I use the following code to convert the imageView to Bitmap. 我使用以下代码将imageView转换为Bitmap。

 BitmapDrawable drawable = (BitmapDrawable) imgPhoto.getDrawable();
 Bitmap photo = drawable.getBitmap();

The imageView implements a rotation gesture. imageView实现旋转手势。 I want to be able to rotate the imageView and then create a Bitmap using the rotated imageView. 我希望能够旋转imageView,然后使用旋转的imageView创建位图。 However, it seems my Bitmap is still getting the original imageView instead of the rotated position. 但是,看来我的位图仍在获取原始的imageView而不是旋转的位置。 The following is the complete code. 以下是完整的代码。 Thank you. 谢谢。

 public class MainActivity extends AppCompatActivity implements RotationGestureDetector.OnRotationGestureListener{

private ImageView imgPhoto, imgBackground, imgCombine;
private RotationGestureDetector mRotationDetector;
private Button btnCombine;
private float angle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRotationDetector = new RotationGestureDetector(this);

    imgPhoto = (ImageView) findViewById(R.id.imgPhoto);
    imgBackground = (ImageView) findViewById(R.id.imgBackground);
    collageImage = (ImageView) findViewById(R.id.imgCombine);

    btnCombine = (Button)findViewById(R.id.btnCombineImage);
    btnCombine.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BitmapDrawable drawable = (BitmapDrawable) imgPhoto.getDrawable();
            Bitmap photo = drawable.getBitmap();

        }
    });
}

@Override
public boolean onTouchEvent(MotionEvent event){
    mRotationDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}

@Override
public void OnRotation(RotationGestureDetector rotationDetector) {
    angle = rotationDetector.getAngle();
    Log.d("RotationGestureDetector", "Rotation: " + Float.toString(angle));
    imgPhoto.setRotation(imgPhoto.getRotation() + (-angle));
}

 }

Try using this code after the onClick. 尝试在onClick之后使用此代码。 It should provide a rotated bitmap with the angle specified. 它应提供具有指定角度的旋转位图。

Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotatedPhoto = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);

To rotate ImageView: 旋转ImageView:

Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivotX, pivotY);
imageView.setImageMatrix(matrix);

Source: https://stackoverflow.com/a/10104318/7639113 资料来源: https : //stackoverflow.com/a/10104318/7639113

Then you can create the bitmap by using DrawingCache, 然后,您可以使用DrawingCache创建位图,

imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = imageView.getDrawingCache();

The rotate effect is actually applied at the Canvas level. 旋转效果实际上是在“画布”级别上应用的。 So, what you can do is get the current Canvas augmentations and then draw your Bitmap into a new one with the changes. 因此,您可以做的是获取当前的Canvas增强,然后将更改后的位图绘制到新的位图中。

           Matrix transformMatrix =  imgPhoto.getImageMatrix()

            Bitmap original = photo;

            Bitmap adjusted = Bitmap.createBitmap(original.getWidth(),
                    original.getHeight(),
                    original.getConfig());
            Canvas canvas = new Canvas(adjusted);
            canvas.setMatrix(transformMatrix);
            canvas.drawBitmap(original, 0, 0, null);

            //at this point adjusted bitmap contains the rotated image

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

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