Android:ImageView画像ズーム、中央揃え

5564 ワード

いくつかの重要な知識点:
1、レイアウトファイルのandroid:scaleType=「matrix」で画像のダイナミックスケーリングを設定する
2、matrix.postTranslate(dx,dy); 画像を移動します.postScale(sx,sy,p.x,p.y); 画像のズーム
3、2点の中点、距離の計算方式、スケーリング範囲の関数を制限して、指定の領域内でピクチャーの方法を移動します
(以下のコードはネット上で似たようなものが多く、参考までに)
public class Main extends Activity

{

	private static final int NONE = 0;

	private static final int DRAG = 1;

	private static final int ZOOM = 2;



	private int mode = NONE;

	private float oldDist;

	private Matrix matrix = new Matrix();

	private Matrix savedMatrix = new Matrix();

	private PointF start = new PointF();

	private PointF mid = new PointF();

	

	ImageView view;

	Bitmap bitmap;

	DisplayMetrics dm;

	float minScaleR=0.1f;  //      

	static final float MAX_SCALE = 4f; //      

	float dist = 1f;



	@Override

	public void onCreate(Bundle savedInstanceState)

	{

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		view = (ImageView) findViewById(R.id.image_view);

		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);

		

		dm = new DisplayMetrics();

		getWindowManager().getDefaultDisplay().getMetrics(dm); //     

		

		

		matrix.setScale(minScaleR, minScaleR); //     

		matrix.postTranslate(120, 120);		//        imageview           120   

		view.setImageMatrix(matrix);

		

		view.setOnTouchListener(new OnTouchListener()

		{

			@Override

			public boolean onTouch(View v, MotionEvent event)

			{

				ImageView view = (ImageView) v;

				switch (event.getAction() & MotionEvent.ACTION_MASK)

				{

				case MotionEvent.ACTION_DOWN:  //  

					Toast.makeText(Main.this, bitmap.getWidth()+"*"+bitmap.getHeight(), Toast.LENGTH_LONG).show();

					savedMatrix.set(matrix);

					start.set(event.getX(), event.getY());

					mode = DRAG;

					break;

				case MotionEvent.ACTION_UP:  //    

				case MotionEvent.ACTION_POINTER_UP:  //    

					mode = NONE;

					break;

				case MotionEvent.ACTION_POINTER_DOWN:  //  

					oldDist = spacing(event);

					if (oldDist > 10f)

					{

						savedMatrix.set(matrix);

						midPoint(mid, event);

						mode = ZOOM;

					}

					break;

				case MotionEvent.ACTION_MOVE: //      

					if (mode == DRAG)

					{

						matrix.set(savedMatrix);

						matrix.postTranslate(event.getX() - start.x,event.getY() - start.y);  //xy       

						//matrix.postTranslate(event.getX() - start.x,0); //  x                 

						//matrix.postTranslate(0,event.getY() - start.y);  //  y                 

					}

					else if (mode == ZOOM)

					{

						float newDist = spacing(event);

						if (newDist > 10f)

						{

							matrix.set(savedMatrix);

							float scale = newDist / oldDist;

							matrix.postScale(scale, scale, mid.x, mid.y);

						}

					}

					break;

				}

				

				view.setImageMatrix(matrix);

				

				CheckScale();  //      

				center();  //    

				

				return true;

			}



			

			//     

			private float spacing(MotionEvent event)

			{

				float x = event.getX(0) - event.getX(1);

				float y = event.getY(0) - event.getY(1);

				return FloatMath.sqrt(x * x + y * y);

			}

			

			//     

			private void midPoint(PointF point, MotionEvent event)

			{

				float x = event.getX(0) + event.getX(1);

				float y = event.getY(0) + event.getY(1);

				point.set(x / 2, y / 2);

			}

		});

	}

	

	//            

	protected void CheckScale()

	{

		float p[] = new float[9];

		matrix.getValues(p);

		if (mode == ZOOM)

		{

			if (p[0] < minScaleR)

			{

				matrix.setScale(minScaleR, minScaleR);

			}

			if (p[0] > MAX_SCALE)

			{

				matrix.set(savedMatrix);

			}

		}

	}



	//              

	protected void center()

	{

		center(true,true);

	}



	private void center(boolean horizontal, boolean vertical)

	{

		Matrix m = new Matrix();

		m.set(matrix);

		RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());

		m.mapRect(rect);

		float height = rect.height();

		float width = rect.width();

		float deltaX = 0, deltaY = 0;

		if (vertical)

		{

			//int screenHeight = dm.heightPixels;  //          

			int screenHeight = 400;

			if (height < screenHeight)

			{

				deltaY = (screenHeight - height)/2 - rect.top;

			}else if (rect.top > 0)

			{

				deltaY = -rect.top;

			}else if (rect.bottom < screenHeight)

			{

				deltaY = view.getHeight() - rect.bottom;

			}

		}

		

		if (horizontal)

		{

			//int screenWidth = dm.widthPixels;  //          

			int screenWidth = 400;

			if (width < screenWidth)

			{

				deltaX = (screenWidth - width)/2 - rect.left;

			}else if (rect.left > 0)

			{

				deltaX = -rect.left;	

			}else if (rect.right < screenWidth)

			{

				deltaX = screenWidth - rect.right;

			}

		}

		matrix.postTranslate(deltaX, deltaY);

	}

}
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <ImageView

        android:layout_width="200dip"

        android:layout_height="200dip"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:scaleType="matrix"

        android:src="@drawable/test"

        android:id="@+id/image_view">

    </ImageView>

</RelativeLayout>