Rotate image from center point in imageview

一世执手 提交于 2020-01-04 15:15:36

问题


I want to rotate an image in android. I found this useful post and it works great but it seems rotation in android starts from bottom left corner. I need to rotate my image from center point. Is it possible? Code for the same is helpful. Thanks.


回答1:


The problem with @goodm's solution is that imageView might not have been layed out yet which causes imageView.getDrawable().getBounds().width() and .height() to return 0. That's why you're still rotating around 0,0. One way around this is to ensure you create and apply your matrix after layout using something like this: How to set fixed aspect ratio for a layout in Android

@Voicu's solution is ok, but it requires you to work directly with bitmaps which could be inefficient. Better might be to query the image resource directly for it's size, but not actually load it into memory. I use a utility method to do this, it looks like this:

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

This returns an Options object that will contain the actual width and height. From an Activity you could use it like this:

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



回答2:


How about this (slightly different than goodm's answer):

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



回答3:


this is working for me:

RotateAnimation anim= new RotateAnimation(0f,350f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

// then set interpolator, duration, and RepeatCount

yourImageView.startAnimation(anim);




回答4:


Try:

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

It's coming from the same answer what you gave a link.




回答5:


I have a library that does this. You can find it here: https://bitbucket.org/warwick/hg_dial_v2




回答6:


The answer by goodm given above works, just ensure you are getting the bounds etc. in the onWindowFocusChanged() activity life-cycle callback instead of onCreate().

Because we need to ensure view has been rendered for the getBounds() functions to work correctly, else we get 0.0 as the values of these methods when calling them in onCreate(). onWindowFocusChanged() is the place where one can be sure.



来源:https://stackoverflow.com/questions/16527209/rotate-image-from-center-point-in-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!