2011-05-16 17 views
6

He estado intentando hacer que un archivo de imagen gire en el acto y estoy luchando, cada tutorial que encuentro parece hacer esto de una manera diferente.Imagen de Android gire a través del archivo xml

¿Puede alguien señalar dónde me estoy equivocando aquí?

GamePlay.java

import android.app.Activity; 
import android.graphics.drawable.AnimationDrawable; 
import android.os.Bundle; 
import android.widget.ImageView; 

public class GamePlay extends Activity { 

/** Called when the activity is first created. */ 
@Override public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.gameplay); 

ImageView logo = (ImageView)findViewById(R.id.mainlogo); 
logo.setBackgroundResource(R.anim.rotate); 

AnimationDrawable frameAnimation = (AnimationDrawable) logo.getBackground(); 

frameAnimation.start(); 

} 
} 

rotate.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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/logo" /> 

gameplay.xml

<ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/mainlogo" 
    android:src="@drawable/logo">  
</ImageView> 

+0

rotación de imagen ¿tu dices? Lo cargaría como una textura en OpenGL y luego usaría los comandos de rotación Open GL normales para hacer lo que dijo su xml. También usaría Simple XML Framework para analizar el XML en primer lugar. –

+0

En rotate.xml que debería estar ubicado en 'res/anim' debe agregar algunos atributos como:' android: repeatCount = "infinite" 'and' android: duration = "1200" ' – McIntosh

+1

Creo que está girando, pero desde no tiene 'android: duration =" "' no dura lo suficiente como para verse. – Aiapaec

Respuesta

0

probar este código; funciona para mí:

ImageView img=(ImageView)findViewById(R.id.ImageView01); 
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.snoopy); 
// Getting width & height of the given image. 
int w = bmp.getWidth(); 
int h = bmp.getHeight(); 
// Setting post rotate to 90 
Matrix mtx = new Matrix(); 
mtx.postRotate(90); 
// Rotating Bitmap 
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true); 
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP); 

img.setImageDrawable(bmd); 

} 
+0

Hay algo mal con sus llaves ... En particular, le falta la apertura. –

1

Cambio rotate.xml a

<?xml version="1.0" encoding="UTF-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="1200" 
    android:fromDegrees="0" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:repeatCount="infinite" 
    android:toDegrees="360" /> 
</set> 

lugar en res/animal/carpeta

y probar este para iniciar la animación

ImageView logo = (ImageView)findViewById(R.id.mainlogo); 
Animation rotateAnimation = AnimationUtils.loadAnimation(context, 
      R.anim.rotate); 
logo.startAnimation(rotateAnimation); 
Cuestiones relacionadas