android播放gif动画控件(例子)
程序员文章站
2022-07-14 17:46:12
...
1.测试程序
package com.test;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import com.ant.liao.GifView;
import com.ant.liao.GifView.GifImageType;
public class TestAction extends Activity implements OnClickListener{
private GifView gf1;
private GifView gf2;
private String httpUrl = "http://img4.3lian.com/sucai2/img16/125/239.gif";
private HttpClient httpClient = null;
private boolean f = true;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//Log.d("dddddddddd",Environment.getRootDirectory().getAbsolutePath());
// LinearLayout ll = new LinearLayout(this);
// LayoutParams la = new LayoutParams(LayoutParams.FILL_PARENT,
// LayoutParams.FILL_PARENT);
//
// ll.setLayoutParams(la);
// gf1 = new GifView(this);
// gf2 = new GifView(this);
//
// gf1.setGifImage(R.drawable.gif1);
// gf2.setGifImage(R.drawable.gif2);
//
// ll.addView(gf1);
// ll.addView(gf2);
//
// setContentView(ll);
setContentView(R.layout.gif);
InputStream is = getHttpGif("http://www.fyjs.cn/bbs/attachments/Mon_0901/200_72972_de058a810e39fcf.gif");
if (is != null){
gf1 = (GifView)findViewById(R.id.gif1);
//gf1.setGifImage(R.drawable.gif1);
gf1.setGifImage(is);
gf1.setOnClickListener(this);
}
//
gf2 = (GifView)findViewById(R.id.gif2);
gf2.setGifImageType(GifImageType.COVER);
gf2.setShowDimension(300, 300);
gf2.setGifImage(R.drawable.a);
//gf2.setOnClickListener(this);
}
public InputStream getHttpGif(String url){
InputStream stream = null;
if (url.length() > 0){
try {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
HttpConnectionParams.setSoTimeout(params, 20 * 1000);
HttpConnectionParams.setSocketBufferSize(params, 8192);
// 设置重定向,缺省为 true
HttpClientParams.setRedirecting(params, true);
httpClient = new DefaultHttpClient(params);
HttpGet httpGet = new HttpGet (url);
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
httpGet.abort();
return null;
}
stream = response.getEntity().getContent();
} catch (Exception e) {
e.printStackTrace();
}
}
return stream;
}
public void onClick(View v) {
if(f){
gf2.showCover();
f = false;
}else{
gf2.showAnimation();
f = true;
}
}
}
2.XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.ant.liao.GifView
android:id="@+id/gif1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingRight="14px" android:enabled="false" />
<TextView android:id="@+id/tsxt" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingRight="4px" android:enabled="false"
android:text="click the Angel" />
<com.ant.liao.GifView
android:id="@+id/gif2"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingTop="4px" android:paddingLeft="14px" android:enabled="false" />
</LinearLayout>
下一篇: Leetcode9.回文数
推荐阅读
-
android 简单图片动画播放的实例代码
-
android之视频播放系统VideoView和自定义VideoView控件的应用
-
WPS手机移动版Android Excel测量计算程序附GIF动画
-
android使用ExpandableListView控件实现小说目录效果的例子
-
html5 移动端视频video的android兼容(去除播放控件、全屏)
-
Android-JNI开发系列实践-利用Android C源码实现GIF图片的播放
-
Android自定义控件实现带数值和动画的圆形进度条
-
Android编程实现动画自动播放功能
-
三星老机型Always On Display功能也能播放GIF动画了
-
Android自定义控件--3(刷新动画)