安卓:一个角度旋转图像的ImageView图像、角度、ImageView

2023-09-11 10:49:34 作者:放荡的灵魂.Χ

我使用下面的code。通过一个角度旋转的ImageView一个图像。有没有更简单的,更简单的方法可用。

  ImageView的IV =(ImageView的)findViewById(imageviewid);
TextView的电视=(TextView中)findViewById(txtViewsid);
矩阵垫=新的Matrix();
位图BMAP = BitmapFactory.de codeResource(getResources(),imageid);
mat.postRotate(的Integer.parseInt(度)); ===>角度被旋转
位图bMapRotate = Bitmap.createBitmap(BMAP,0,0,bMap.getWidth(),bMap.getHeight(),垫,真正的);
iv.setImageBitmap(bMapRotate);
 

解决方案

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

 进口android.graphics.Matrix;
进口android.widget.ImageView;
 

code:(假设的ImageView 角度 pivotX &安培; pivotY 已定义)

 矩阵矩阵=新的Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX); //需要
matrix.postRotate((浮动)的角度,pivotX,pivotY);
imageView.setImageMatrix(矩阵);
 
要使一个等边三角形绕它的中心旋转一个角度后与自身重合 则旋转的最小角度是

此方法不需要创建一个新的位图每次。

  

请注意:要旋转的ImageView 上的 ontouch 的在运行时可以   设置的 onTouchListener 的关于的ImageView &放大器;通过将最后两个旋转   线;在上述code(即 postRotate 的矩阵放大器设置上的的ImageView 的)   在你触摸监听器部分的 ACTION_MOVE 的一部分。

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);

解决方案

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

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

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.

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.