Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am trying to rotate imagebutton to 360 by clicking it but it can't rotate it?

I am trying to rotate an imagebutton to 360 by clicking itself but it doesn't work

I am using xml file like this..

<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" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="83dp"
        android:layout_marginTop="103dp"
        android:clickable="true"
        android:src="@drawable/cute" />
</RelativeLayout>

and java code like this...

package com.example.testapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.RotateAnimation;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnClickListener {


    ImageButton img;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageButton) findViewById(R.id.imageView1);



    }
    public void onClick(View v) {
        // TODO Auto-generated method stub
        RotateAnimation ra =new RotateAnimation(0, 360);
        ra.setFillAfter(true);
        ra.setDuration(0);

        img.startAnimation(ra);

    }

}

my aim of application is using 9 img btn develop and puzzle find game....

like image 723
CrazyMind Avatar asked Dec 21 '22 13:12

CrazyMind


2 Answers

Your code works just fine. the problem is : you set duration to 0. You can't see it happen because if you rotate 360 degrees its ending position and starting position will be identical . so set the duration 1000(a second) or more to see the animation actualy happening.

just try:

ra.setDuration(1000);

And you also did not set the onClick listener of img.

just try:

img .setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        //animation here
    }
});
like image 185
Ercan Avatar answered May 22 '23 09:05

Ercan


This was the code which I used to rotate in one small app.

public class MainScreen extends Activity{
    ImageView ivDH = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen);
        ivDH = (ImageView) findViewById(R.id.dollhouse);

        ivDH.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub

                startAnimation(ivDH);
                return false;
            }
        }); 
    }
    public void startAnimation(ImageView ivDH)
    {
        System.out.println("Inside startAnimation()");
        Animation scaleAnim = new ScaleAnimation(0, 2, 0, 2);
        scaleAnim.setDuration(5000);
        scaleAnim.setRepeatCount(1);
        scaleAnim.setInterpolator(new AccelerateInterpolator());
        scaleAnim.setRepeatMode(Animation.REVERSE);

        Animation rotateAnim = new RotateAnimation(0, 360, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF);
        rotateAnim.setDuration(5000);
        rotateAnim.setRepeatCount(1);
        rotateAnim.setInterpolator(new AccelerateInterpolator());
        rotateAnim.setRepeatMode(Animation.REVERSE);

        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(scaleAnim);
        animationSet.addAnimation(rotateAnim);

        ivDH.startAnimation(animationSet);
    }
}

You will have to change some parameters. Hope it helps

like image 44
MysticMagicϡ Avatar answered May 22 '23 08:05

MysticMagicϡ