欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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>