2013-03-25 72 views
1

我想在一秒內顯示約30張照片,因此它們將顯示爲視頻。在Android中顯示連續圖像

我在while(TRUE)循環中使用Android ImageView,但ImageView沒有像我期望的那樣變化。

的ImageView的改變圖片的大約每秒,所以不知道是否有另一種方式來

表現出我的照片?

這些照片是從使用UDP照相機傳送,並且我首先需要在緩衝液中和

比顯示存儲。

上述部分導致ImageView延遲。代碼

部分是這樣的

public void receiveVideoRawData() throws IOException{ 
     socket_video = new DatagramSocket(); 
     byte[] buf_rcv = new byte[153600]; 
     DatagramPacket rawData = new DatagramPacket(buf_rcv, buf_rcv.length); 
     Bitmap image = null; 
     socket_video.receive(rawData);//receive a raw data 
     image = readMyRawData.readUINT_RGBImage(buf_rcv);//decode the raw data 
     ByteArrayOutputStream blob = new ByteArrayOutputStream(); 
     Bitmap pBitmap = image ; 
     pBitmap.compress(Bitmap.CompressFormat.JPEG, 1, blob); 
     byte[] bitmapdata = blob.toByteArray(); 
     ardrone_Frame = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata.length); 
     } 

顯示部分

Thread thread=new Thread (new Runnable() { 
    Message message; 
    String obj="run"; 
    int i; 
    long start; 
    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     Log.e("///enter thread ","THREAD"); 
    while(true){  
     try { 
      Log.e("enter_video_thread","enter_video_thread"); 
      receiveVideoRawData(); 
      Log.e("///receiveVideoRawData ","receiveVideoRawData"); 
      message = handler.obtainMessage(1,obj); 
        handler.sendMessage(message); 
      } 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

}); 
public Handler handler = new Handler(){ 
    @Override 
    public void handleMessage(Message msg) { 
      super.handleMessage(msg); 
      String MsgString = (String)msg.obj; 
      if (MsgString.equals("run")) 
      {   Log.e("///enter handler ","HANDLER");           
          Toast.makeText(ArDroneMain.this,"save image ",Toast.LENGTH_SHORT).show(); 
          //Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+ "/ArdroneVideo.jpg"); 
          myImageView.setImageBitmap(ardrone_Frame); ///Show the image  
          Toast.makeText(ArDroneMain.this,"show image",Toast.LENGTH_SHORT).show(); 
      } 

    } 
}; 
+0

代碼爲這些圖像做ü要動態加載圖像?或者它存儲在res? – 2013-03-25 09:39:47

+0

設置每個圖像後,需要重新繪製'ImageView'。你能展示你目前使用的代碼嗎? – Tigger 2013-03-25 09:50:43

回答

4

你可以做到這一點,我使它成爲一個動畫。 爲您的動畫製作圖像並保存在png格式的文件夾中。 動畫下面

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> 
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> 
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> 
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> 
</animation-list> 

然後

AnimationDrawable rocketAnimation; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); 
    rocketImage.setBackgroundResource(R.drawable.rocket_thrust); 
    rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); 
} 

public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
    rocketAnimation.start(); 
    return true; 
    } 
    return super.onTouchEvent(event); 
} 
0

您可以隨時使用FFmpeg的創建圖像的視頻,還可以使用一個TimerTask

delay=1000/totalImages; 

myTimer = new Timer(); 
     myTimer.schedule(new TimerTask() {   
      @Override 
      public void run() { 

          if(count==imgArr.length) 
           cancel(); 

          imageView.setImageResource(imgArr[count]); 
          count++;  

      } 

     }, 0, delay);