package com.chaoran.listener;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.chaoran.entiry.SelfImage;
import com.activity.chaoran.R;

public class ImageClickListener implements OnClickListener {
	private Activity activity;

	public ImageClickListener(Activity activity) {
		this.activity = activity;
	}

	@Override
	public void onClick(View v) {
		SelfImage img = (SelfImage) v;
		if(img.getBitmap()==null){
			return;
		}
		View contentView = LayoutInflater
				.from(activity.getApplicationContext()).inflate(R.layout.popup,
						null);
		// 设置popupWindow的背景颜色
		contentView.setBackgroundColor(Color.BLACK);
		// 声明一个弹出框
		final PopupWindow popupWindow = new PopupWindow(contentView,
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		popupWindow.setFocusable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());//加上这句返回键才对PopupWindow起作用
		popupWindow.showAtLocation(v, Gravity.CENTER_VERTICAL, 0, 0);
		Display display = activity.getWindowManager().getDefaultDisplay();
		ImageView imageView = (ImageView) contentView
				.findViewById(R.id.zoomimg);
		// int width = display.getWidth();
		// int height = display.getHeight();
		// //将图片设置为宽100，高200，在这儿就可以实现图片的大小缩放
		// Bitmap resize=Bitmap.createScaledBitmap(img.getBitmap(),
		// width,height, true);
		// Matrix m=new Matrix();
		// // m.setRotate(90); //逆时针旋转15度
		// // //做好旋转与大小之后，重新创建位图，0-width宽度上显示的区域，高度类似
		// Bitmap b=Bitmap.createBitmap(resize, 0, 0, resize.getWidth(),
		// resize.getHeight(),m, true);
		// 显示图片
		imageView.setImageBitmap(img.getBitmap());
		imageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
			}
		});
	}

}
