我正在整合giphy到我的android应用程序。如何在android中从url中播放动画GIF图像?
我该如何在Android上玩animated gif
图片?我应该使用ImageView, WebView, VideoView
等吗?例如,如果我想从this网址播放动画。
我正在整合giphy到我的android应用程序。如何在android中从url中播放动画GIF图像?
我该如何在Android上玩animated gif
图片?我应该使用ImageView, WebView, VideoView
等吗?例如,如果我想从this网址播放动画。
只需创建一个html字符串并将其加载到android webview中即可。测试的解决方案。
http://developer.android.com/reference/android/webkit/WebView.html#loadData(java.lang.String,java.lang.String中,java.lang.String中)
String x = "<!DOCTYPE html><html><body><img src=\"http://goo.gl/uPJ9P2\" alt=\"Smileyface\" width=\"100%\" height=\"100%\"></body></html>";
webView.loadData(x, "text/html", "utf-8");
是这样,我应该把URL字符串内X =“
http://giphy.com/gifs/funny-cat-DFiwMapItOTh6” – 2014-11-03 10:39:35u能plzz写什么要我把里面的字符串x..the代码ü上面给不具备的起始体标记,但确实有一个结束body标签?是IMG标记?thanku乌拉圭回合时间... – 2014-11-03 10:42:44
@ user3753273检查现在 – Javanator 2014-11-03 10:53:37
尝试这种方式
Movie movie;
GifView(Context context) {
super(context);
movie = Movie.decodeStream(
context.getResources().openRawResource(
R.drawable.some_gif));
}
@Override
protected void onDraw(Canvas canvas) {
if (movie != null) {
movie.setTime(
(int) SystemClock.uptimeMillis() % movie.duration());
movie.draw(canvas, 0, 0);
invalidate();
}
}
我想从网址播放..我应该用url替换R.drawable.some_gif吗? – 2014-11-03 10:27:07
解决方案的总体草图是使用聘请定制查看其中提请问定期将其自身绘制到画布的电影。
第一步是构建Movie实例。有一个名为decodeStream的工厂,可以通过InputStream创建电影,但仅使用来自UrlConnection的流是不够的。如果您尝试此操作,您将在电影加载器尝试调用流上的重置时收到IOException。不幸的是,使用一个单独的BufferedInputStream和一个手动设置的标记来告诉它保存足够的数据,以便重置不会失败。幸运的是,URLConnection可以告诉我们需要多少数据。我说这种攻击是不幸的,因为它有效地要求将整个图像缓冲在内存中(这对桌面应用程序来说不成问题,但对于内存受限的移动设备而言这是一个严重问题)。
这里是电影设置代码的剪断:
URL url = new URL(gifSource);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bis.mark(conn.getContentLength());
Movie movie = Movie.decodeStream(bis);
bis.close();
接下来,你需要创建一个显示该电影的视图。具有自定义onDraw的视图的子类将执行该技巧(假设它可以访问用前面的代码创建的电影)。
@Override protected void onDraw(Canvas canvas) {
if(movie != null) {
long now = android.os.SystemClock.uptimeMillis();
int dur = Math.max(movie.duration(), 1); // is it really animated?
int pos = (int)(now % dur);
movie.setTime(pos);
movie.draw(canvas, x, y);
}
}
视图不会触发本身没有帮助重绘,盲目地调用无效()在的onDraw结束仅仅是一个能源浪费。在另一个线程(可能是您用来下载图像数据的线程)中,您可以将消息发布到主线程,要求视图以稳定(但不是疯狂)的速度失效。
Handler handler = new Handler();
new Thread() {
@Override public void run() {
// ... setup the movie (using the code from above)
// ... create and display the custom view, passing the movie
while(!Thread.currentThread().isInterrupted()) {
handler.post(new Runnable() {
public void run(){
view.invalidate();
}
});
try {
Thread.sleep(50); // yields 20 fps
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}.start();
希望这将有助于您了解
http://stackoverflow.com/questions/3660209/display-animated-gif – 2014-11-03 10:22:07
http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html – Naufal 2014-11-03 10:22:44
如果你做了研发,那么你可以找到它。 http://gayashan-a.blogspot.in/2012/02/android-how-to-display-gif-image-in.html,http://android-er.blogspot.in/2014/03/load- animated-gif-from-internet.html,http://androidsurya.blogspot.in/2014/03/how-to-load-animated-.gif-from-internet-programmatically-in-android-example-how-to -show-gif-images-in-android.html – Piyush 2014-11-03 10:23:11