开发者

Android Image rotation

开发者 https://www.devze.com 2023-01-28 07:17 出处:网络
I want an image to rotate for an indefinite time........which means I want to loop it. This is my attempt but unfortunately it does not work. Any suggestions?

I want an image to rotate for an indefinite time........which means I want to loop it. This is my attempt but unfortunately it does not work. Any suggestions?

package com.android.test;

import android.app.Activity;
import android.graphics.Bitm开发者_如何学运维ap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;

public class imagerotate extends Activity {
 int x=1;
 int y=3;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         LinearLayout linearLayout = new LinearLayout(this);

         while (y==3) {
   Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

   int width = bitmap.getWidth();
   int height = bitmap.getHeight();

   Matrix matrix = new Matrix();
   matrix.postRotate(x);

   Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,height, matrix, true);
   BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);

   ImageView imageView = new ImageView(this);
   imageView.setImageDrawable(bmd);
   imageView.setScaleType(ScaleType.CENTER);

   linearLayout.addView(imageView, new LinearLayout.LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
   setContentView(linearLayout);

   x+=1;
   }
     }
 }


You can't loop in the main thread. This will immediately make your app unresponsive. Consider using a RotateAnimation - see the link for documentation.


You can use following code for rotating an imageview for INFINITE period of time.

Animation rotateAnim = new RotateAnimation(0, 360);
rotateAnim.setDuration(5000);
rotateAnim.setRepeatCount(Animation.INFINITE);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);
img.startAnimation(rotateAnim);

If your total duration needs to be fix, say "total_duration". And "duration" for one cycle is also defined. You can use the above code with custom count as

int count = total_duration/duration;
Animation rotateAnim = new RotateAnimation(0, 360);
rotateAnim.setDuration(duration);
rotateAnim.setRepeatCount(count);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);
img.startAnimation(rotateAnim);

Hope it helps.


well here is the proper way to rotate an image , please see this is will rotate the image randomly.. as i'm using random generator "like a loop" to rotate the image between 0 degree to 1000 degree.

    Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        Matrix mtx = new Matrix();
        mtx.postRotate(n);
        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
                bmp.getHeight(), mtx, true);
        contentView.setImageBitmap(bmp);  

while bmp is the Bitmap you wanna rotate. in order for you to rotate the image normally you just change the mtx.postRotate(n); to mtx.postRotate(90); add to the button or to menu settings. its up to you. cheers. edit : P.S contentView refers to your ImageView.

0

精彩评论

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