package org.crazyit.imageview;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class ImageViewTest extends Activity
{
	//¶¨ÒåÒ»¸ö·ÃÎÊÍ¼Æ¬µÄÊý×é
	int[] images = new int[]{
		R.drawable.lijiang,
		R.drawable.qiao,
		R.drawable.shuangta,
		R.drawable.shui,
		R.drawable.xiangbi,
	};
	//¶¨ÒåÄ¬ÈÏÏÔÊ¾µÄÍ¼Æ¬
	int currentImg = 2;
	//¶¨ÒåÍ¼Æ¬µÄ³õÊ¼Í¸Ã÷¶È
	private int alpha = 255;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final Button plus = (Button)findViewById(R.id.plus);
		final Button minus = (Button)findViewById(R.id.minus);
		final ImageView image1 = (ImageView)findViewById(R.id.image1);
		final ImageView image2 = (ImageView)findViewById(R.id.image2);
		final Button next = (Button)findViewById(R.id.next);
		//¶¨Òå²é¿´ÏÂÒ»ÕÅÍ¼Æ¬µÄ¼àÌýÆ÷
		next.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (currentImg >= 4)
				{
					currentImg = -1;
				}
				BitmapDrawable bitmapDrawable = (BitmapDrawable) image1
					.getDrawable();
				//Èç¹ûÍ¼Æ¬»¹Î´»ØÊÕ£¬ÏÈÇ¿ÖÆ»ØÊÕ¸ÃÍ¼Æ¬
				if (!bitmapDrawable.getBitmap().isRecycled())
				{
					bitmapDrawable.getBitmap().recycle();
				}
				//¸Ä±äImageViewÏÔÊ¾µÄÍ¼Æ¬
				image1.setImageBitmap(BitmapFactory.decodeResource(getResources()
					, images[++currentImg]));
//				try
//				{
//					//¸Ä±äImageViewÀïÏÔÊ¾µÄÍ¼Æ¬
//					image1.setImageResource(images[++currentImg]);
//				}
//				//²¶×½ÄÚ´æÒç³öÒì³£
//				catch(OutOfMemoryError e)
//				{
//				}
			}
		});
		//¶¨Òå¸Ä±äÍ¼Æ¬Í¸Ã÷¶ÈµÄ·½·¨
		OnClickListener listener = new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if(v == plus)
				{
					alpha += 20;
				}
				if(v == minus)
				{
					alpha -= 20;
				}
				if(alpha >= 255)
				{
					alpha = 255;
				}
				if(alpha <= 0)
				{
					alpha = 0;
				}
				//¸Ä±äÍ¼Æ¬µÄÍ¸Ã÷¶È
				image1.setAlpha(alpha);				
			}
		};
		//ÎªÁ½¸ö°´Å¥Ìí¼Ó¼àÌýÆ÷
		plus.setOnClickListener(listener);
		minus.setOnClickListener(listener);
		image1.setOnTouchListener(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View view, MotionEvent event)
			{
				BitmapDrawable bitmapDrawable = (BitmapDrawable) image1
					.getDrawable();
				//»ñÈ¡µÚÒ»¸öÍ¼Æ¬ÏÔÊ¾¿òÖÐµÄÎ»Í¼
				Bitmap bitmap = bitmapDrawable.getBitmap();
				//bitmapÍ¼Æ¬Êµ¼Ê´óÐ¡ÓëµÚÒ»¸öImageViewµÄËõ·Å±ÈÀý
				double scale = bitmap.getWidth() / 320.0;
				//»ñÈ¡ÐèÒªÏÔÊ¾µÄÍ¼Æ¬µÄ¿ªÊ¼µã
				int x = (int) (event.getX() * scale);
				int y = (int) (event.getY() * scale);
				if (x  + 120 > bitmap.getWidth())
				{
					x = bitmap.getWidth() - 120;
				}
				if (y  + 120 > bitmap.getHeight())
				{
					y = bitmap.getHeight() - 120;
				}
				//ÏÔÊ¾Í¼Æ¬µÄÖ¸¶¨ÇøÓò
				image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));
				image2.setAlpha(alpha);
				return false;
			}
		});
	}
}