简体   繁体   English

Android动画围绕其中心点旋转图像

[英]Android animation to rotate a image around its centre point

I am struggling with an android xml animation. 我正在努力使用android xml动画。 I want to rotate an image anti clockwise around its central point. 我想围绕其中心点逆时针旋转图像。

Here is my xml for the animation: 这是我的动画xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >

<rotate
    android:duration="1000"
    android:repeatCount="infinite"
    android:repeatMode="restart"
    android:fromDegrees="360"
    android:toDegrees="0"
    android:pivotX="0.5"
    android:pivotY="0.5"         
    android:interpolator="@android:anim/linear_interpolator"        
   />

No this current animation rotates the image around the point x=0 ; 没有这个当前动画围绕点x = 0旋转图像; y=0 . y = 0。

Answering on behalf of @Mahfa 代表@Mahfa回答

You have to set pivotX and pivotY to "50%" 你必须将pivotX和pivotY设置为“50%”

The important part is the percentage. 重要的部分是百分比。

I think that you need to use a Matrix object. 我认为你需要使用Matrix对象。 I did the same some time ago and I remember that I had the same issue than you: 前段时间我做了同样的事情,我记得我遇到的问题与你相同:

Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, config);
Canvas canvas = new Canvas(targetBitmap);
Matrix matrix = new Matrix();
matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
canvas.drawBitmap(source, matrix, new Paint());

Edit: use this function with a timer handler 编辑:使用此函数与计时器处理程序

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM