android播放gif动画控件(例子)

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>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值