1. 程式人生 > >android 自定義相簿 多選

android 自定義相簿 多選

package com.bingo.activity;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.bingo.activity.BingoMore_Comm.Adapter;
import com.bingo.util.ImageDispose;
import com.bingo.util.ImageMemory;
import com.bingo.util.JudgePath;

public class BingoPhotoAlbumActivity extends Activity {

	private GridView photoalbum;
	private ImageButton back;
	boolean falg = true;
	private ImageList adapter;
	private Button ok;
	private ProgressDialog dialog = null;
	
	JudgePath path = new JudgePath();
	
	List<String> list;
	List<Bitmap> listB = new ArrayList<Bitmap>();
	List<Map<String,String>>  listtemp =  new ArrayList();
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 視窗去掉標題
		requestWindowFeature(Window.FEATURE_NO_TITLE);
//		// 視窗設定為全屏
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.photoalbum);
		
		context =this;
		
		back= (ImageButton) findViewById(R.id.xiangce_back);
		back.setOnClickListener(new xcback());
		ok = (Button) findViewById(R.id.xiangceok);
		ok.setOnClickListener(new xcOk());
		
		
		list = getSD();
		
		imageBitmap();
		
		photoalbum = (GridView) findViewById(R.id.GridView_photoalbum);
		photoalbum.setNumColumns(4);
		photoalbum.setGravity(Gravity.CENTER);
		photoalbum.setVerticalSpacing(3);
		photoalbum.setHorizontalSpacing(1);
		
		
		
		photoalbum.setOnItemClickListener( new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (listtemp.size()>0) {
				falg= true;
					
					for (int i = 0; i < listtemp.size(); i++) {
						if (Integer.valueOf((listtemp.get(i).get("id")))==position) {
							listtemp.remove(i);
							Bitmap src = listB.get(position);
							((ImageView)view).setImageBitmap(src);
							falg = false;
							break;
						}
					}
					if(falg){
						Map<String, String> map = new HashMap<String, String>();
						map.put("id", String.valueOf(position));
						listtemp.add(map);
						Bitmap newb =BitmapFactory.decodeResource(getResources(),R.drawable.selected );
						Bitmap src = adapter.getcheckedImageIDPostion(position);
						Bitmap bi = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Config.ARGB_8888 );
						Canvas cv = new Canvas( bi );  
						cv.drawBitmap( src, 2, 0, null );
						cv.drawBitmap( newb, view.getWidth() - newb.getWidth() -5, view.getHeight() - newb.getHeight() -5, null );//在src的右下角畫入水印    
						cv.save( Canvas.ALL_SAVE_FLAG );//儲存    
						cv.restore();//儲存 
						((ImageView)view).setImageBitmap(bi);
						
						falg = true;
					}
					
				}else {
						Map<String, String> map = new HashMap<String, String>();
						map.put("id", String.valueOf(position));
						listtemp.add(map);
						Bitmap newb =BitmapFactory.decodeResource(getResources(),R.drawable.selected );
						Bitmap src = adapter.getcheckedImageIDPostion(position);
						Bitmap bi = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Config.ARGB_8888 );
						Canvas cv = new Canvas( bi );  
						cv.drawBitmap( src, 2, 0, null );
						cv.drawBitmap( newb, view.getWidth() - newb.getWidth() -5, view.getHeight() - newb.getHeight() -5, null );//在src的右下角畫入水印    
						cv.save( Canvas.ALL_SAVE_FLAG );//儲存    
						cv.restore();//儲存 
						((ImageView)view).setImageBitmap(bi);
				}
			}
		});

	}

	class xcback implements OnClickListener{
		@Override
		public void onClick(View v) {
			finish();
		}
	}
	class xcOk implements OnClickListener{
		@Override
		public void onClick(View v) {
			List<String> list01 = new ArrayList<String>();
			if(listtemp.size()>0){
				for (int i = 0; i < listtemp.size(); i++) {
					//list01.add(adapter.getcheckedImageIDPostion(Integer.valueOf(listtemp.get(i).get("id"))));
					list01.add(list.get(Integer.valueOf(listtemp.get(i).get("id"))));
				}
				
			}
			Intent intent = new Intent(BingoPhotoAlbumActivity.this,PhotoShake.class);
			if(list01.size()>0){
				intent.putExtra("listBitmap", (Serializable)list01);
				startActivity(intent);
			}else{
				Toast.makeText(BingoPhotoAlbumActivity.this, "請選擇圖片", Toast.LENGTH_SHORT).show();
			}
			
			
		}
		
	}
	
	public class ImageList extends BaseAdapter {
		private Context context;

		public ImageList(Context a) {
			this.context = a;
		}

		@Override
		public int getCount() {
			return listB.size();
		}

		@Override
		public Object getItem(int position) {
			return listB.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView iv = new ImageView(context);
			iv.setImageBitmap( listB.get(position));
			if(listtemp.size()>0){
				for (int i = 0; i < listtemp.size(); i++) {
					if(Integer.valueOf((listtemp.get(i).get("id")))==position){
						Bitmap newb =BitmapFactory.decodeResource(getResources(),R.drawable.selected );
						Bitmap src = adapter.getcheckedImageIDPostion(position);
						Bitmap bi = Bitmap.createBitmap( listB.get(position).getWidth(), listB.get(position).getHeight(), Config.ARGB_8888 );
						Canvas cv = new Canvas( bi );  
						cv.drawBitmap( src, 2, 0, null );
						cv.drawBitmap( newb, listB.get(position).getWidth() - newb.getWidth() -5, listB.get(position).getHeight() - newb.getHeight() -5, null );//在src的右下角畫入水印    
						cv.save( Canvas.ALL_SAVE_FLAG );//儲存    
						cv.restore();//儲存 
						((ImageView)iv).setImageBitmap(bi);
					}
				}
			}
			iv.setAdjustViewBounds(true);
			return iv;
		}
		/* 自定義獲取對應位置的圖片ID */

		public Bitmap getcheckedImageIDPostion(int i) {

			return listB.get(i);

		}
	}
	
	
	private void imageBitmap(){
		
	dialog = ProgressDialog.show(BingoPhotoAlbumActivity.this, "","載入中,請稍等···");
		new Thread() {
			@Override
			public void run() {
		
		//List list01 = new ArrayList();
		Bitmap bitmap;
		
		for (int i = 0; i < list.size(); i++) {
			
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(list.get(i), opts);
		
		opts.inSampleSize = new ImageMemory().computeSampleSize(opts, -1, 110 * 110);
		opts.inJustDecodeBounds = false;

		Bitmap bmp = BitmapFactory.decodeFile(list.get(i), opts);

		ImageDispose iDispose= new ImageDispose();
		
		bitmap = iDispose.zoomBitmap(bmp, 110, 110);
		listB.add(bitmap);
		
		}
		Message message = new Message();
		message.what = 1;
		handler0.sendMessage(message);
			}
		}.start();
		
		
	}
	private Handler handler0 = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				adapter = new ImageList(context);
				photoalbum.setAdapter(adapter);
				dialog.dismiss();
				break;
			case -1:
				break;
			}
		};
	};		

	/**
	 * 
	 * @return
	 */
	private List<String> getSD() {
		/* 設定目前所在路徑 */
		List<String> it = new ArrayList<String>();
		File f = new File(new JudgePath().getPath()+"/dcim/Camera");
		//File f = new File(path.getPath() );
		if (f.listFiles() != null) {
			File[] files = f.listFiles();

			/* 將所有檔案存入ArrayList中 */

			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				if (getImageFile(file.getPath()))
					it.add(file.getPath());
			}
		}

		return it;
	}

	private boolean getImageFile(String fName) {
		boolean re;

		/* 取得副檔名 */
		String end = fName
				.substring(fName.lastIndexOf(".") + 1, fName.length())
				.toLowerCase();

		/* 按副檔名的型別決定MimeType */
		if (end.equals("jpg") || end.equals("gif") || end.equals("png")
				|| end.equals("jpeg") || end.equals("bmp")) {
			re = true;
		} else {
			re = false;
		}
		return re;
	}

	

}