android 仿系统图片播放器中的幻灯片播放

在android系统相册中有一个幻灯片播放的功能 效果非常好 他是用openGl 画的  现在我分享一个使用SurfaceView画的一个小demo  不多说 直接看代码:

package com.zhao.imageslide;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class SlideSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable{ 
		private static final String TAG = "SlideSurfaceView";
	    boolean mbLoop = false;
	  
	    private Context mContext;
	    SurfaceHolder mSurfaceHolder = null;
	    private int mWidth = 0;
	    private int mHeight = 0;
	    
	    private List<Integer> mList;
	    private Bitmap mCurrentBitmap;
	    private Bitmap mNextBitmap;
	    private boolean isLoadingNextBitmap = false;
	    private int mCurrentposition = 0;
	    int miCount = 0;  
	    int y =50;  
	      
	    public SlideSurfaceView(Context context, List<Integer> list) {  
	        super(context);  
	        mContext = context;
	        mList = list;
	        mSurfaceHolder = this.getHolder();  
	        
	        mSurfaceHolder.addCallback(this);
	        this.setFocusable(true);  
	        mbLoop = true;  
	    }  
	    
	      
	    @Override  
	    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
	            int height) {  
	    }  
	      
	    @Override  
	    public void surfaceCreated(SurfaceHolder holder) {  
	    	mWidth = getMeasuredWidth();
	        mHeight = getMeasuredHeight();
	        Log.i(TAG, "mWidth:" + mWidth + "  mHeight:" + mHeight);
	        loadFirstBitmap();
	        new Thread(this).start();
	    }  
	      
	    @Override  
	    public void surfaceDestroyed(SurfaceHolder holder) {  
	        mbLoop = false;  
	    }  
	    
	    
	      
	    @Override  
	    public void run() {  
	        while (mbLoop){  
	            try {  
	                Thread.sleep(10);  
	            } catch (Exception e) {  
	                  
	            }  
	            synchronized( mSurfaceHolder ){  
	                Draw();  
	            }  
	        }  
	    }  
	    public void Draw(){  
	        Canvas canvas = mSurfaceHolder.lockCanvas();  
	        if (mSurfaceHolder==null || canvas == null) {  
	            return;  
	        }  
	        if (miCount < 100) {  
	            miCount++;  
	        }else {  
	            miCount = 5;  
	        }  
	        Paint mPaint = new Paint();  
	        Paint mNextPaint = new Paint();
	        mPaint.setAntiAlias(true);  
	        canvas.drawRect(0, 0, mWidth, mHeight, mPaint);
	        switch (miCount) {
			case 96:
				mPaint.setAlpha(215);
				mNextPaint.setAlpha(55);
				break;
			case 97:
				mPaint.setAlpha(175);
				mNextPaint.setAlpha(95);
				break;
			case 98:
				mPaint.setAlpha(135);
				mNextPaint.setAlpha(135);
				break;
			case 99:
				mPaint.setAlpha(95);
				mNextPaint.setAlpha(175);
				break;
			case 100:
				mPaint.setAlpha(55);
				mNextPaint.setAlpha(215);
				break;
			default:
				break;
			}
	        if(miCount > 95) {
	        	if(mNextBitmap != null) {
	        		Bitmap nextBitmap = getScaleImage(mNextBitmap, miCount - 96);
					canvas.drawBitmap(nextBitmap, (mWidth - nextBitmap.getWidth()) / 2, (mHeight - nextBitmap.getHeight()) / 2, mNextPaint);
	        	}
	        }
	        Bitmap bitmap = getScaleImage(mCurrentBitmap, miCount);
	        canvas.drawBitmap(bitmap, (mWidth - bitmap.getWidth()) / 2, (mHeight - bitmap.getHeight()) / 2, mPaint);
	        mSurfaceHolder.unlockCanvasAndPost(canvas);
	        if(mNextBitmap != null) {
	        	if(miCount >= 100) {
	        		Log.i(TAG, "change to next");
	        		mCurrentBitmap.recycle();
	        		mCurrentBitmap = mNextBitmap;
	        		mNextBitmap = null;
	        		mCurrentposition ++;
	        		if(mCurrentposition >= mList.size()) {
	        			mCurrentposition = 0;
	        		}
	        	}
	        } else {
	        	if(!isLoadingNextBitmap) {
	        		loadNextBitmap();
	        	}
	        }
	    }  
	 
	    private Bitmap getScaleImage(Bitmap srcBitmap, int index) {
	    	Bitmap bitmap = null;
	    	Matrix matrix = new Matrix();
	    	float scale = getScale(srcBitmap);
	    	float scaleSpeed;
	    	if(scale < 1) {
	    		scaleSpeed = (1 - scale) / 100;
    		} else {
    			scaleSpeed = 0.2f / 100;
    		}
	    	float bitScale = scale + scaleSpeed * index;
	    	matrix.postScale(bitScale, bitScale);
	    	bitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true);
	    	return bitmap;
	    }
	    
	    
	    private void loadFirstBitmap() {
	    	if(mCurrentBitmap == null) {
	    		BitmapFactory.Options options = new BitmapFactory.Options();
				options.inJustDecodeBounds = true;
				BitmapFactory.decodeResource(getResources(), mList.get(0), options);
				options.inJustDecodeBounds = false;
				options.inSampleSize = calculateInSampleSize(options,(int)(mWidth * 1.2F), (int)(mHeight * 1.2F));
				mCurrentBitmap = BitmapFactory.decodeResource(getResources(), mList.get(0), options);
				if(mCurrentBitmap != null) {
					mCurrentBitmap = createScaleBitmap(mCurrentBitmap, (int)(mWidth * 1.2F), (int)(mHeight * 1.2F));
				}
	    	}
		}
	    
	    private void loadNextBitmap() {
	    	Log.i(TAG, "loadNextBitmap");
	    	isLoadingNextBitmap = true;
	    	new Thread() {
	    		@Override
	    		public void run() {
	    			super.run();
	    			if(mList != null && mList.size() > 0) {
	    				int index = mCurrentposition + 1;
	    				if(index >= mList.size()) {
	    					index = 0;
	    				}
	    				
	    				BitmapFactory.Options options = new BitmapFactory.Options();
						options.inJustDecodeBounds = true;
						BitmapFactory.decodeResource(getResources(), mList.get(index), options);
						options.inJustDecodeBounds = false;
						options.inSampleSize = calculateInSampleSize(options,(int)(mWidth * 1.2F), (int)(mHeight * 1.2F));
						mNextBitmap = BitmapFactory.decodeResource(getResources(), mList.get(index), options);
						if(mNextBitmap != null) {
							mNextBitmap = createScaleBitmap(mNextBitmap, (int)(mWidth * 1.2F), (int)(mHeight * 1.2F));
						}
	    			}
	    			isLoadingNextBitmap = false;
	    		}
	    	}.start();
	    }
	    
	    private static int calculateInSampleSize(BitmapFactory.Options options,
	            int reqWidth, int reqHeight) {
	        final int height = options.outHeight;
	        final int width = options.outWidth;
	        int inSampleSize = 1;
	        if (height > reqHeight || width > reqWidth) {
	            final int halfHeight = height / 2;
	            final int halfWidth = width / 2;
	            while ((halfHeight / inSampleSize) > reqHeight
	                    && (halfWidth / inSampleSize) > reqWidth) {
	                inSampleSize *= 2;
	            }
	        }
	        return inSampleSize;
	    }
		
		private static Bitmap createScaleBitmap(Bitmap src, int dstWidth, int dstHeight) {
			Bitmap dst = null;
	        int width = src.getWidth();
	        int height = src.getHeight();
	        Log.i(TAG, "dstWidth:" + dstWidth + "  width:"+ width +"   dstHeight:" + dstHeight + "  height:" + height);
	        float scale = 1.0f;
	        Matrix matrix = new Matrix();
	        if(width > dstWidth || height > dstHeight) {
	        	scale = Math.min(dstWidth * 1.0F / width, dstHeight * 1.0F / height);
	        } else {
	        	if(width > height) {
	        		scale = dstWidth * 1.0f / width;
	        	} else {
	        		scale = dstHeight * 1.0f / height;
	        	}
	        	if(scale >= 4.0f) {
	        		scale = 4.0f;
	        	}
	        }
	        Log.e(TAG, "scale:" + scale);
        	matrix.postScale(scale, scale);
        	dst = Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
	        if(src != dst) {
	        	src.recycle();
	        }
	        Log.e(TAG, "dstHeight    " + "height:" + dst.getHeight() + "   width:" + dst.getWidth() );
	        return dst;
	    }

		private float getScale(Bitmap bitmap) {
			float scale = 1.0f;
			int width = bitmap.getWidth();
			int height = bitmap.getHeight();
			if(width <= mWidth && height <= mHeight) {
			} else {
				float scaleWidth = mWidth * 1.0f / width;
				float scaleHeight = mHeight * 1.0F / height;
				scale = scaleWidth > scaleHeight ? scaleHeight : scaleWidth;
			}
			return scale;
		}
}

代码可直接使用 很简单 注释就不写了

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值