package com.supermap.customview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by Administrator on 2017/5/26 0026.
*/
public class GuaGuaKa extends View {
private Paint mOutPaint;
private Path mPath;
private Canvas mCanvas;
private Bitmap mBitmap;
private int mLastY;
private int mLastX;
private Bitmap bitmap;
public GuaGuaKa(Context context) {
this(context, null);
}
public GuaGuaKa(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GuaGuaKa(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
/**
* 1——进行一些初始化操作
*/
private void init() {
mOutPaint = new Paint();
mPath = new Path();
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.timg);
/**
* 设置画笔的属性
* */
// ctrl+alt+m抽取方法
setOutPaint();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
mLastX = x;
mLastY = y;
mPath.moveTo(mLastX, mLastY);
break;
case MotionEvent.ACTION_MOVE:
int dx = Math.abs(x - mLastX);
int dy = Math.abs(y - mLastY);
if (dx > 3 || dy > 3) {
mPath.lineTo(x, y);
}
mLastX = x;
mLastY = y;
break;
case MotionEvent.ACTION_UP:
break;
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(bitmap, 0, 0, null);
drawPath();
canvas.drawBitmap(mBitmap, 0, 0, null);
}
private void drawPath() {
mOutPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mCanvas.drawPath(mPath, mOutPaint);
}
/**
* 2——绘画
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
/**
* 初始化bitmap
*/
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
/**
* canvas的操作是在bitmap上
*/
mCanvas = new Canvas(mBitmap);
mCanvas.drawColor(Color.GRAY);
resetBitmapSize(width, height);
}
private void resetBitmapSize(int width, int height) {
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
float scale = bitmapWidth / bitmapHeight;
bitmap = scaleBitmap(bitmap, width, (int) scale * width);
}
private void setOutPaint() {
mOutPaint.setColor(Color.RED);
mOutPaint.setAntiAlias(true);
mOutPaint.setDither(true);
mOutPaint.setStrokeJoin(Paint.Join.ROUND);
mOutPaint.setStrokeCap(Paint.Cap.ROUND);
mOutPaint.setStyle(Paint.Style.STROKE);
mOutPaint.setStrokeWidth(10);
}
/**
* 根据给定的宽和高进行拉伸
*
* @param origin 原图
* @param newWidth 新图的宽
* @param newHeight 新图的高
* @return new Bitmap
*/
private Bitmap scaleBitmap(Bitmap origin, int newWidth, int newHeight) {
if (origin == null) {
return null;
}
int height = origin.getHeight();
int width = origin.getWidth();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);// 使用后乘
/**
* (Bitmap source, int x, int y, int width, int height,
Matrix m, boolean filter)
*/
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
if (!origin.isRecycled()) {
origin.recycle();
}
return newBM;
}
}
android刮刮卡效果
最新推荐文章于 2021-05-30 14:31:39 发布