Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kitegamesstudio.stickerdemo.view;
- 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.PointF;
- import android.graphics.RectF;
- import android.graphics.drawable.Drawable;
- import android.os.Build;
- import android.support.annotation.NonNull;
- import android.support.v4.content.ContextCompat;
- import android.support.v4.graphics.drawable.DrawableCompat;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import com.kitegamesstudio.stickerdemo.R;
- import com.kitegamesstudio.stickerdemo.util.DisplayUtil;
- import java.io.IOException;
- import java.io.InputStream;
- public class StickerView extends View {
- public static final float MAX_SCALE_SIZE = 3.2f;
- public static final float MIN_SCALE_SIZE = 0.1f;
- private int FOCUS_VIEW = 1;
- private float increaseTouchArea = (float) 0.0;
- private float[] mOriginPoints;
- private float[] mPoints;
- private RectF mOriginContentRect;
- private RectF mContentRect;
- private RectF mViewRect;
- private float mLastPointX, mLastPointY, mLastPointX1, mLastPointY1;
- private float startDistance, initRotate;
- private Bitmap mBitmap;
- private Bitmap mControllerBitmap, mDeleteBitmap, mInvertBitmap, mMoreBitmap;
- private Matrix mMatrix;
- private Paint mPaint, mBorderPaint;
- private float mControllerWidth, mControllerHeight, mDeleteWidth, mDeleteHeight, mInvertWidth, mInvertHeight, mMoreWidth, mMoreHeight;
- private boolean mInController, mInMove, mInDelete, mInMore, mInInvert;
- int count = 0;
- PointF midPoint = new PointF();
- int touchMoveCount = 0;
- private boolean mDrawController = true;
- //private boolean mCanTouch;
- private float mStickerScaleSize = 1.0f;
- private OnStickerListener mOnStickerListener;
- public StickerView(Context context) {
- this(context, null);
- }
- public StickerView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
- public StickerView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init();
- }
- private void init() {
- mPaint = new Paint();
- mPaint.setAntiAlias(true);
- mPaint.setFilterBitmap(true);
- mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setStrokeWidth(4.0f);
- mPaint.setColor(Color.WHITE);
- mBorderPaint = new Paint(mPaint);
- mBorderPaint.setColor(Color.parseColor("#B2ffffff"));
- mBorderPaint.setShadowLayer(DisplayUtil.dip2px(getContext(), 2.0f), 0, 0, Color.parseColor("#33000000"));
- mControllerBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_sticker_control);
- mControllerWidth = mControllerBitmap.getWidth();
- mControllerHeight = mControllerBitmap.getHeight();
- mDeleteBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.cancel);
- mDeleteWidth = mDeleteBitmap.getWidth();
- mDeleteHeight = mDeleteBitmap.getHeight();
- mInvertBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.invert);
- // mInvertBitmap = Bitmap.createScaledBitmap(mInvertBitmap, 70, 70, false);
- mInvertWidth = mInvertBitmap.getWidth();
- mInvertHeight = mInvertBitmap.getHeight();
- mMoreBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.round_add_button);
- // mMoreBitmap = Bitmap.createScaledBitmap(mMoreBitmap, 70, 70, false);
- mMoreWidth = mMoreBitmap.getWidth();
- mMoreHeight = mMoreBitmap.getHeight();
- /*InputStream inputStream = getContext().getResources().openRawResource(R.raw.ic_sticker_control);
- mControllerBitmap = BitmapFactory.decodeStream(inputStream);
- mControllerBitmap = Bitmap.createScaledBitmap(mControllerBitmap, 50, 50, true);
- mControllerWidth = mControllerBitmap.getWidth();
- mControllerHeight = mControllerBitmap.getHeight();
- Log.d("Bitmapsize"," W: "+mControllerWidth+" H: "+mControllerHeight);
- inputStream = getContext().getResources().openRawResource(R.raw.cancel);
- mDeleteBitmap = BitmapFactory.decodeStream(inputStream);
- mDeleteBitmap = Bitmap.createScaledBitmap(mDeleteBitmap, 50, 50, true);
- mDeleteWidth = mDeleteBitmap.getWidth();
- mDeleteHeight = mDeleteBitmap.getHeight();
- Log.d("Bitmapsize1"," W: "+mDeleteWidth+" H: "+mDeleteHeight);
- inputStream = getContext().getResources().openRawResource(R.raw.invert);
- mInvertBitmap = BitmapFactory.decodeStream(inputStream);
- mInvertBitmap = Bitmap.createScaledBitmap(mInvertBitmap, 50, 50, true);
- mInvertWidth = mInvertBitmap.getWidth();
- mInvertHeight = mInvertBitmap.getHeight();
- Log.d("Bitmapsize2"," W: "+mInvertWidth+" H: "+mInvertHeight);
- inputStream = getContext().getResources().openRawResource(R.raw.round_add_button);
- mMoreBitmap = BitmapFactory.decodeStream(inputStream);
- // mMoreBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.ic_round_add_button);
- // mMoreBitmap = getBitmapFromVectorDrawable(getContext(),R.drawable.ic_round_add_button);
- Log.d("Bitmapsize4"," W: "+mMoreWidth+" H: "+mMoreHeight);
- mMoreBitmap = Bitmap.createScaledBitmap(mMoreBitmap, 50, 50, true);
- mMoreWidth = mMoreBitmap.getWidth();
- mMoreHeight = mMoreBitmap.getHeight();
- Log.d("Bitmapsize3"," W: "+mMoreWidth+" H: "+mMoreHeight);*/
- mInDelete = false;
- }
- public void setWaterMark(@NonNull Bitmap bitmap) {
- mBitmap = bitmap;
- mStickerScaleSize = 1.0f;
- Log.d("bitmap", "W: " + mBitmap.getWidth() + " H: " + mBitmap.getHeight());
- setFocusable(true);
- try {
- float px = mBitmap.getWidth();
- float py = mBitmap.getHeight();
- //mOriginPoints = new float[]{px, py, px + bitmap.getWidth(), py, bitmap.getWidth() + px, bitmap.getHeight() + py, px, py + bitmap.getHeight()};
- mOriginPoints = new float[]{0, 0, px, 0, px, py, 0, py, px / 2, py / 2};
- mOriginContentRect = new RectF(0, 0, px, py);
- Log.d("rect", ""+mOriginContentRect);
- mPoints = new float[10];
- mContentRect = new RectF();
- mMatrix = new Matrix();
- float transtLeft = ((float) DisplayUtil.getDisplayWidthPixels(getContext()) - mBitmap.getWidth()) / 2;
- float transtTop = ((float) DisplayUtil.getDisplayWidthPixels(getContext()) - mBitmap.getHeight()) / 2;
- mMatrix.postTranslate(transtLeft, transtTop);
- } catch (Exception e) {
- e.printStackTrace();
- }
- postInvalidate();
- }
- public void setWaterMark(@NonNull Bitmap bitmap,Matrix matrix,RectF rectF,float[] origenalPoints) {
- mBitmap = bitmap;
- mStickerScaleSize = 1.0f;
- Log.d("bitmap", "W: " + mBitmap.getWidth() + " H: " + mBitmap.getHeight());
- setFocusable(true);
- try {
- float px = mBitmap.getWidth();
- float py = mBitmap.getHeight();
- mMatrix = new Matrix(matrix);
- float transtLeft = (float)(mBitmap.getWidth()*0.05);
- float transtTop = (float)(mBitmap.getHeight()*0.05);
- mMatrix.postTranslate(transtLeft, transtTop);
- //mOriginPoints = new float[]{px, py, px + bitmap.getWidth(), py, bitmap.getWidth() + px, bitmap.getHeight() + py, px, py + bitmap.getHeight()};
- mOriginPoints = new float[10];
- mOriginPoints = origenalPoints;
- mOriginContentRect = new RectF(rectF);
- Log.d("rect2", ""+mOriginContentRect);
- mPoints = new float[10];
- mContentRect = new RectF();
- } catch (Exception e) {
- e.printStackTrace();
- }
- postInvalidate();
- }
- public Matrix getMarkMatrix() {
- return mMatrix;
- }
- @Override
- public void setFocusable(boolean focusable) {
- super.setFocusable(focusable);
- postInvalidate();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- Log.d("draw", "Draw.... " + count++);
- super.onDraw(canvas);
- if (mBitmap == null || mMatrix == null) {
- return;
- }
- Log.d("point","->"+mOriginPoints[0]+" "+mOriginPoints[1]+" "+mOriginPoints[4]+" "+mOriginPoints[5]+" "+mOriginPoints[2]+" "+mOriginPoints[3]);
- Log.d("point","#>"+mPoints[0]+" "+mPoints[1]+" "+mPoints[4]+" "+mPoints[5]+" "+mPoints[2]+" "+mPoints[3]);
- mMatrix.mapPoints(mPoints, mOriginPoints);
- Log.d("point","->"+mOriginPoints[0]+" "+mOriginPoints[1]+" "+mOriginPoints[4]+" "+mOriginPoints[5]+" "+mOriginPoints[2]+" "+mOriginPoints[3]);
- Log.d("isRect2","( "+mPoints[0]+","+mPoints[1]+" )"+"( "+mPoints[2]+","+mPoints[3]+" )"
- +"( "+mPoints[4]+","+mPoints[5]+" )"+"( "+mPoints[6]+","+mPoints[7]+" )"
- +"( "+mPoints[8]+","+mPoints[9]+" )");
- Log.d("rect5"," -> "+mOriginContentRect);
- Log.d("rect5"," #> "+mContentRect);
- mMatrix.mapRect(mContentRect, mOriginContentRect);
- Log.d("rect56"," -> "+mOriginContentRect);
- Log.d("rect56"," #> "+mContentRect);
- canvas.drawBitmap(mBitmap, mMatrix, mPaint);
- if (mDrawController && isFocusable()) {
- canvas.drawLine(mPoints[0], mPoints[1], mPoints[2], mPoints[3], mBorderPaint);
- canvas.drawLine(mPoints[2], mPoints[3], mPoints[4], mPoints[5], mBorderPaint);
- canvas.drawLine(mPoints[4], mPoints[5], mPoints[6], mPoints[7], mBorderPaint);
- canvas.drawLine(mPoints[6], mPoints[7], mPoints[0], mPoints[1], mBorderPaint);
- canvas.drawBitmap(mControllerBitmap, mPoints[4] - mControllerWidth / 2, mPoints[5] - mControllerHeight / 2, mBorderPaint);
- canvas.drawBitmap(mDeleteBitmap, mPoints[0] - mDeleteWidth / 2, mPoints[1] - mDeleteHeight / 2, mBorderPaint);
- canvas.drawBitmap(mInvertBitmap, mPoints[6] - mInvertWidth / 2, mPoints[7] - mInvertHeight / 2, mBorderPaint);
- canvas.drawBitmap(mMoreBitmap, mPoints[2] - mMoreWidth / 2, mPoints[3] - mMoreHeight / 2, mBorderPaint);
- }
- }
- public Bitmap getBitmap() {
- Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
- Canvas canvas = new Canvas(bitmap);
- mDrawController = false;
- draw(canvas);
- mDrawController = true;
- canvas.save();
- return bitmap;
- }
- public void setShowDrawController(boolean show) {
- mDrawController = show;
- }
- private boolean isInController(float x, float y) {
- int position = 4;
- //while (position < 8) {
- float rx = mPoints[position];
- float ry = mPoints[position + 1];
- RectF rectF = new RectF(rx - (mControllerWidth )-increaseTouchArea,
- ry - (mControllerHeight)-increaseTouchArea,
- rx + (mControllerWidth)+increaseTouchArea,
- ry + (mControllerHeight )+increaseTouchArea);
- if (rectF.contains(x, y)) {
- return true;
- }
- // position += 2;
- //}
- return false;
- }
- private boolean isInDelete(float x, float y) {
- int position = 0;
- //while (position < 8) {
- float rx = mPoints[position];
- float ry = mPoints[position + 1];
- RectF rectF = new RectF(rx - (mDeleteWidth)-increaseTouchArea,
- ry - (mDeleteHeight )-increaseTouchArea,
- rx + (mDeleteWidth)+increaseTouchArea,
- ry + (mDeleteHeight)+increaseTouchArea);
- if (rectF.contains(x, y)) {
- return true;
- }
- // position += 2;
- //}
- return false;
- }
- private boolean isInvert(float x, float y) {
- int position = 6;
- float rx = mPoints[position];
- float ry = mPoints[position + 1];
- RectF rectF = new RectF(rx - (mInvertWidth)-increaseTouchArea,
- ry - (mInvertHeight)-increaseTouchArea,
- rx + (mInvertWidth)+increaseTouchArea,
- ry + (mInvertHeight)+increaseTouchArea);
- if (rectF.contains(x, y)) {
- return true;
- }
- return false;
- }
- private boolean isMoreSticker(float x, float y) {
- int position = 2;
- float rx = mPoints[position];
- float ry = mPoints[position + 1];
- RectF rectF = new RectF(rx - (mMoreWidth)-increaseTouchArea,
- ry - (mMoreWidth)-increaseTouchArea,
- rx + (mMoreWidth )+increaseTouchArea,
- ry + (mMoreWidth)+increaseTouchArea);
- if (rectF.contains(x, y)) {
- return true;
- }
- return false;
- }
- @Override
- public boolean dispatchTouchEvent(MotionEvent event) {
- Log.e("touch", "InDispatchTouch.....#");
- // Log.d("focus",""+getFocusable());
- /*if(getFocusable()<1){
- FOCUS_VIEW = 0;
- }*/
- if (!isFocusable()) {
- FOCUS_VIEW = 0;
- return super.dispatchTouchEvent(event);
- }
- if (mViewRect == null) {
- mViewRect = new RectF(0f, 0f, getMeasuredWidth(), getMeasuredHeight());
- }
- if (event.getPointerCount() == 1) {
- float x = event.getX();
- float y = event.getY();
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- touchMoveCount = 1;
- if (isInController(x, y)&& FOCUS_VIEW == 1) {
- mInController = true;
- mLastPointY = y;
- mLastPointX = x;
- break;
- }
- if (isInDelete(x, y)&& FOCUS_VIEW == 1) {
- mInDelete = true;
- break;
- }
- if (isInvert(x, y)&& FOCUS_VIEW == 1) {
- mInInvert = true;
- }
- if (isMoreSticker(x, y)&& FOCUS_VIEW == 1) {
- mInMore = true;
- }
- if (mContentRect.contains(x, y)) {
- mLastPointY = y;
- mLastPointX = x;
- mInMove = true;
- }
- break;
- case MotionEvent.ACTION_UP:
- Log.d("touchMoveCount",""+touchMoveCount);
- touchMoveCount = 0;
- if (isInDelete(x, y) && mInDelete && FOCUS_VIEW == 1) {
- Log.d("delete", "Now delete....");
- doDeleteSticker();
- }
- if (isInvert(x, y) && mInInvert && FOCUS_VIEW == 1) {
- flipBitmap();
- invalidate();
- }
- if (isMoreSticker(x, y) && mInMore && FOCUS_VIEW == 1) {
- doMoreSticker();
- }
- case MotionEvent.ACTION_CANCEL:
- mLastPointX = 0;
- mLastPointY = 0;
- mInController = false;
- mInMove = false;
- mInDelete = false;
- mInInvert = false;
- break;
- case MotionEvent.ACTION_MOVE:
- if (mInController && touchMoveCount>8 && FOCUS_VIEW == 1) {
- Log.e("control", "one finger scale");
- mMatrix.postRotate(rotation(event), mPoints[8], mPoints[9]);
- float nowLenght = caculateLength(mPoints[0], mPoints[1]);
- float touchLenght = caculateLength(event.getX(), event.getY());
- if (Math.sqrt((nowLenght - touchLenght) * (nowLenght - touchLenght)) > 0.0f) {
- float scale = touchLenght / nowLenght;
- float nowsc = mStickerScaleSize * scale;
- if (nowsc >= MIN_SCALE_SIZE && nowsc <= MAX_SCALE_SIZE) {
- mMatrix.postScale(scale, scale, mPoints[8], mPoints[9]);
- mStickerScaleSize = nowsc;
- }
- }
- Log.e("Scale", " x1: " + mPoints[0] + " y1: " + mPoints[1] + " x2: " + mPoints[4] + " y2: " + mPoints[5]);
- invalidate();
- mLastPointX = x;
- mLastPointY = y;
- break;
- }
- if (mInMove == true) { //拖动的操作
- float cX = x - mLastPointX;
- float cY = y - mLastPointY;
- mInController = false;
- //Log.i("MATRIX_OK", "ma_jiaodu:" + a(cX, cY));
- if (Math.sqrt(cX * cX + cY * cY) > 2.0f && canStickerMove(cX, cY)) {
- //Log.i("MATRIX_OK", "is true to move");
- mMatrix.postTranslate(cX, cY);
- postInvalidate();
- mLastPointX = x;
- mLastPointY = y;
- }
- break;
- }
- Log.d("rect3"," -> "+mOriginContentRect);
- Log.d("rect3"," #> "+mContentRect);
- touchMoveCount++;
- return true;
- }
- } else if (event.getPointerCount() == 2) {
- Log.e("control", "two finger");
- float x1 = event.getX(0);
- float y1 = event.getY(0);
- float x2 = event.getX(1);
- float y2 = event.getY(1);
- float deltaX = event.getX(0) - event.getX(1);
- float deltaY = event.getY(0) - event.getY(1);
- double radians = Math.atan(deltaY / deltaX);
- //Convert to degrees
- int degrees = (int) (radians * 180 / Math.PI);
- switch (event.getActionMasked()) {
- case MotionEvent.ACTION_POINTER_DOWN:
- if (mContentRect.contains(x1, y1) && mContentRect.contains(x2, y2)) {
- Log.e("control", "two finger get permission yes");
- mLastPointY = y1;
- mLastPointX = x1;
- mLastPointX1 = x2;
- mLastPointY1 = y2;
- startDistance = twofingerLength(event);
- initRotate = degrees;
- midPoint(midPoint, event);
- mInController = true;
- }
- break;
- case MotionEvent.ACTION_POINTER_UP:
- startDistance = (float) 0.0;
- case MotionEvent.ACTION_CANCEL:
- mLastPointX = 0;
- mLastPointY = 0;
- mLastPointX1 = 0;
- mLastPointY1 = 0;
- mInController = false;
- mInMove = false;
- mInDelete = false;
- break;
- case MotionEvent.ACTION_MOVE:
- Log.e("control", "two finger moveing....");
- if (mInController) {
- Log.e("control", "two finger scale");
- if ((degrees - initRotate) > 45) {
- //Going CCW across the boundary
- mMatrix.postRotate(-5, mPoints[8], mPoints[9]);
- } else if ((degrees - initRotate) < -45) {
- //Going CW across the boundary
- mMatrix.postRotate(5, mPoints[8], mPoints[9]);
- } else if (Math.abs(degrees - initRotate) > 1) {
- //Normal rotation, rotate the difference
- mMatrix.postRotate(degrees - initRotate, mPoints[8], mPoints[9]);
- }
- float touchLenght = twofingerLength(event);
- if (Math.sqrt((startDistance - touchLenght) * (startDistance - touchLenght)) > 0.0f) {
- float scale = touchLenght / startDistance;
- float nowsc = mStickerScaleSize * scale;
- if (nowsc >= MIN_SCALE_SIZE && nowsc <= MAX_SCALE_SIZE) {
- mMatrix.postScale(scale, scale, midPoint.x, midPoint.y);
- Log.d("draw", "Touch.... " + count++);
- mStickerScaleSize = nowsc;
- }
- }
- invalidate();
- Log.d("rect3"," -> "+mOriginContentRect);
- Log.d("rect3"," #> "+mContentRect);
- mLastPointY = y1;
- mLastPointX = x1;
- mLastPointX1 = x2;
- mLastPointY1 = y2;
- startDistance = touchLenght;
- initRotate = degrees;
- break;
- }
- return true;
- }
- }
- Log.d("matrix",mMatrix.toString());
- if(isFocusable()){
- FOCUS_VIEW = 1;
- }
- return true;
- }
- private void doDeleteSticker() {
- setVisibility(View.GONE);
- if (mOnStickerListener != null) {
- mOnStickerListener.onDelete(this);
- }
- }
- private void doMoreSticker() {
- if (mOnStickerListener != null) {
- // float[] values = new float[9];
- // mMatrix.getValues(values);
- // float width = Math.abs(values[Matrix.MSCALE_X]) * mBitmap.getWidth();
- // float height = Math.abs(values[Matrix.MSCALE_Y]) * mBitmap.getHeight();
- // Bitmap bitmap = Bitmap.createScaledBitmap(mBitmap, (int) width, (int) height, true);
- // Bitmap bitmap = Bitmap.createBitmap(mBitmap,0,0,mBitmap.getWidth(),mBitmap.getHeight(),mMatrix,true);
- // Bitmap bitmap1 = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight());
- // Bitmap bitmap = getBitmap();
- // Log.d("bitmap1", "W: " + bitmap.getWidth() + " H: " + bitmap.getHeight());
- // mOnStickerListener.onMoreSticker(mBitmap);
- Log.d("rect1", ""+mOriginContentRect);
- mOnStickerListener.onMoreSticker(mBitmap,mMatrix,mOriginContentRect,mOriginPoints);
- // mOnStickerListener.onMoreSticker(this);
- }
- }
- private boolean canStickerMove(float cx, float cy) {
- float px = cx + mPoints[8];
- float py = cy + mPoints[9];
- if (mViewRect.contains(px, py)) {
- return true;
- } else {
- return false;
- }
- }
- private float caculateLength(float x, float y) {
- float ex = x - mPoints[8];
- float ey = y - mPoints[9];
- return (float) Math.sqrt(ex * ex + ey * ey);
- }
- private float twofingerLength(MotionEvent event) {
- float x = event.getX(0) - event.getX(1);
- float y = event.getY(0) - event.getY(1);
- return (float) Math.sqrt(x * x + y * y);
- }
- private float rotation(MotionEvent event) {
- float originDegree = calculateDegree(mLastPointX, mLastPointY);
- float nowDegree = calculateDegree(event.getX(), event.getY());
- return nowDegree - originDegree;
- }
- private float twoFingerRotation(MotionEvent event) {
- return -1;
- }
- private float calculateDegree(float x, float y) {
- double delta_x = x - mPoints[8];
- double delta_y = y - mPoints[9];
- double radians = Math.atan2(delta_y, delta_x);
- return (float) Math.toDegrees(radians);
- }
- public RectF getContentRect() {
- return mContentRect;
- }
- public void setOnStickerDeleteListener(OnStickerListener listener) {
- mOnStickerListener = listener;
- }
- public interface OnStickerListener {
- public void onDelete(StickerView stickerView);
- // public void onMoreSticker(Bitmap stickerBitmap);
- public void onMoreSticker(Bitmap stickerBitmap,Matrix matrix,RectF rectF,float[] origenalPoints);
- // public void onMoreSticker(StickerView stickerView);
- }
- /**
- * Calculate the mid point of the first two fingers
- */
- private void midPoint(PointF point, MotionEvent event) {
- float x = event.getX(0) + event.getX(1);
- float y = event.getY(0) + event.getY(1);
- point.set(x / 2, y / 2);
- }
- public void flipBitmap() {
- Matrix matrix = new Matrix();
- matrix.preScale(-1.0f, 1.0f);
- mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
- }
- public boolean checkTouch(float x,float y){
- return check(mPoints[0],mPoints[1],mPoints[2],mPoints[3],mPoints[4],mPoints[5],mPoints[6],mPoints[7],x,y);
- }
- /* A utility function to calculate area of
- triangle formed by (x1, y1), (x2, y2) and
- (x3, y3) */
- static float area(float x1, float y1, float x2, float y2, float x3, float y3)
- {
- return (float)Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0);
- }
- /* A function to check whether point P(x, y)
- lies inside the rectangle formed by A(x1, y1),
- B(x2, y2), C(x3, y3) and D(x4, y4) */
- static boolean check(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float x, float y)
- {
- /* Calculate area of rectangle ABCD */
- float A = area(x1, y1, x2, y2, x3, y3)+ area(x1, y1, x4, y4, x3, y3);
- /* Calculate area of triangle PAB */
- float A1 = area(x, y, x1, y1, x2, y2);
- /* Calculate area of triangle PBC */
- float A2 = area(x, y, x2, y2, x3, y3);
- /* Calculate area of triangle PCD */
- float A3 = area(x, y, x3, y3, x4, y4);
- /* Calculate area of triangle PAD */
- float A4 = area(x, y, x1, y1, x4, y4);
- /* Check if sum of A1, A2, A3 and A4
- is same as A */
- // return (A == A1 + A2 + A3 + A4);
- Log.d("toucharea","#: "+A+" @: "+(A1 + A2 + A3 + A4)+" diff: "+Math.abs(A - (A1 + A2 + A3 + A4)));
- if(Math.abs(A - (A1 + A2 + A3 + A4))<15000.0) return true;
- else return false;
- }
- public Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
- Drawable drawable = ContextCompat.getDrawable(context, drawableId);
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
- drawable = (DrawableCompat.wrap(drawable)).mutate();
- }
- Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
- drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
- Canvas canvas = new Canvas(bitmap);
- drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
- drawable.draw(canvas);
- return bitmap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement