从imageview中心点旋转图像
我想在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]);
是的,我看到这个,但它不会从中心点旋转。我应该添加什么东西?感谢您的快速回复。 – IndieBoy 2013-05-13 17:06:36
@JASX发布了一个解释,如果你好奇... – newbyca 2013-10-15 07:26:59