关闭

canvas绘制bitmap1--两个bitmap交集裁剪等效果

标签: Android位图bitmap
975人阅读 评论(0) 收藏 举报
分类:


package com.example.xfermodesdemo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Xfermode;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {
		private static final int W = 64;// 宽64
		private static final int H = 64;// 高64
		private static final int ROW_MAX = 4;// 每行4个

		private Bitmap mSrcB;
		private Bitmap mDstB;
		private Shader mBG; // 背景 是跳棋盘 样式 checker-board pattern

		private static final Xfermode[] sModes = {
				new PorterDuffXfermode(PorterDuff.Mode.CLEAR),
				new PorterDuffXfermode(PorterDuff.Mode.SRC),
				new PorterDuffXfermode(PorterDuff.Mode.DST),
				new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER),
				new PorterDuffXfermode(PorterDuff.Mode.DST_OVER),
				new PorterDuffXfermode(PorterDuff.Mode.SRC_IN),
				new PorterDuffXfermode(PorterDuff.Mode.DST_IN),
				new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT),
				new PorterDuffXfermode(PorterDuff.Mode.DST_OUT),
				new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP),
				new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP),
				new PorterDuffXfermode(PorterDuff.Mode.XOR),
				new PorterDuffXfermode(PorterDuff.Mode.DARKEN),
				new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN),
				new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY),
				new PorterDuffXfermode(PorterDuff.Mode.SCREEN) };
		private static final String[] sLabels = { "Clear", "Src", "Dst",
				"SrcOver", "DstOver", "SrcIn", "DstIn", "SrcOut", "DstOut",
				"SrcATop", "DstATop", "Xor", "Darken", "Lighten", "Multiply",
				"Screen" };

		public SampleView(Context context) {
			super(context);

			mSrcB = makeSrc(W, H);
			mDstB = makeDst(W, H);

			// 棋盘样式背景
			Bitmap bm = Bitmap.createBitmap(new int[] { 0xFFFFFFFF, 0xFFCCCCCC,
					0xFFCCCCCC, 0xFFFFFFFF }, 2, 2, Bitmap.Config.RGB_565);

			mBG = new BitmapShader(bm, Shader.TileMode.REPEAT,
					Shader.TileMode.REPEAT);

			Matrix m = new Matrix();
			m.setScale(6, 6);// 缩放
			mBG.setLocalMatrix(m);

		}

		@SuppressLint("DrawAllocation")
		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawColor(Color.WHITE);

			Paint labelP = new Paint(Paint.ANTI_ALIAS_FLAG);
			labelP.setTextAlign(Paint.Align.CENTER);

			Paint paint = new Paint();
			paint.setFilterBitmap(false);

			canvas.translate(15, 35);
			int x = 0;
			int y = 0;
			for (int i = 0; i < sModes.length; i++) {
				// 绘制边界
				paint.setStyle(Paint.Style.STROKE);// 仅描边
				paint.setShader(null);
				canvas.drawRect(x - 0.5f, y - 0.5f, x + W + 0.5f, y + H + 0.5f,
						paint);

				// 绘制棋盘样式
				paint.setStyle(Paint.Style.FILL);// 完全填充
				paint.setShader(mBG);
				canvas.drawRect(x, y, x + W, y + H, paint);

				// 绘制src/des 到屏幕中
				int sc = canvas.saveLayer(x, y, x + W, y + H, null,
						Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG
								| Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
								| Canvas.FULL_COLOR_LAYER_SAVE_FLAG
								| Canvas.CLIP_TO_LAYER_SAVE_FLAG);
				canvas.translate(x, y);
				canvas.drawBitmap(mDstB, 0, 0, paint);
				paint.setXfermode(sModes[i]);
				canvas.drawBitmap(mSrcB, 0, 0, paint);
				paint.setXfermode(null);
				canvas.restoreToCount(sc);

				// 绘制文字
				canvas.drawText(sLabels[i], x + W / 2, y - labelP.getTextSize()
						/ 2, labelP);

				x += W + 10;

				// wrap around when we've drawn enough for one row
				if ((i % ROW_MAX) == ROW_MAX - 1) {
					x = 0;
					y += H + 30;
				}
			}
		}
	}

	/** 创建一个bitmap */
	static Bitmap makeDst(int w, int h) {
		Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		Canvas c = new Canvas(bm);
		Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);// 抗锯齿
		p.setColor(0xFFFFCC44);
		c.drawOval(new RectF(0, 0, w * 3 / 4, h * 3 / 4), p);// 绘制椭圆
		return bm;
	}

	/** 创建一个矩形的bitmap */
	static Bitmap makeSrc(int w, int h) {
		Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		Canvas c = new Canvas(bm);// bitmap中的画布
		Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

		p.setColor(0xFF66AAFF);
		c.drawRect(w / 3, h / 3, w * 19 / 20, h * 19 / 20, p);// 画矩形
		return bm;
	}

}

结果图:


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:48398次
    • 积分:1611
    • 等级:
    • 排名:千里之外
    • 原创:113篇
    • 转载:36篇
    • 译文:0篇
    • 评论:1条