我是安卓的新手。我想gif
在按钮单击时加载或播放图像。gif
我在布局上加载了这两个文件。但是当我单击下一个按钮时,将播放下一个图像,在这里我们为按钮单击编写 setonclickListener .... 如何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();
}
}
}