在不在中间的位置旋转图像视图
Rotate imageview in place that isn't in the middle
我试图在 imageView 不在中间的位置旋转它。我正在使用 RotateAnimation,这是我的代码:
int x = imageView.getLeft();
int y = imageView.getTop();
Animation rotate = new RotateAnimation((float) 0, (float) 359, (float) x, (float) y);
imageView.setAnimation(rotate);
当我执行此操作时,imageView 不会原地旋转,而是围绕屏幕外的轴心点旋转。但是,当我在调试模式下 运行 时,x 和 y 等于 300 和 352,这似乎是正确的。有人可以解释为什么 imageView 围绕其他轴心点而不是我为其设置的轴心点旋转吗?
在 anim 文件夹中创建 rotate.xml
文件并使用以下代码
<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1600"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="358" />
并在 Activity
myImage.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
R.anim.rotate));
试试这个
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;
}
此 returns 一个包含实际宽度和高度的选项对象。从 Activity 你可以像这样使用它:
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 不在中间的位置旋转它。我正在使用 RotateAnimation,这是我的代码:
int x = imageView.getLeft();
int y = imageView.getTop();
Animation rotate = new RotateAnimation((float) 0, (float) 359, (float) x, (float) y);
imageView.setAnimation(rotate);
当我执行此操作时,imageView 不会原地旋转,而是围绕屏幕外的轴心点旋转。但是,当我在调试模式下 运行 时,x 和 y 等于 300 和 352,这似乎是正确的。有人可以解释为什么 imageView 围绕其他轴心点而不是我为其设置的轴心点旋转吗?
在 anim 文件夹中创建 rotate.xml
文件并使用以下代码
<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1600"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="358" />
并在 Activity
myImage.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
R.anim.rotate));
试试这个
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;
}
此 returns 一个包含实际宽度和高度的选项对象。从 Activity 你可以像这样使用它:
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);