2013-02-18 64 views
0

我要自動更改畫布圖像是否可以自動更改Canvas圖像Android?

意思是我希望那個畫布圖像應該被一個接一個地連續設置。

我已經寫了代碼,它只設置圖像一次。

但我不知道自動更改畫布圖像的代碼,以便它會建立像對象的影響在道路上運行..

那麼什麼是創建這類動畫的方式在畫布上?

請給我一些想法來創建這樣的2D動畫。

Thanx提前。

回答

1

我會做到這一點的方式如下:

  • 創建一個包含一個位圖,並在onDraw()此位圖繪製在畫布
  • 給這對一個自定義視圖自定義查看setter,給它一個新的位圖(可能作爲ressouce-id或類似的東西)
  • 創建一個新的線程(用UI處理程序),它改變了畫布每秒至少24times的資源,並呼籲customView.invalidate()通過handler.post

這個工作對我來說

編輯:代碼

活動:

package de.test.animation; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.LinearLayout; 

public class AnimationTestActivity extends Activity { 
Button btn; 
CustomView customView; 
LinearLayout layout; 

int[] imageIDs; 

private void init(){ //array with my ressouce-IDs 
    imageIDs = new int[]{ 
     R.drawable.pic1,  
     R.drawable.pic2,  
     R.drawable.pic3,  
     R.drawable.pic4,  
     R.drawable.pic5,  
     R.drawable.pic6,  
     R.drawable.pic7,  
     R.drawable.pic8,  
     R.drawable.pic9,  
     R.drawable.pic10, 
     R.drawable.pic11, 
     R.drawable.pic12, 
     R.drawable.pic13, 
     R.drawable.pic14, 
     R.drawable.pic15, 
     R.drawable.pic16, 
     R.drawable.pic17, 
     R.drawable.pic18, 
     R.drawable.pic19, 
     R.drawable.pic20, 
     R.drawable.pic21, 
     R.drawable.pic22, 
     R.drawable.pic23, 
     R.drawable.pic24  
    }; 
} 

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

    btn = (Button) findViewById(R.id.btnStart); 
    layout = (LinearLayout)findViewById(R.id.layout); 

    customView = new CustomView(this); 
    customView.setNewImage(imageIDs[0]); 
    layout.addView(customView); 

    btn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Thread t = new Thread(){ 
       private final int FPS = 24; //How many frames will be dran per second 
       private final int SLEEPTIME = 1000/FPS; //Time, the thread waits, before drawing the next picture 

       @Override 
       public void run() { 
        super.run(); 
        for(int i=0;i<imageIDs.length;i++){ 
         customView.setNewImage(imageIDs[i]); //set next picture 
         customView.repaint(); //draw the picture on the canvas 
         try { 
          sleep(SLEEPTIME); //wait, until the next picture can be drawn 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 
      }; 
      t.start(); 
     } 
    }); 
} 
} 

CustomView:

包de.test.animation;

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.view.View; 


public class CustomView extends View { 
private Bitmap image; //image to be drawn on this view 
private Context context; 

public CustomView(Context context) { //constructor 
    super(context); 
    this.context = context; 
} 

public void setNewImage(int r_id){ //method to set a new picture (via resouce-id) 
    image = BitmapFactory.decodeResource(context.getResources(), r_id); //decode the image from the resouces 
} 

public void repaint(){ //method to repaint this view 
    this.post(new Runnable(){ //posting via a new runnable (otherwhise you get a "calledByWrongThreadException" 
     @Override 
     public void run() { 
      invalidate(); //Thread initiates UI-Thread to update this view 
     } 
    }); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    canvas.drawBitmap(image, 0, 0, new Paint()); //draw the picture in the view 
} 


} 

我希望這可以幫助你。 然後祝你好運。

+0

你可以請求你的完整代碼與setter和線程? – zanky 2013-02-18 11:39:10

+0

OnDraw將如何被調用? – zanky 2013-02-18 11:54:43

+0

我編輯了我的答案並添加了代碼。祝你好運 – 2013-02-18 22:02:39

2

退房本教程幀的動畫:

http://www.youtube.com/watch?v=iTKtT-R98EE

更多信息,可以在下面的鏈接中找到:

Starting Frame-By-Frame Animation

以下是一步一步的過程:

在「幀動畫」中,您將重複交換幀,以使其顯示爲連續的t人眼,我們覺得它是動畫。幀被稱爲圖像。因此,爲了實現幀動畫,需要有一組圖像來描述運動。

第1步 - 創建一個可繪製的文件夾。

在它內部創建一個animation_list.xml文件。它包括: 具有幀圖像地址的項目列表。

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> 

<item android:drawable="@drawable/blank" android:duration="210" /> 
<item android:drawable="@drawable/logo" android:duration="210" /> 
<item android:drawable="@drawable/logo1" android:duration="210" /> 
<item android:drawable="@drawable/logo2" android:duration="210" /> 
<item android:drawable="@drawable/logo3" android:duration="210" /> 
<item android:drawable="@drawable/logo4" android:duration="210" /> 
<item android:drawable="@drawable/logo5" android:duration="210" /> 
<item android:drawable="@drawable/logo6" android:duration="210" /> 
<item android:drawable="@drawable/logofinal" android:duration="210" /> 
</animation-list> 

步驟2-創建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" > 

    <ImageView 
     android:id="@+id/imageAnimation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" /> 

</RelativeLayout> 

步驟3-外onCreate方法:

聲明圖像查看和動畫繪製對象

// Declaring an Image View and an Animation Drawable 

ImageView view; 
AnimationDrawable frameAnimation; 

Step4-裏面的onCreate方法:

類型轉換圖像視圖 類型轉換動畫繪製對象 設置可繪製backgroung上的圖像視圖

// Typecasting the Image View 
view = (ImageView) findViewById(R.id.imageAnimation); 

// Setting animation_list.xml as the background of the image view 
view.setBackgroundResource(R.drawable.animation_list); 

// Typecasting the Animation Drawable 
frameAnimation = (AnimationDrawable) view.getBackground(); 

Step5-的onCreate方法後:

當它的重點是當它是對用戶可見

的動畫應該只運行。因此在onCreate方法之後定義這個方法。

// Called when Activity becomes visible or invisible to the user 
@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
     if (hasFocus) { 
    // Starting the animation when in Focus 
    frameAnimation.start(); 
    } else { 
     // Stoping the animation when not in Focus 
    frameAnimation.stop(); 
     } 
} 

Source

相關問題