android自定義View實現圖片的繪製、旋轉、縮放

1、圖片

把一張JPG圖片改名爲image.jpg,然後拷貝到項目的res-drawable中。

2、activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button android:id="@+id/buttonLeft"
        android:text="圖片向左移動" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button android:id="@+id/buttonRight"
        android:text="圖片向右移動"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <Button android:id="@+id/buttonRotationLeft"
        android:text="圖片向左旋轉"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button android:id="@+id/buttonRotationRight"
        android:text="圖片向右旋轉"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button android:id="@+id/buttonNarrow"
        android:text="圖片縮小"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button android:id="@+id/buttonEnlarge"
        android:text="圖片放大"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
3、MainActivity.java類

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Canvas;
import android.widget.LinearLayout;
import android.widget.Button;

類的實現

public class MainActivity extends Activity {
	ImageView imageView = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//	動態加載圖片到LinearLayout中
		imageView = new ImageView(this);
		LinearLayout ll = (LinearLayout) findViewById(R.id.imageid);
		ll.addView(imageView);
		//	向左移動
		Button btnLeft = (Button) findViewById(R.id.buttonLeft);
		btnLeft.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				imageView.setPosLeft();
			}
		});
		//	向右移動
		Button btnRight = (Button) findViewById(R.id.buttonRight);
		btnRight.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				imageView.setPosRight();
			}
		});
		
		//	向左旋轉
		Button btnRotationLeft = (Button)findViewById(R.id.buttonRotationLeft);
		btnRotationLeft.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				imageView.setRotationLeft();
			}
		});
		//	向右旋轉
		Button btnRotationRight = (Button)findViewById(R.id.buttonRotationRight);
		btnRotationRight.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				imageView.setRotationRight();
			}
		});
		
		//	放大圖片
		Button btnEnlarge = (Button)findViewById(R.id.buttonEnlarge);
		btnEnlarge.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				imageView.setEnlarge();
			}
		});
		//	縮小圖片
		Button btnNarrow = (Button)findViewById(R.id.buttonNarrow);
		btnNarrow.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				imageView.setNarrow();
			}
		});
	}
	
	//	自定義圖片View
	class ImageView extends View {
		private Paint paint = null;	//	畫筆
		private Bitmap bitmap = null;	//	圖片位圖
		private Bitmap bitmapDisplay = null;
		private Matrix matrix = null;
		private int nBitmapWidth = 0;	//	圖片的寬度
		private int nBitmapHeight = 0;	//	圖片的高度
		private int nPosX = 120;	//	圖片所在的位置X
		private int nPosY = 10;	//	圖片所在的位置Y
		private float fAngle = 0.0f;	//	圖片旋轉
		private float fScale = 1.0f;	//	圖片縮放 1.0表示爲原圖
		
		public ImageView(Context context) {
			super(context);
			
			paint = new Paint();
			paint.setFlags(Paint.ANTI_ALIAS_FLAG);
			
			//	加載需要操作的圖片
			bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
			bitmapDisplay = bitmap;
			
			matrix = new Matrix();
			//	獲取圖片高度和寬度
			nBitmapWidth = bitmap.getWidth();
			nBitmapHeight = bitmap.getHeight();
		}
		
		//	向左移動
		public void setPosLeft() {
			nPosX -= 10; 
		}
		//	向右移動
		public void setPosRight() {
			nPosX += 10;
		}
		
		//	向左旋轉
		public void setRotationLeft() {
			fAngle--;
			setAngle();
		}
		//	向右旋轉
		public void setRotationRight() {
			fAngle++;
			setAngle();
		}
		
		//	圖片放大
		public void setEnlarge() {
			if (fScale < 2) {
				fScale += 0.1f;
				setScale();
			}
		}
		//	圖片縮小
		public void setNarrow() {
			if (fScale > 0.5) {
				fScale -= 0.1f;
				setScale();
			}
		}
		
		//	設置旋轉比例
		private void setAngle() {
			matrix.reset();
			matrix.setRotate(fAngle);
			bitmapDisplay = Bitmap.createBitmap(bitmap,0,0,nBitmapWidth,nBitmapHeight,matrix,true);
		}
		
		//	設置縮放比例
		private void setScale() {
			matrix.reset();
			matrix.postScale(fScale, fScale);
			bitmapDisplay = Bitmap.createBitmap(bitmap,0,0,nBitmapWidth,nBitmapHeight,matrix,true);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			canvas.drawBitmap(bitmapDisplay, nPosX, nPosY, paint);
			invalidate();
		}
	}
}

4、效果圖

效果圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章