Android常用画図方法練習


CanvasView

package com.sonzer.CanvasDemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
/**
 *         
 * @author sonzer
 *
 */
public class CanvasView extends View {
	private Paint mPaint1 = null;
	private Bitmap mBitmap = null;

	public CanvasView(Context context) {
		super(context);
		//      
		mPaint1 = new Paint();
		mPaint1.setColor(Color.RED);//       
		mPaint1.setStrokeWidth(2);//       
		mPaint1.setAntiAlias(true);//      
		//     
		mBitmap = ((BitmapDrawable) getResources().getDrawable(
				R.drawable.sonzer_pic)).getBitmap();
		
		mBitmap = rotate(mBitmap, 10, 0, 0);//     
		mBitmap = scale(mBitmap, 0.3f, 0.3f, 0, 0);//     
		mBitmap = dispose(mBitmap, mBitmap.getWidth(), mBitmap.getHeight());//     
		//    (     )
		// canvas.setDrawFilter(new PaintFlagsDrawFilter(0,
		// Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
		
		alphaAnimation();//       
//		translateAnimation();//    
//		scaleAnimation();//    
//		rotateAnimation();//    
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawColor(Color.WHITE);//         
		canvas.clipRect(0, 0, 320, 480);//       

		canvas.drawLine(20, 20, 100, 30, mPaint1);//     
		canvas.drawPoint(60, 50, mPaint1);//    
		canvas.drawRect(new Rect(20, 70, 120, 100), mPaint1); //     
		canvas.drawRoundRect(new RectF(140, 70, 230, 100), 10, 10, mPaint1); //       
		canvas.drawCircle(50, 140, 30, mPaint1);//     
		canvas.drawArc(new RectF(140, 110, 230, 170), 180, 90, true, mPaint1);//     
		canvas.drawOval(new RectF(240, 110, 280, 170), mPaint1);//      
		canvas.drawText("    ", 20, 200, mPaint1);//     
		float[] points = new float[] { 35, 220, 20, 250, 20, 250, 50, 250, 35,
				220, 50, 250 };
		canvas.drawLines(points, mPaint1);//       ,  3  ,6   

		
		canvas.drawBitmap(mBitmap, 20, 270, null);//     

	}

	/**
	 *     
	 * 
	 * @param mBitmap
	 * @param angle
	 * @param px
	 * @param py
	 * @return
	 */
	public Bitmap rotate(Bitmap mBitmap, float angle, float px, float py) {
		Matrix matrix = new Matrix();
		matrix.reset();
		matrix.setRotate(angle, px, py);
		mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(),
				mBitmap.getHeight(), matrix, true);
		return mBitmap;
	}

	/**
	 *     
	 * 
	 * @param sx
	 * @param sy
	 * @param px
	 * @param py
	 * @return
	 */
	public Bitmap scale(Bitmap mBitmap, float sx, float sy, float px, float py) {
		Matrix matrix = new Matrix();
		matrix.reset();
		matrix.setScale(sx, sy, px, py);
		mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(),
				mBitmap.getHeight(), matrix, true);
		return mBitmap;
	}

	/**
	 *     
	 * 
	 * @param mBitmap
	 * @param width
	 * @param height
	 * @return
	 */
	public Bitmap dispose(Bitmap mBitmap, final int width, final int height) {
		int bitmap[] = new int[width * height];
		mBitmap.getPixels(bitmap, 0, width, 0, 0, width, height);
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				int index = i * width + j;
				//   RGB 
				int r = (bitmap[index] >> 16) & 0xff;
				int g = (bitmap[index] >> 8) & 0xff;
				int b = (bitmap[index]) & 0xff;
				//      
				double gray = r * 0.3 + g * 0.59 + b * 0.11;
				// 0xff000000 | (R << 16) | (G << 8) | B
				bitmap[index] = 0xff000000 | ((int) gray << 16)
						| ((int) gray << 8) | (int) gray;
			}
		}
		Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_4444);
		bm.setPixels(bitmap, 0, width, 0, 0, width, height);
		mBitmap = bm;
		bitmap = null;
		return mBitmap;
	}

	/**
	 *        
	 */
	public void alphaAnimation() {
		Animation alpha = new AlphaAnimation(0.0f, 1.0f);
		alpha.setDuration(3000);
		this.startAnimation(alpha);
	}

	/**
	 *     
	 */
	public void translateAnimation() {
		Animation translate = new TranslateAnimation(10, 100, 10, 100);

		translate.setDuration(3000);
		this.startAnimation(translate);
	}

	/**
	 *     
	 */
	public void scaleAnimation() {
		Animation scale = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		scale.setDuration(3000);
		this.startAnimation(scale);
	}

	/**
	 *     
	 */
	public void rotateAnimation() {
		Animation rotate = new RotateAnimation(0.0f, 360.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotate.setDuration(3000);
		this.startAnimation(rotate);
	}
}


CanvasDemoActivity

package com.sonzer.CanvasDemo;

import android.app.Activity;
import android.os.Bundle;

public class CanvasDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CanvasView view=new CanvasView(this);
        setContentView(view);
    }
}