2013-03-28 62 views
0

我是一個新的android。我想按鈕點擊加載或播放gif圖片。我在版面上加載了兩個gif文件。但是當我點擊下一個按鈕,下一個圖像將播放,在這裏我們寫setonclickListener按鈕單擊....如何播放按鈕在Android上點擊gif圖像。如果你知道,請告訴我。如何加載GIF文件上的按鈕在Android中單擊?

這裏是我的代碼..

package com.androidqa; 

import java.io.ByteArrayOutputStream; 
import java.io.InputStream; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Movie; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 


public class AnimationView extends View 
{ 
    private Movie mMovie,mMovie1; 
    private long mMovieStart; 
    private long mMovieStart1; 
    Button btnFirst,btnSecond; 
    private static final boolean DECODE_STREAM = true; 
    private static byte[] streamToBytes(InputStream is) 
    { 
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024); 
    byte[] buffer = new byte[1024]; 
    int len; 
    try 
    { 
     while ((len = is.read(buffer)) >= 0) { 
      os.write(buffer, 0, len); 
     } 
     } catch (java.io.IOException e) { 
     } 
     return os.toByteArray(); 
    } 
    @SuppressWarnings("unused") 
    private static byte[] streamToBytes1(InputStream is1) 
    { 
    ByteArrayOutputStream os1 = new ByteArrayOutputStream(1024); 
    byte[] buffer1 = new byte[1024]; 
    int len; 
    try 
    { 
     while ((len = is1.read(buffer1)) >= 0) { 
      os1.write(buffer1, 0, len); 
     } 
     } catch (java.io.IOException e) { 
     } 
     return os1.toByteArray(); 
    } 

    public AnimationView(Context context,AttributeSet attrs) 
    { 
     super(context,attrs); 
     setFocusable(true); 
     java.io.InputStream is; 
     java.io.InputStream is1; 
     is = context.getResources().openRawResource(R.drawable.th_welcome); 
     is1 = context.getResources().openRawResource(R.drawable.animimage); 
     if (DECODE_STREAM) 
     { 
     mMovie = Movie.decodeStream(is); 
     mMovie1=Movie.decodeStream(is1); 
     } else 
     { 
     byte[] array = streamToBytes(is); 
     mMovie = Movie.decodeByteArray(array, 0, array.length); 
     byte[] array1 = streamToBytes1(is1); 
     mMovie1= Movie.decodeByteArray(array1, 0, array1.length); 
     } 
    } 
    @Override 
    public void onDraw(Canvas canvas) 
    { 
    long now = android.os.SystemClock.uptimeMillis(); 

     if (mMovieStart == 0) { // first time 
     mMovieStart = now; 
     } 
     if (mMovie != null) 
     { 
     int dur = mMovie.duration(); 
     if (dur == 0) { 
      dur = 3000; 
     } 
     int relTime = (int) ((now - mMovieStart) % dur); 
     Log.d("", "real time :: " +relTime); 
     mMovie.setTime(relTime); 
     mMovie.draw(canvas, getWidth() - 200, getHeight()-200); 
     invalidate(); 
     } 
     if (mMovieStart1 == 0) { // first time 
      mMovieStart1 = now; 
     } 
     if (mMovie1 != null) 
     { 
      int dur = mMovie1.duration(); 
      if (dur == 0) { 
      dur = 3000; 
      } 
      int relTime = (int) ((now - mMovieStart1) % dur); 
     Log.d("", "real time :: " +relTime); 
      mMovie1.setTime(relTime); 
      mMovie1.draw(canvas, getWidth() - 200, getHeight()-200); 
      invalidate(); 
     } 
    } 
    } 
+0

爲什麼你不使用簡單的frameanimation? AFAIK gif不會在android中播放。 – 2013-03-28 07:24:51

+0

Android不支持播放gif文件,如果您想播放,您將不得不使用動畫。 – 2013-03-28 07:52:25

+0

我只想加載gif圖像而不是jpg圖像.....幀動畫許多圖像只能動畫...... – Bala 2013-03-28 09:05:47

回答

1

Android不支持GIF動畫文件的播放。如果您需要播放它們,則需要將它們拆分爲多個幀,並逐一爲每個幀設置動畫。

也見下面的鏈接更clearification: -

How to play a GIF file in Android?

Is it possible to play GIF format in Android?

+0

我錯誤地問了問題....我不想玩gif圖像...在按鈕上單擊gif圖像將被加載..... gif圖像加載佈局沒有按鈕點擊...我需要當我點擊按鈕時,gif圖像將被顯示或加載..提前感謝。@ HCD – Bala 2013-03-28 09:52:46

0

在處理一個按鈕,點擊您的MainActivity寫代碼。即:

static boolean clicked = false; 

public void onClick(View v) { 

clicked = true; 
AnimationView.invalidate(); 

/* When I am referring to AnimationView, I mean the name of your AnimationView and not the class. */ 

} 

然後在你的onDraw方法中做一個if語句,說如果clicked爲true,視圖只會失效。

祝你好運

相關問題