向左或向右旋转图像90度

时间:2015-09-18 05:42:51

标签: android imageview android-animation

所以我有点孤岛危机。我正在制作一个游戏,每侧有4种不同颜色的正方形。似乎无法使右侧屏幕上的方形旋转90度和左侧屏幕上的-90度。我可以让它旋转,但不能留在该位置。喜欢尽可能顺利。

活动:

m.redraw()

布局:

public class GameActivity extends Activity implements OnTouchListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        // Reference to the left and right linear screens
        LinearLayout leftTap = (LinearLayout) findViewById(R.id.leftTap);
        leftTap.setOnTouchListener(this);
        LinearLayout rightTap = (LinearLayout) findViewById(R.id.rightTap);
        rightTap.setOnTouchListener(this);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {

        // Square itself
        ImageView square = (ImageView) findViewById(R.id.square);

        // Left and Right Animations
        Animation leftAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_left_animation);
        Animation rightAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_right_animation);

        switch (v.getId()) {
            case R.id.leftTap:
                // Do left Rotate animation
                square.startAnimation(leftAnimation);

                break;

            case R.id.rightTap:
                // Do right Rotate animation
                square.startAnimation(rightAnimation);

                break;

            default:
                break;
        }
        return false;
    }

}

左击动画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"
    tools:context=".GameActivity"
    android:weightSum="1"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/leftTap"
        android:visibility="visible"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/square"></LinearLayout>


    <ImageView
        android:contentDescription="The Square"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/square"
        android:src="@drawable/photo"
        android:scaleType="fitCenter" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/rightTap"
        android:visibility="visible"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/square"></LinearLayout>


</RelativeLayout>

右击动画XML:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="500"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="-90"/>
</set>

我可以:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="500"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="90"/>

但它不会有动画(基本上是线性插值)的影响。我想知道怎么做?

1 个答案:

答案 0 :(得分:2)

只需使用ViewPropertyAnimator

您的OnTouchListener可能看起来像这样(虽然我不确定您为什么要决定使用OnTouchListener代替OnClickListener):

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        switch (v.getId()) {
            case R.id.leftTap:
                // Do left Rotate animation
                square.animate().rotation(-90).setInterpolator(new LinearInterpolator()).setDuration(500);
                break;

            case R.id.rightTap:
                // Do right Rotate animation
                square.animate().rotation(90).setInterpolator(new LinearInterpolator()).setDuration(500);
                break;

            default:
                break;
        }
    }
    return v.onTouchEvent(event);
}

如果更符合您的需求,您可以使用rotationBy代替rotation