2015-01-04 3 views
0

안녕하세요 저는 입니다. Android 프로그래밍와 임의의 각도로 이미지를 회전하는 법을 알고 싶습니다. "Spin the Bottle"게임에이 기능이 필요합니다. 지금까지 내 코드는 다음과 같습니다.Android에서 이미지를 임의로 회전하는 방법은 무엇입니까?

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

     } 

    }); 

} 

} 

이제 XML로 이미지를 회전 할 수 있지만 자바 코드로 회전 할 수 있습니까? 여기 ANIM 폴더에있는 XML 코드 :

<?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입니다 :

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

답변

1

쉽게 Java 코드뿐만 아니라 XML에서 애니메이션을 구성 할 수 있습니다.

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

생각, 댓글이 필요하지 않습니다 것을 : 당신의 사건을 위해이 같은 somehting을 시도, public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)

그래서 클래스 RotateAnimation와 '생성자를 사용할 수 있습니다.

+0

고마워요. 나는 지금 시험해 볼 것이다 :) – jelic98

+0

이것은 작동하지 않는다. 그냥 원래 위치로 돌아갑니다. 무작위로 회전하지 않습니다. –

관련 문제