如何在android中点击按钮加载gif文件?

时间:2013-03-28 07:21:56

标签: android android-canvas

我是android中的新手。我想在点击按钮时加载或播放gif图像。我在布局上加载了两个gif文件。但是当我点击下一个按钮时,下一个图像将被播放,在这里我们编写setonclickListener按钮点击....如何在按钮点击android中播放gif图像。如果你知道的话请告诉我。

这是我的代码..

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

2 个答案:

答案 0 :(得分:1)

Android不支持播放动画GIF文件。如果你需要播放它们,那么你需要将它们分成几帧,并逐个动画每一帧。

另见下面链接以获得更多清晰度: -

How to play a GIF file in Android?

Is it possible to play GIF format in Android?

答案 1 :(得分: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才会使视图失效。

祝你好运