从ImageView的中心点旋转图像 [英] Rotate image from center point in imageview

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

问题描述

我要旋转图像。我发现这个有用的帖子和它的工作很好,但它似乎在旋转Android的从左下角开始。我需要从中心点旋转我的形象。可能吗?如果这是怎么了?
坦克。

I want to rotate an image. I found this useful post and it's work great but it seems rotation in android start from bottom left corner. I need to rotate my image from center point. Is it possible? If it is how? Tanks.

推荐答案

的问题@ goodm的解决方案是,ImageView的可能尚未奠定了没有导致imageView.getDrawable()的getBounds()宽()和。高度()返回0,这就是为什么你还在绕着0,0。解决此问题的方法是确保您创建和使用这样的布局后,申请你的矩阵:<一href=\"http://stackoverflow.com/questions/12311346/how-to-set-fixed-aspect-ratio-for-a-layout-in-android/12311431#12311431\">How可为在Android的布局固定纵横比

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

这将返回一个Options对象将包含实际的宽度和高度。从活动中你可以使用这样的:

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

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

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