web-dev-qa-db-ja.com

画像を連続的に回転させる方法は?

糸を使ってこの画像を回転させようとしています。私は何をすべきか?

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

        }
    }
11
user2152295

ボタンを回転させるためにこのコードを使用します

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>
28
Rajan

おそらく遅いことはわかっていますが、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);
17
MetaSnarf

これをチェックしてください..

あなたの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>

そしてもう1つ..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>

それでおしまい。準備できた。

10
AndroidHacker

Kotlinの場合:

 ivBall.setOnClickListener(View.OnClickListener {

            //Animate using XML
            // val rotateAnimation = AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely)

            //OR using Code
            val rotateAnimation = RotateAnimation(
                    0f, 359f,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f

            )
            rotateAnimation.duration = 300
            rotateAnimation.repeatCount = 2

            //Either way you can add Listener like this
            rotateAnimation.setAnimationListener(object : Animation.AnimationListener {

                override fun onAnimationStart(animation: Animation?) {
                }

                override fun onAnimationRepeat(animation: Animation?) {
                }

                override fun onAnimationEnd(animation: Animation?) {

                    val Rand = Random()
                    val ballHit = Rand.nextInt(50) + 1
                    Toast.makeText(context, "ballHit : " + ballHit, Toast.LENGTH_SHORT).show()
                }
            })

            ivBall.startAnimation(rotateAnimation)
        })

rotate_indefinitely.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" />
0
Hitesh Sahu

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)

}

0
Jéwôm'