我想在android中旋转图像。我发现this useful post,它工作得很好,但它似乎在Android的旋转从左下角开始。我需要从中心点旋转我的图像。可能吗?代码相同是有帮助的。 谢谢。从imageview中心点旋转图像
回答
尝试:
Matrix matrix=new Matrix();
imageView.setScaleType(ScaleType.MATRIX);
matrix.postRotate((float) angle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);
imageView.setImageMatrix(matrix);
它从你给的链接相同的答案来。
这个怎么样(比goodm的回答略有不同):
public Bitmap rotateImage(int angle, Bitmap bitmapSrc) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(bitmapSrc, 0, 0,
bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}
Yahhhh。它的工作。谢谢。 – IndieBoy 2013-05-13 17:47:58
只是偶然发现了这个,因为我有类似的问题!如果你能帮助我,我会在哪里加载png图片到这个代码中? – 2017-06-02 17:40:35
的问题@ goodm的解决方案是,ImageView的可能尚未奠定了没有导致imageView.getDrawable()的getBounds()。 width()和.height()返回0.这就是为什么你仍然在0,0左右旋转的原因。解决这个问题的一个方法就是确保在使用类似这样的布局之后创建并应用矩阵:How to set fixed aspect ratio for a layout in Android
@ Voicu的解决方案没问题,但它需要您直接处理可能效率低下的位图。更好的做法可能是直接查询图像资源的大小,但实际上并未将其加载到内存中。我用一个实用的方法来做到这一点,它看起来像这样:
public static android.graphics.BitmapFactory.Options getSize(Context c, int resId){
android.graphics.BitmapFactory.Options o = new android.graphics.BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeResource(c.getResources(), resId, o);
return o;
}
这将返回一个Options对象将包含实际的宽度和高度。从活动中你可以使用这样的:
ImageView img = (ImageView)findViewById(R.id.yourImageViewId);
Options o = getSize(this, R.drawable.yourImage);
Matrix m = new Matrix();
m.postRotate(angle, o.outWidth/2, o.outHeight/2);
img.setScaleType(ScaleType.MATRIX);
img.setImageMatrix(m);
,这是为我工作:
RotationAnimatin anim= new RotationAnimation(0f,350f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//然后设置插,持续时间和RepeatCount
yourImageView.startAnimation(anim);
它是RotateAnimation而不是RotationAnimation – 2017-02-09 13:57:45
尝试此
gestureCenterPoint.x =(int)(imageView.getPaddingLeft()+ matrix瓦尔斯[Matrix.MTRANS_X]); gestureCenterPoint.x + =(origonalWidth/2)*(matrixVals [Matrix.MSKEW_X]); gestureCenterPoint.x + =(origonalWidth/2)*(matrixVals [Matrix.MSCALE_X]);
gestureCenterPoint.y =(int)(imageView.getPaddingTop()+ matrixVals [Matrix.MTRANS_Y]); gestureCenterPoint.y + =(origonalHeight/2)*(matrixVals [Matrix.MSKEW_Y]); gestureCenterPoint.y + =(origonalHeight/2)*(matrixVals [Matrix.MSCALE_Y]);
- 1. HTML5从中心点旋转图像
- 2. ImageView中的旋转图像
- 3. Android - 围绕中心点旋转图像?
- 4. 从ImageView旋转后获取图像
- 5. 从中心点旋转SVG
- 6. WP7/Xaml从其中心旋转图像
- 7. jquery - 使图像旋转围绕中心点旋转
- 8. 使用图像中心作为旋转轴旋转图像
- 9. 在android imageview中旋转图像
- 10. 旋转后在ImageView中适合图像
- 11. 旋转中心图像视图
- 12. 拉直图像从图像的中心旋转和缩放
- 13. 围绕它的中心旋转imageview
- 14. 如何ImageView在360度中心旋转?
- 15. Android ImageView检查图像旋转吗?
- 16. Swift:围绕中心点旋转图层
- 17. 中心使用旋转的图像GDI +
- 18. 旋转图像保留中心?
- 19. wpf围绕中心旋转图像
- 20. 中心和旋转与IE8(jQueryRotate)图像
- 21. 在其中心旋转图像
- 22. Java在中心旋转图像
- 23. 如何在图像中心旋转UIImage
- 24. jQuery旋转图像不在中心
- 25. 围绕中心旋转图像
- 26. openCV图像旋转中心C
- 27. 如何在中心点android上旋转一个imageview
- 28. 在画布中围绕其中心点旋转图像
- 29. 如何使用Kineticjs在中心点周围旋转图像?
- 30. BlackBerry drawTexturedPath旋转将锚点移至图像的中心
是的,我看到这个,但它不会从中心点旋转。我应该添加什么东西?感谢您的快速回复。 – IndieBoy 2013-05-13 17:06:36
@JASX发布了一个解释,如果你好奇... – newbyca 2013-10-15 07:26:59