Android:按角度旋转图像视图中的图像 [英] Android: Rotate image in imageview by an angle

查看:44
本文介绍了Android:按角度旋转图像视图中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将 ImageView 中的图像旋转一个角度.有没有更简单、更简单的方法.

I am using the following code to rotate a image in ImageView by an angle. Is there any simpler and less complex method available.

ImageView iv = (ImageView)findViewById(imageviewid);
TextView tv = (TextView)findViewById(txtViewsid);
Matrix mat = new Matrix();
Bitmap bMap = BitmapFactory.decodeResource(getResources(),imageid);
mat.postRotate(Integer.parseInt(degree));===>angle to be rotated
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,bMap.getWidth(),bMap.getHeight(), mat, true);
iv.setImageBitmap(bMapRotate);

推荐答案

另一种旋转 ImageView 的简单方法:
更新:
所需的进口:

Another simple way to rotate an ImageView:
UPDATE:
Required imports:

import android.graphics.Matrix;
import android.widget.ImageView;

代码:(假设 imageViewanglepivotX & pivotY 已经定义)

Code: (Assuming imageView, angle, pivotX & pivotY are already defined)

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

这种方法不需要每次都创建一个新的位图.

This method does not require creating a new bitmap each time.

注意:要在运行时在 ontouch 上旋转 ImageView,您可以在 ImageView 上设置 onTouchListener &通过添加最后两个来旋转它上面代码中的行(即 postRotate 矩阵 & 将其设置在 imageView 上)触摸侦听器 ACTION_MOVE 部分中的部分.

NOTE: To rotate an ImageView on ontouch at runtime you can set onTouchListener on ImageView & rotate it by adding last two lines(i.e. postRotate matrix & set it on imageView) in above code section in your touch listener ACTION_MOVE part.

这篇关于Android:按角度旋转图像视图中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆