我试图通过点击它将图像按钮旋转到360但它不能旋转它?

时间:2012-11-06 09:48:59

标签: android rotation

我试图通过点击自己将图像按钮旋转到360,但它不起作用

我正在使用像这样的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" >

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

和这样的java代码......

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

    }

}

我的应用目标是使用9 img btn develop和puzzle find game ....

2 个答案:

答案 0 :(得分:7)

您的代码运行正常。问题是:你将持续时间设置为0.你无法看到它发生,因为如果你旋转360度,它的结束位置和起始位置将是相同的。所以设置持续时间1000(秒)或更长时间来看动画实际发生。

试试:

ra.setDuration(1000);

而且你也没有设置img的onClick监听器。

试试:

img .setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        //animation here
    }
});

答案 1 :(得分:4)

这是我在一个小应用程序中旋转的代码。

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

您必须更改一些参数。希望它有所帮助