Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何连续旋转图像?_Android_Android Imageview_Android Animation - Fatal编程技术网

Android 如何连续旋转图像?

Android 如何连续旋转图像?,android,android-imageview,android-animation,Android,Android Imageview,Android Animation,我试图用一根线来旋转这个图像。我该怎么办 public class Start extends Activity { View base; Bitmap rotate, base1, rotate1; ImageView rotator; float angle, pivX, pivY; @Override protected void onCreate(Bundle savedI

我试图用一根线来旋转这个图像。我该怎么办

     public class Start extends Activity {
        View base;
            Bitmap rotate, base1, rotate1;
        ImageView rotator;
        float angle, pivX, pivY;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.start);
            base = findViewById(R.id.base);
            rotator = (ImageView) findViewById(R.id.rotator);
            pivX = (rotator.getLeft()) / 2;
            pivY = (rotator.getTop()) / 2;
            Thread thread = new Thread() {
        @Override
        public void run() {
            for (angle = 0; angle < 50; angle++) {
                Matrix matrix = new Matrix();
                rotator.setScaleType(ScaleType.MATRIX); // required
                matrix.postRotate((float) angle, pivX, pivY);
                rotator.setImageMatrix(matrix);
                if (angle == 40) {`enter code here`
                    angle = 0;
                    return;
                }
            }
        }
    };

    thread.start();

        }
    }
公共类开始扩展活动{
视图库;
位图旋转,base1,rotate1;
影像旋转器;
浮动角度,pivX,pivY;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
base=findviewbyd(R.id.base);
rotator=(ImageView)findViewById(R.id.rotator);
pivX=(rotator.getLeft())/2;
pivY=(rotator.getTop())/2;
线程线程=新线程(){
@凌驾
公开募捐{
用于(角度=0;角度<50;角度++){
矩阵=新矩阵();
rotator.setScaleType(ScaleType.MATRIX);//必需
矩阵旋转后((浮动)角度,pivX,pivY);
旋转器。设置图像矩阵(矩阵);
如果(角度==40){`请在此处输入代码`
角度=0;
返回;
}
}
}
};
thread.start();
}
}

使用此代码旋转按钮

btn_rotate = (Button)findViewById(R.id.btn_rotate);
rotation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
rotation.setFillAfter(true);
btn_rotate.startAnimation(rotation);
rotate.xml 将此文件放入res->anim->rotate.xml中

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

<rotate
    android:duration="500"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:startOffset="0"
    android:toDegrees="360" />
</set>

看看这个

您的Java类

package com.example.rotate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements AnimationListener {

    TextView txtMessage;
    Button btnStart;

    // Animation
    Animation animFadein;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtMessage = (TextView) findViewById(R.id.tv);
        btnStart = (Button) findViewById(R.id.btn);

        // load the animation
        animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.rotate);

        // set animation listener
        animFadein.setAnimationListener(this);

        // button click event
        btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                txtMessage.setVisibility(View.VISIBLE);

                // start the animation
                txtMessage.startAnimation(animFadein);
            }
        });

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // Take any action after completing the animation

        // check for fade in animation
        if (animation == animFadein) {
            Toast.makeText(getApplicationContext(), "Animation Stopped",
                    Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

}
您的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

还有一件事。。您需要在res文件夹中定义anim文件夹

现在将rotate.xml文件放在anim文件夹中

rotate.xml文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="600"
        android:repeatMode="restart"
        android:repeatCount="infinite"
        android:interpolator="@android:anim/cycle_interpolator"/>

</set>


就这样。你可以走了。

我知道可能很晚了,但以下是我如何在java代码上制作旋转动画:

RotateAnimation rotate = new RotateAnimation(
                0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f
        );

rotate.setDuration(900);
rotate.setRepeatCount(Animation.INFINITE);
itemImage.startAnimation(rotate);
在科特林: rotate_.xml

<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="600"
    android:fromDegrees="0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:toDegrees="360" />

您可以在Kotlin中创建扩展:

fun ImageView.rotate(){
val rotateAnimation = RotateAnimation(
    0f, 359f,
    Animation.RELATIVE_TO_SELF, 0.5f,
    Animation.RELATIVE_TO_SELF, 0.5f

)
rotateAnimation.duration = 300
rotateAnimation.setAnimationListener(object : Animation.AnimationListener {
    override fun onAnimationStart(animation: Animation?) {}
    override fun onAnimationRepeat(animation: Animation?) {}
    override fun onAnimationEnd(animation: Animation?) {}
})
this.startAnimation(rotateAnimation)

}

这对我在Kotlin使用Android API 29时起到了作用:

val imageViewAnimator = ObjectAnimator.ofFloat(imageView,View.TRANSLATION_Y, 30f)
    imageViewAnimator.repeatCount = Animation.INFINITE
    imageViewAnimator.repeatMode = ObjectAnimator.REVERSE
    imageViewAnimator.duration = 1500
    imageViewAnimator.start()

这段代码的结果是一个“缓慢反弹”的无限动画,但您可以使用属性根据自己的意愿对其进行修改。

添加了不工作的次数。厌倦了添加更多文本的消息,请尝试在视图中传递动画。试试我的帖子。只需复制粘贴代码和休耕指令,以便更好地理解。如果您希望仅使用java代码执行此操作,请让我知道我将发布它。如何停止此动画您可以发布它,仅使用java代码@RajanYou也可以在xml中设置fillAfter:
,但此动画每转一圈都会暂停。如何避免?@omidomi你可以通过改变android:duration的值来管理速度,值越高,速度越低,会发现插值器需要进入动画集才能有效。不是那么平滑!即使设置为1500持续时间。
val imageViewAnimator = ObjectAnimator.ofFloat(imageView,View.TRANSLATION_Y, 30f)
    imageViewAnimator.repeatCount = Animation.INFINITE
    imageViewAnimator.repeatMode = ObjectAnimator.REVERSE
    imageViewAnimator.duration = 1500
    imageViewAnimator.start()