Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将图像向左或向右旋转90度_Android_Imageview_Android Animation - Fatal编程技术网

Android 将图像向左或向右旋转90度

Android 将图像向左或向右旋转90度,android,imageview,android-animation,Android,Imageview,Android Animation,所以我有点紧张。我正在制作一个游戏,我有一个正方形,每边有4种不同的颜色。似乎无法使正方形在右屏幕触摸时旋转90度,在左屏幕点击时旋转-90度。我可以让它在敲击时旋转,但不能停留在这个位置。希望它尽可能平滑 活动: public class GameActivity extends Activity implements OnTouchListener { @Override protected void onCreate(Bundle savedInstanceState)

所以我有点紧张。我正在制作一个游戏,我有一个正方形,每边有4种不同的颜色。似乎无法使正方形在右屏幕触摸时旋转90度,在左屏幕点击时旋转-90度。我可以让它在敲击时旋转,但不能停留在这个位置。希望它尽可能平滑

活动:

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

}
布局:

<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>
但它不会有动画(基本上是线性插值)的影响。我想知道如何才能做到这一点?

只要使用

您的
OnTouchListener
可能是这样的(尽管我不确定您为什么决定使用
OnTouchListener
而不是例如
OnClickListener
):


或者您可以使用
rotationBy
而不是
rotation
,如果这更适合您的需要。

我这样做是因为我希望能够左右触摸以旋转正方形的任一侧。我不想要纽扣。或者onclick会在ontouch上完成工作吗?
<?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"/>
switch (v.getId()) {

            case R.id.leftClick:
                square.setRotation(square.getRotation() - 90);

                break;

            case R.id.rightClick:
                square.setRotation(square.getRotation() + 90);

                break;

            default:

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