阳光天下

学无止境-与大家共同进步

导航

继承SurfaceView实现图片自动播放

Posted on 2011-12-11 16:22  阳光天下  阅读(6857)  评论(1编辑  收藏  举报

代码如下,多多交流。

  1 package com.farben.ams.widget;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.util.List;
6 import android.content.Context;
7 import android.graphics.Bitmap;
8 import android.graphics.BitmapFactory;
9 import android.graphics.Canvas;
10 import android.graphics.Color;
11 import android.graphics.Matrix;
12 import android.graphics.Paint;
13 import android.graphics.Rect;
14 import android.graphics.Paint.Style;
15 import android.media.ThumbnailUtils;
16 import android.provider.MediaStore.Images.Thumbnails;
17 import android.util.Log;
18 import android.view.SurfaceHolder;
19 import android.view.SurfaceView;
20 import android.view.ViewGroup.LayoutParams;
21
22 /**
23 * 图片浏览器
24 * @author Keynes Cao
25 * @create 2011-12-5 上午9:24:23
26 * @package com.farben.ams.widget
27 */
28 public class ImageSurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable{
29
30 //图片集
31 private List<String> mList = null;
32 //运行状态
33 public boolean mLoop = false;
34 //获取画布
35 private SurfaceHolder mSurfaceHolder = null;
36 //图片索引
37 private int mCount = 0;
38 //时间间隔
39 private long speed = 1000;
40
41 private static Matrix matrix = new Matrix();
42 /**
43 * @param context
44 * <see>容器</see>
45 * @param list
46 * <see>图片地址列表 </see>
47 * @param rate
48 * <see>图片切换时间 单位:毫秒</see>
49 *
50 */
51 public ImageSurfaceView(Context context,List<String>list,long speed) {
52 super(context);
53 if(list!=null && list.size()>0){
54 this.mList = list;
55 this.speed = speed;
56 }
57 mSurfaceHolder = getHolder();
58 mSurfaceHolder.addCallback(this);
59 mLoop = true;//开始画图
60 }
61
62 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
63 //图像创建时
64 public void surfaceCreated(SurfaceHolder holder) {
65 if(mList!=null && mList.size()>0){
66 if(mList.size() == 1){
67 Log.d("ImageSurfaceView"," Only one picture");
68 drawImg();
69 }else{
70 Log.d("ImageSurfaceView"," run Thread.");
71 new Thread(this).start();
72 }
73 }
74
75 }
76 //视图销毁时
77 public void surfaceDestroyed(SurfaceHolder holder) {
78 mLoop = false;
79 }
80 //画图方法
81 private void drawImg(){
82 Canvas canvas = mSurfaceHolder.lockCanvas();
83 if(canvas == null || mSurfaceHolder == null){
84 return;
85 }
86 if(mCount >= mList.size()){
87 mCount = 0;
88 }
89 Bitmap bitmap = null;
90 try{
91 String path = mList.get(mCount++);
92 bitmap = BitmapFactory.decodeStream(new FileInputStream(path));
93 if(bitmap!=null){
94 //画布宽和高
95 int height = getHeight();
96 int width = getWidth();
97 //生成合适的图像
98 bitmap = getReduceBitmap(bitmap,width,height);
99
100 Paint paint = new Paint();
101 paint.setAntiAlias(true);
102 paint.setStyle(Style.FILL);
103 //清屏
104 paint.setColor(Color.BLACK);
105 canvas.drawRect(new Rect(0, 0, getWidth(),getHeight()), paint);
106 //Log.d("ImageSurfaceView_IMG",path);
107 //画图
108 canvas.drawBitmap(bitmap, matrix, paint);
109 }
110 //解锁显示
111 mSurfaceHolder.unlockCanvasAndPost(canvas);
112 }catch(Exception ex){
113 Log.e("ImageSurfaceView",ex.getMessage());
114 return;
115 }finally{
116 //资源回收
117 if(bitmap!=null){
118 bitmap.recycle();
119 }
120 }
121 }
122 //刷新图片
123 public void run() {
124 while(mLoop){
125 synchronized (mSurfaceHolder) {
126 drawImg();
127 }
128 try {
129 Thread.sleep(speed);
130 } catch (InterruptedException e) {
131 Log.e("ImageSurfaceView_Thread",e.getMessage());
132 }
133 }
134 mList = null;//消毁
135 }
136 //缩放图片
137 private Bitmap getReduceBitmap(Bitmap bitmap ,int w,int h){
138 int width = bitmap.getWidth();
139 int hight = bitmap.getHeight();
140 Matrix matrix = new Matrix();
141 float wScake = ((float)w/width);
142 float hScake = ((float)h/hight);
143 matrix.postScale(wScake, hScake);
144 return Bitmap.createBitmap(bitmap, 0,0,width,hight,matrix,true);
145 }
146
147 }