在android中使用GIF图像的方法和类
我想在在android中使用GIF图像的方法和类,android,gif,Android,Gif,我想在android中使用GIF图像。 我找不到合适的教程来定义在android中使用GIF图像时使用的类或方法 我想在安卓系统中点击一个按钮播放相应的GIF图像 我是android新手,请帮助。最简单的解决方案是使用Glide库。 也存在支持GIF的WebView解决方案,但我不推荐它 您可以在不使用任何库的情况下播放GIF图像。请参阅 以下代码: xml: 使用Glade库加载GIF图像单击按钮即可在对话框中播放GIF <com.jaldihealthy.SampleView
android
中使用GIF
图像。
我找不到合适的教程来定义在android中使用GIF
图像时使用的类或方法
我想在安卓系统中点击一个按钮播放相应的GIF
图像
我是android新手,请帮助。最简单的解决方案是使用Glide库。 也存在支持GIF的WebView解决方案,但我不推荐它 您可以在不使用任何库的情况下播放GIF图像。请参阅 以下代码: xml:
使用Glade库加载GIF图像单击按钮即可在对话框中播放GIF
<com.jaldihealthy.SampleView
android:id="@+id/gif_view"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_height="300dp"/>
public class SampleView extends View {
private Movie mMovie;
private long mMovieStart;
private ProgressDialog mProgressDialog;
Context context;
public SampleView(Context context) {
super(context);
setFocusable(true);
}
public void setmMovie(String url,Context context) {
this.context=context;
new RetrieveFeedTask().execute(url);
}
public SampleView(Context context, AttributeSet attrSet) {
super(context, attrSet);
setFocusable(true);
/* java.io.InputStream is;
is = context.getResources().openRawResource(R.drawable.girl_dances);
mMovie = Movie.decodeStream(is);*/
}
public SampleView(Context context, AttributeSet attrSet, int defStyle) {
super(context, attrSet, defStyle);
setFocusable(true);
/* java.io.InputStream is;
is = context.getResources().openRawResource(R.drawable.girl_dances);
mMovie = Movie.decodeStream(is);*/
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0x00000000);
Paint p = new Paint();
p.setAntiAlias(true);
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (mMovie != null) {
int dur = mMovie.duration();
if (dur == 0) {
dur = 1000;
}
int relTime = (int) ((now - mMovieStart) % dur);
mMovie.setTime(relTime);
mMovie.draw(canvas, getWidth() / 2 - mMovie.width() / 2,
getHeight() / 2 - mMovie.height() / 2);
invalidate();
}
}
public void showDialog() {
mProgressDialog = new ProgressDialog(context, R.style.MyTheme);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
mProgressDialog.show();
mProgressDialog.setContentView(R.layout.progress_layout);
mProgressDialog.setCancelable(false);
}
public void dismissDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
class RetrieveFeedTask extends AsyncTask<String, Void, InputStream> {
private Exception exception;
InputStream is;
@Override
protected void onPreExecute() {
showDialog();
super.onPreExecute();
}
protected InputStream doInBackground(String... urls) {
try {
String url = urls[0];
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xmlreader = parser.getXMLReader();
is = new URL(url).openStream();
mMovie = Movie.decodeStream(is);
} catch (Exception e) {
this.exception = e;
return null;
}
return is;
}
protected void onPostExecute(InputStream is) {
// TODO: check this.exception
// TODO: do something with the feed
dismissDialog();
invalidate();
}
}
}
gif_view.setmMovie(url,mContext);