开发者

Android image rotate via xml file

开发者 https://www.devze.com 2023-03-06 14:10 出处:网络
I\'ve been attempting to make a image file rotate on the spot an开发者_运维技巧d am struggling, every tutorial I find seems to do this in a different way.

I've been attempting to make a image file rotate on the spot an开发者_运维技巧d am struggling, every tutorial I find seems to do this in a different way.

Can someone point where I'm going wrong here.

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>


Change rotate.xml to

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

place it in res/anim/ folder

and try this to start animation

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


Try this code; it works for me:

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

精彩评论

暂无评论...
验证码 换一张
取 消