实现内容是:在android实现Gif播放,原理是自定义View,然后在自定view用Movie类实现Gif画在canvas中来播放Movie中的图片。
内容:提供播放和暂停功能
注意事项:在注册表对应的Activity中要android:hardwareAccelerated=“false”,就是说不要利用硬件加速,原理参考:http://blog.csdn.net/yarkey09/article/details/13022387
package com.example.myViews;
import java.io.InputStream;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
1.www.wyyfk.cn
import android.widget.ImageView;
/**
*
- @author lxq
- @version 1.0
- @category 实现自定义播放Gif
*/
public class GifImageView extends ImageView {
private Movie movie;
private boolean isPlaying=false; //表示正在播放
private int with=0;
private int height=0;
private long currentTime=0;
private long preTime=0;
private boolean haveGif=false;
private long pausePage=0;
private long currentPage=0;
private boolean is_init=false;
public GifImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public GifImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public GifImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
if(!haveGif) super.onDraw(canvas);
else if(movie!=null){
if(is_init){
movie.setTime(0);
movie.draw(canvas, 0, 0);
is_init=false;
}
else{
if(isPlaying){
if(!playMovie(canvas)){
pausePage=0;
currentPage=0;
isPlaying=false;
is_init=true;
}
invalidate();
}
else{
playMovie(canvas);
}
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
if(movie!=null){
setMeasuredDimension(with, height);
}
else{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void startGif(){
if(isPlaying==true) return;
isPlaying=true;
invalidate();
}
public void pauseGif(){
if(isPlaying==false) return;
isPlaying=false;
invalidate();
}
/**
*
* @param canvas
* @return false means playing movie is end
*
*/
public boolean playMovie(Canvas canvas){
currentTime=SystemClock.uptimeMillis();
if(preTime==0){
preTime=currentTime;
}
int time=(int)((currentTime-preTime)%movie.duration());
time=(int)((time+pausePage));
if(time>=movie.duration()){
return false;
}
else{
movie.setTime(time);
movie.draw(canvas, 0, 0);
//invalidate();
return true;
}
}
/**
*
* @param is
* @throws NullPointerException
*
*/
public void setGif(InputStream is) throws NullPointerException{
if(is!=null){
movie=Movie.decodeStream(is);
if(movie!=null){
haveGif=true;
Bitmap bitmap=BitmapFactory.decodeStream(is);
if(bitmap!=null){
with=bitmap.getWidth();
height=bitmap.getHeight();
Log.e("with="+with+",height="+height,"...........");
bitmap.recycle();
}
else{
Log.e("bitmap=null","..............");
}
/*
with=302;
height=220;*/
haveGif=true;
is_init=true;
//setMeasuredDimension(with, height);
requestLayout();
//invalidate();
}
}
else {
throw new NullPointerException("no find inputSteam");
}
}
public void setWithAndHeight(int w,int h){
with=w;
height=h;
}
}
package com.example.activities;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.example.gifandroid.R;
public class MainActivity extends ActionBarActivity {
com.example.myViews.GifImageView gifImg;
Button start_bn;
Button pause_bn;
InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
void initView(){
gifImg=(com.example.myViews.GifImageView)findViewById(R.id.gifImageView);
try {
is=MainActivity.this.getAssets().open("anim.gif");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gifImg.setGif(is);
start_bn=(Button)findViewById(R.id.start);
pause_bn=(Button)findViewById(R.id.pause);
gifImg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, 1);
}
});
start_bn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
gifImg.startGif();
}
});
pause_bn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
gifImg.pauseGif();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.e("uri", uri.toString());
ContentResolver cr = this.getContentResolver();
try {
is=cr.openInputStream(uri);
if(is==null){
Toast.makeText(MainActivity.this, "Main_is==null",Toast.LENGTH_LONG).show();
Log.e("Main_is==null","..........");
}
else{
Toast.makeText(MainActivity.this, "Main_is!=null",Toast.LENGTH_LONG).show();
Log.e("main_is!=null","..........");
}
Bitmap bitmap=BitmapFactory.decodeStream(cr.openInputStream(uri));
if(bitmap!=null){
int with=bitmap.getWidth();
int height=bitmap.getHeight();
Log.e("bitmap_with="+with+",height="+height,"...........");
bitmap.recycle();
}
gifImg.setWithAndHeight(300, 300);
gifImg.setGif(is);
//gifImg.setBackground(new BitmapDrawable(BitmapFactory.decodeStream(is)));
//gifImg.setImageBitmap(BitmapFactory.decodeStream(is));
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.example.gifandroid”
android:versionCode=“1”
android:versionName=“1.0”
>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission
android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS"
/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.activities.MainActivity"
android:label="@string/app_name"
android:hardwareAccelerated="false"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>