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;
		}
}

代碼可直接使用 很簡單 注釋就不寫了

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com