4

我正在尝试通过单击自身将图像按钮旋转到 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 开发和益智查找游戏....

4

2 回答 2

7

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
    }
});
于 2012-11-06T10:31:23.587 回答
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);
    }
}

您将不得不更改一些参数。希望能帮助到你

于 2012-11-06T09:55:16.890 回答