如何在Android中随机旋转图片? [英] How to randomly rotate an image in Android?

查看:147
本文介绍了如何在Android中随机旋转图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android编程的新手,我想知道如何以随机角度旋转图像。我的旋转瓶子游戏需要这个。到目前为止,这是我的代码:

I'm new at Android Programing and I want to know how to rotate an image by a random angle. I need this for my "Spin the Bottle" game. Here is my code so far:

package example.com.bottlegame;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.RotateDrawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import java.lang.Object;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import java.util.Random;


public class MainActivity extends ActionBarActivity {

ImageView spin,bottle;

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

    spin = (ImageView) findViewById(R.id.ivSpin);
    bottle = (ImageView) findViewById(R.id.ivBottle);

    final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.roatate);

    spin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            bottle.startAnimation(animRotate);

        }

    });

}

}

现在我m使用XML旋转图像,但是您能帮我仅用Java代码旋转图像吗?以下是anim文件夹中的XML代码:

Now I'm rotating an image with XML, but can you help me to rotate it with just Java code. Here is the XML code in anim folder:

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:drawable="@drawable/bottle"
    android:duration="1000"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:startOffset="0"
/>

</set>` 

这是activity_main.xml :

And here is the activity_main.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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/bg_background">


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="100sp"
    android:layout_centerHorizontal="true"
    android:id="@+id/ivSpin"
    android:src="@drawable/spin_up"
    android:layout_marginTop="400sp"
    android:contentDescription="@string/spin"
/>

<ImageView
    android:layout_width="65sp"
    android:layout_height="200sp"
    android:id="@+id/ivBottle"
    android:src="@drawable/bottle"
    android:layout_marginTop="80dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/bottle"
/>


</RelativeLayout>


推荐答案

您也可以轻松地用Java代码配置动画就像在xml中一样。对于您的情况,您可以使用RotateAnimation类及其构造函数 public RotateAnimation(float fromDegrees,float toDegrees,float axisX,float axisY)

You can easily configure your animation in Java code as well as in xml. For your case you could use class RotateAnimation and its' constructor public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)

因此,尝试这样的操作:

So, try somehting like this:

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

    spin = (ImageView) findViewById(R.id.ivSpin);
    bottle = (ImageView) findViewById(R.id.ivBottle);

    float toDegrees = new Random().nextFloat() * Integer.MAX_VALUE % 360;
    final Animation animRotate = new RotateAnimation(0, toDegrees, 50, 50);
    animRotate.setDuration(1000);
    animRotate.setRepeatCount(1);
    animRotate.setRepeatMode(Animation.REVERSE);

    spin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bottle.startAnimation(animRotate);
        }
    });
}

我认为不需要评论。

这篇关于如何在Android中随机旋转图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆