2010-12-02 63 views
2

我想要一個圖像旋轉無限期........這意味着我想循環它。這是我的嘗試,但不幸的是它不起作用。有什麼建議麼?Android Image旋轉

package com.android.test; 

import android.app.Activity; 
import android.graphics.Bitmap; 
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; 
    } 
    } 
} 

回答

2

你不能在主線程中循環。這會立即讓您的應用程序無響應。考慮使用RotateAnimation - 請參閱文檔鏈接。

+0

好................我不知道那是什麼意思.............但是可以請你直接給我一個教程或者什麼教導如何實現這一目標?或者你可以修復我的代碼並將其發佈到此處。這將有所幫助 – efa 2010-12-03 00:14:33

0

這裏是適當的方式來旋轉圖像,請參閱這是將隨機旋轉圖像..因爲我使用隨機發生器「像一個循環」旋轉圖像0度到1000度之間。

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是你想旋轉的位圖。 爲了讓您旋轉圖像,您通常只需將mtx.postRotate(n); 更改爲mtx.postRotate(90);添加到按鈕或菜單設置。隨你便。 歡呼聲。 編輯:P.S contentView引用您的ImageView。

0

您可以使用下面的代碼來旋轉imageview無限期的時間。

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

如果您的總持續時間需要修復,請說「total_duration」。還定義了一個週期的「持續時間」。您可以使用上述代碼與自定義計數作爲

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

希望它有幫助。