Android 常用的画图方法

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);
	}
}

你可能感兴趣的:(android)