Advertisement
ghorardim

StickerView

Sep 19th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 27.29 KB | None | 0 0
  1. package com.kitegamesstudio.stickerdemo.view;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.graphics.Matrix;
  9. import android.graphics.Paint;
  10. import android.graphics.PointF;
  11. import android.graphics.RectF;
  12. import android.graphics.drawable.Drawable;
  13. import android.os.Build;
  14. import android.support.annotation.NonNull;
  15. import android.support.v4.content.ContextCompat;
  16. import android.support.v4.graphics.drawable.DrawableCompat;
  17. import android.util.AttributeSet;
  18. import android.util.Log;
  19. import android.view.MotionEvent;
  20. import android.view.View;
  21.  
  22. import com.kitegamesstudio.stickerdemo.R;
  23. import com.kitegamesstudio.stickerdemo.util.DisplayUtil;
  24.  
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27.  
  28. public class StickerView extends View {
  29.     public static final float MAX_SCALE_SIZE = 3.2f;
  30.     public static final float MIN_SCALE_SIZE = 0.1f;
  31.     private int FOCUS_VIEW = 1;
  32.     private float increaseTouchArea = (float) 0.0;
  33.     private float[] mOriginPoints;
  34.     private float[] mPoints;
  35.     private RectF mOriginContentRect;
  36.     private RectF mContentRect;
  37.     private RectF mViewRect;
  38.  
  39.     private float mLastPointX, mLastPointY, mLastPointX1, mLastPointY1;
  40.     private float startDistance, initRotate;
  41.  
  42.     private Bitmap mBitmap;
  43.     private Bitmap mControllerBitmap, mDeleteBitmap, mInvertBitmap, mMoreBitmap;
  44.     private Matrix mMatrix;
  45.     private Paint mPaint, mBorderPaint;
  46.     private float mControllerWidth, mControllerHeight, mDeleteWidth, mDeleteHeight, mInvertWidth, mInvertHeight, mMoreWidth, mMoreHeight;
  47.     private boolean mInController, mInMove, mInDelete, mInMore, mInInvert;
  48.     int count = 0;
  49.     PointF midPoint = new PointF();
  50.     int touchMoveCount = 0;
  51.     private boolean mDrawController = true;
  52.     //private boolean mCanTouch;
  53.     private float mStickerScaleSize = 1.0f;
  54.  
  55.     private OnStickerListener mOnStickerListener;
  56.  
  57.     public StickerView(Context context) {
  58.         this(context, null);
  59.     }
  60.  
  61.  
  62.     public StickerView(Context context, AttributeSet attrs) {
  63.         this(context, attrs, 0);
  64.     }
  65.  
  66.     public StickerView(Context context, AttributeSet attrs, int defStyle) {
  67.         super(context, attrs, defStyle);
  68.         init();
  69.     }
  70.  
  71.     private void init() {
  72.  
  73.         mPaint = new Paint();
  74.         mPaint.setAntiAlias(true);
  75.         mPaint.setFilterBitmap(true);
  76.         mPaint.setStyle(Paint.Style.STROKE);
  77.         mPaint.setStrokeWidth(4.0f);
  78.         mPaint.setColor(Color.WHITE);
  79.  
  80.         mBorderPaint = new Paint(mPaint);
  81.         mBorderPaint.setColor(Color.parseColor("#B2ffffff"));
  82.         mBorderPaint.setShadowLayer(DisplayUtil.dip2px(getContext(), 2.0f), 0, 0, Color.parseColor("#33000000"));
  83.  
  84.         mControllerBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_sticker_control);
  85.         mControllerWidth = mControllerBitmap.getWidth();
  86.         mControllerHeight = mControllerBitmap.getHeight();
  87.  
  88.         mDeleteBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.cancel);
  89.         mDeleteWidth = mDeleteBitmap.getWidth();
  90.         mDeleteHeight = mDeleteBitmap.getHeight();
  91.  
  92.         mInvertBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.invert);
  93.     //    mInvertBitmap = Bitmap.createScaledBitmap(mInvertBitmap, 70, 70, false);
  94.         mInvertWidth = mInvertBitmap.getWidth();
  95.         mInvertHeight = mInvertBitmap.getHeight();
  96.  
  97.         mMoreBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.round_add_button);
  98.     //    mMoreBitmap = Bitmap.createScaledBitmap(mMoreBitmap, 70, 70, false);
  99.         mMoreWidth = mMoreBitmap.getWidth();
  100.         mMoreHeight = mMoreBitmap.getHeight();
  101.  
  102.         /*InputStream inputStream = getContext().getResources().openRawResource(R.raw.ic_sticker_control);
  103.         mControllerBitmap = BitmapFactory.decodeStream(inputStream);
  104.         mControllerBitmap = Bitmap.createScaledBitmap(mControllerBitmap, 50, 50, true);
  105.         mControllerWidth = mControllerBitmap.getWidth();
  106.         mControllerHeight = mControllerBitmap.getHeight();
  107.         Log.d("Bitmapsize"," W: "+mControllerWidth+" H: "+mControllerHeight);
  108.  
  109.         inputStream = getContext().getResources().openRawResource(R.raw.cancel);
  110.         mDeleteBitmap = BitmapFactory.decodeStream(inputStream);
  111.         mDeleteBitmap = Bitmap.createScaledBitmap(mDeleteBitmap, 50, 50, true);
  112.         mDeleteWidth = mDeleteBitmap.getWidth();
  113.         mDeleteHeight = mDeleteBitmap.getHeight();
  114.         Log.d("Bitmapsize1"," W: "+mDeleteWidth+" H: "+mDeleteHeight);
  115.  
  116.  
  117.         inputStream = getContext().getResources().openRawResource(R.raw.invert);
  118.         mInvertBitmap = BitmapFactory.decodeStream(inputStream);
  119.         mInvertBitmap = Bitmap.createScaledBitmap(mInvertBitmap, 50, 50, true);
  120.         mInvertWidth = mInvertBitmap.getWidth();
  121.         mInvertHeight = mInvertBitmap.getHeight();
  122.         Log.d("Bitmapsize2"," W: "+mInvertWidth+" H: "+mInvertHeight);
  123.  
  124.  
  125.  
  126.         inputStream = getContext().getResources().openRawResource(R.raw.round_add_button);
  127.         mMoreBitmap = BitmapFactory.decodeStream(inputStream);
  128.   //      mMoreBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.ic_round_add_button);
  129.      //   mMoreBitmap = getBitmapFromVectorDrawable(getContext(),R.drawable.ic_round_add_button);
  130.         Log.d("Bitmapsize4"," W: "+mMoreWidth+" H: "+mMoreHeight);
  131.         mMoreBitmap = Bitmap.createScaledBitmap(mMoreBitmap, 50, 50, true);
  132.         mMoreWidth = mMoreBitmap.getWidth();
  133.         mMoreHeight = mMoreBitmap.getHeight();
  134.         Log.d("Bitmapsize3"," W: "+mMoreWidth+" H: "+mMoreHeight);*/
  135.  
  136.         mInDelete = false;
  137.  
  138.  
  139.     }
  140.  
  141.     public void setWaterMark(@NonNull Bitmap bitmap) {
  142.         mBitmap = bitmap;
  143.         mStickerScaleSize = 1.0f;
  144.         Log.d("bitmap", "W: " + mBitmap.getWidth() + " H: " + mBitmap.getHeight());
  145.  
  146.         setFocusable(true);
  147.         try {
  148.  
  149.  
  150.             float px = mBitmap.getWidth();
  151.             float py = mBitmap.getHeight();
  152.  
  153.  
  154.             //mOriginPoints = new float[]{px, py, px + bitmap.getWidth(), py, bitmap.getWidth() + px, bitmap.getHeight() + py, px, py + bitmap.getHeight()};
  155.             mOriginPoints = new float[]{0, 0, px, 0, px, py, 0, py, px / 2, py / 2};
  156.             mOriginContentRect = new RectF(0, 0, px, py);
  157.             Log.d("rect", ""+mOriginContentRect);
  158.             mPoints = new float[10];
  159.             mContentRect = new RectF();
  160.  
  161.             mMatrix = new Matrix();
  162.             float transtLeft = ((float) DisplayUtil.getDisplayWidthPixels(getContext()) - mBitmap.getWidth()) / 2;
  163.             float transtTop = ((float) DisplayUtil.getDisplayWidthPixels(getContext()) - mBitmap.getHeight()) / 2;
  164.  
  165.             mMatrix.postTranslate(transtLeft, transtTop);
  166.  
  167.         } catch (Exception e) {
  168.             e.printStackTrace();
  169.         }
  170.         postInvalidate();
  171.  
  172.     }
  173.     public void setWaterMark(@NonNull Bitmap bitmap,Matrix matrix,RectF rectF,float[] origenalPoints) {
  174.         mBitmap = bitmap;
  175.         mStickerScaleSize = 1.0f;
  176.         Log.d("bitmap", "W: " + mBitmap.getWidth() + " H: " + mBitmap.getHeight());
  177.  
  178.         setFocusable(true);
  179.         try {
  180.  
  181.  
  182.             float px = mBitmap.getWidth();
  183.             float py = mBitmap.getHeight();
  184.  
  185.  
  186.             mMatrix = new Matrix(matrix);
  187.             float transtLeft = (float)(mBitmap.getWidth()*0.05);
  188.             float transtTop = (float)(mBitmap.getHeight()*0.05);
  189.  
  190.             mMatrix.postTranslate(transtLeft, transtTop);
  191.             //mOriginPoints = new float[]{px, py, px + bitmap.getWidth(), py, bitmap.getWidth() + px, bitmap.getHeight() + py, px, py + bitmap.getHeight()};
  192.             mOriginPoints = new float[10];
  193.             mOriginPoints = origenalPoints;
  194.             mOriginContentRect = new RectF(rectF);
  195.             Log.d("rect2", ""+mOriginContentRect);
  196.             mPoints = new float[10];
  197.             mContentRect = new RectF();
  198.  
  199.  
  200.         } catch (Exception e) {
  201.             e.printStackTrace();
  202.         }
  203.         postInvalidate();
  204.  
  205.     }
  206.  
  207.     public Matrix getMarkMatrix() {
  208.         return mMatrix;
  209.     }
  210.  
  211.     @Override
  212.     public void setFocusable(boolean focusable) {
  213.         super.setFocusable(focusable);
  214.         postInvalidate();
  215.     }
  216.  
  217.     @Override
  218.     protected void onDraw(Canvas canvas) {
  219.         Log.d("draw", "Draw.... " + count++);
  220.         super.onDraw(canvas);
  221.         if (mBitmap == null || mMatrix == null) {
  222.             return;
  223.         }
  224.         Log.d("point","->"+mOriginPoints[0]+" "+mOriginPoints[1]+" "+mOriginPoints[4]+" "+mOriginPoints[5]+" "+mOriginPoints[2]+" "+mOriginPoints[3]);
  225.         Log.d("point","#>"+mPoints[0]+" "+mPoints[1]+" "+mPoints[4]+" "+mPoints[5]+" "+mPoints[2]+" "+mPoints[3]);
  226.         mMatrix.mapPoints(mPoints, mOriginPoints);
  227.         Log.d("point","->"+mOriginPoints[0]+" "+mOriginPoints[1]+" "+mOriginPoints[4]+" "+mOriginPoints[5]+" "+mOriginPoints[2]+" "+mOriginPoints[3]);
  228.         Log.d("isRect2","( "+mPoints[0]+","+mPoints[1]+" )"+"( "+mPoints[2]+","+mPoints[3]+" )"
  229.                 +"( "+mPoints[4]+","+mPoints[5]+" )"+"( "+mPoints[6]+","+mPoints[7]+" )"
  230.                 +"( "+mPoints[8]+","+mPoints[9]+" )");
  231.         Log.d("rect5"," -> "+mOriginContentRect);
  232.         Log.d("rect5"," #> "+mContentRect);
  233.         mMatrix.mapRect(mContentRect, mOriginContentRect);
  234.         Log.d("rect56"," -> "+mOriginContentRect);
  235.         Log.d("rect56"," #> "+mContentRect);
  236.         canvas.drawBitmap(mBitmap, mMatrix, mPaint);
  237.         if (mDrawController && isFocusable()) {
  238.             canvas.drawLine(mPoints[0], mPoints[1], mPoints[2], mPoints[3], mBorderPaint);
  239.             canvas.drawLine(mPoints[2], mPoints[3], mPoints[4], mPoints[5], mBorderPaint);
  240.             canvas.drawLine(mPoints[4], mPoints[5], mPoints[6], mPoints[7], mBorderPaint);
  241.             canvas.drawLine(mPoints[6], mPoints[7], mPoints[0], mPoints[1], mBorderPaint);
  242.             canvas.drawBitmap(mControllerBitmap, mPoints[4] - mControllerWidth / 2, mPoints[5] - mControllerHeight / 2, mBorderPaint);
  243.             canvas.drawBitmap(mDeleteBitmap, mPoints[0] - mDeleteWidth / 2, mPoints[1] - mDeleteHeight / 2, mBorderPaint);
  244.             canvas.drawBitmap(mInvertBitmap, mPoints[6] - mInvertWidth / 2, mPoints[7] - mInvertHeight / 2, mBorderPaint);
  245.             canvas.drawBitmap(mMoreBitmap, mPoints[2] - mMoreWidth / 2, mPoints[3] - mMoreHeight / 2, mBorderPaint);
  246.         }
  247.     }
  248.  
  249.     public Bitmap getBitmap() {
  250.         Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
  251.         Canvas canvas = new Canvas(bitmap);
  252.         mDrawController = false;
  253.         draw(canvas);
  254.         mDrawController = true;
  255.         canvas.save();
  256.         return bitmap;
  257.     }
  258.  
  259.     public void setShowDrawController(boolean show) {
  260.         mDrawController = show;
  261.     }
  262.  
  263.     private boolean isInController(float x, float y) {
  264.         int position = 4;
  265.         //while (position < 8) {
  266.         float rx = mPoints[position];
  267.         float ry = mPoints[position + 1];
  268.         RectF rectF = new RectF(rx - (mControllerWidth )-increaseTouchArea,
  269.                 ry - (mControllerHeight)-increaseTouchArea,
  270.                 rx + (mControllerWidth)+increaseTouchArea,
  271.                 ry + (mControllerHeight )+increaseTouchArea);
  272.         if (rectF.contains(x, y)) {
  273.             return true;
  274.         }
  275.         //   position += 2;
  276.         //}
  277.         return false;
  278.  
  279.     }
  280.  
  281.     private boolean isInDelete(float x, float y) {
  282.         int position = 0;
  283.         //while (position < 8) {
  284.         float rx = mPoints[position];
  285.         float ry = mPoints[position + 1];
  286.         RectF rectF = new RectF(rx - (mDeleteWidth)-increaseTouchArea,
  287.                 ry - (mDeleteHeight )-increaseTouchArea,
  288.                 rx + (mDeleteWidth)+increaseTouchArea,
  289.                 ry + (mDeleteHeight)+increaseTouchArea);
  290.         if (rectF.contains(x, y)) {
  291.             return true;
  292.         }
  293.         //   position += 2;
  294.         //}
  295.         return false;
  296.  
  297.     }
  298.  
  299.     private boolean isInvert(float x, float y) {
  300.         int position = 6;
  301.         float rx = mPoints[position];
  302.         float ry = mPoints[position + 1];
  303.         RectF rectF = new RectF(rx - (mInvertWidth)-increaseTouchArea,
  304.                 ry - (mInvertHeight)-increaseTouchArea,
  305.                 rx + (mInvertWidth)+increaseTouchArea,
  306.                 ry + (mInvertHeight)+increaseTouchArea);
  307.         if (rectF.contains(x, y)) {
  308.             return true;
  309.         }
  310.         return false;
  311.     }
  312.  
  313.     private boolean isMoreSticker(float x, float y) {
  314.         int position = 2;
  315.         float rx = mPoints[position];
  316.         float ry = mPoints[position + 1];
  317.         RectF rectF = new RectF(rx - (mMoreWidth)-increaseTouchArea,
  318.                 ry - (mMoreWidth)-increaseTouchArea,
  319.                 rx + (mMoreWidth )+increaseTouchArea,
  320.                 ry + (mMoreWidth)+increaseTouchArea);
  321.         if (rectF.contains(x, y)) {
  322.             return true;
  323.         }
  324.         return false;
  325.     }
  326.  
  327.     @Override
  328.     public boolean dispatchTouchEvent(MotionEvent event) {
  329.         Log.e("touch", "InDispatchTouch.....#");
  330.  
  331.    //     Log.d("focus",""+getFocusable());
  332.         /*if(getFocusable()<1){
  333.             FOCUS_VIEW = 0;
  334.         }*/
  335.  
  336.         if (!isFocusable()) {
  337.             FOCUS_VIEW = 0;
  338.             return super.dispatchTouchEvent(event);
  339.         }
  340.  
  341.         if (mViewRect == null) {
  342.             mViewRect = new RectF(0f, 0f, getMeasuredWidth(), getMeasuredHeight());
  343.         }
  344.  
  345.         if (event.getPointerCount() == 1) {
  346.             float x = event.getX();
  347.             float y = event.getY();
  348.             switch (event.getAction()) {
  349.                 case MotionEvent.ACTION_DOWN:
  350.                     touchMoveCount = 1;
  351.                     if (isInController(x, y)&& FOCUS_VIEW == 1) {
  352.                         mInController = true;
  353.                         mLastPointY = y;
  354.                         mLastPointX = x;
  355.                         break;
  356.                     }
  357.  
  358.                     if (isInDelete(x, y)&& FOCUS_VIEW == 1) {
  359.                         mInDelete = true;
  360.                         break;
  361.                     }
  362.  
  363.                     if (isInvert(x, y)&& FOCUS_VIEW == 1) {
  364.                         mInInvert = true;
  365.                     }
  366.  
  367.                     if (isMoreSticker(x, y)&& FOCUS_VIEW == 1) {
  368.                         mInMore = true;
  369.                     }
  370.  
  371.                     if (mContentRect.contains(x, y)) {
  372.                         mLastPointY = y;
  373.                         mLastPointX = x;
  374.                         mInMove = true;
  375.                     }
  376.                     break;
  377.                 case MotionEvent.ACTION_UP:
  378.                     Log.d("touchMoveCount",""+touchMoveCount);
  379.                     touchMoveCount = 0;
  380.                     if (isInDelete(x, y) && mInDelete && FOCUS_VIEW == 1) {
  381.                         Log.d("delete", "Now delete....");
  382.                         doDeleteSticker();
  383.                     }
  384.                     if (isInvert(x, y) && mInInvert && FOCUS_VIEW == 1) {
  385.                         flipBitmap();
  386.                         invalidate();
  387.                     }
  388.                     if (isMoreSticker(x, y) && mInMore && FOCUS_VIEW == 1) {
  389.                         doMoreSticker();
  390.                     }
  391.                 case MotionEvent.ACTION_CANCEL:
  392.                     mLastPointX = 0;
  393.                     mLastPointY = 0;
  394.                     mInController = false;
  395.                     mInMove = false;
  396.                     mInDelete = false;
  397.                     mInInvert = false;
  398.                     break;
  399.                 case MotionEvent.ACTION_MOVE:
  400.  
  401.                     if (mInController && touchMoveCount>8 && FOCUS_VIEW == 1) {
  402.                         Log.e("control", "one finger scale");
  403.  
  404.                         mMatrix.postRotate(rotation(event), mPoints[8], mPoints[9]);
  405.                         float nowLenght = caculateLength(mPoints[0], mPoints[1]);
  406.                         float touchLenght = caculateLength(event.getX(), event.getY());
  407.                         if (Math.sqrt((nowLenght - touchLenght) * (nowLenght - touchLenght)) > 0.0f) {
  408.                             float scale = touchLenght / nowLenght;
  409.                             float nowsc = mStickerScaleSize * scale;
  410.                             if (nowsc >= MIN_SCALE_SIZE && nowsc <= MAX_SCALE_SIZE) {
  411.                                 mMatrix.postScale(scale, scale, mPoints[8], mPoints[9]);
  412.                                 mStickerScaleSize = nowsc;
  413.                             }
  414.                         }
  415.                         Log.e("Scale", " x1: " + mPoints[0] + " y1: " + mPoints[1] + " x2: " + mPoints[4] + " y2: " + mPoints[5]);
  416.                         invalidate();
  417.                         mLastPointX = x;
  418.                         mLastPointY = y;
  419.                         break;
  420.  
  421.                     }
  422.  
  423.                     if (mInMove == true) { //拖动的操作
  424.                         float cX = x - mLastPointX;
  425.                         float cY = y - mLastPointY;
  426.                         mInController = false;
  427.                         //Log.i("MATRIX_OK", "ma_jiaodu:" + a(cX, cY));
  428.  
  429.                         if (Math.sqrt(cX * cX + cY * cY) > 2.0f && canStickerMove(cX, cY)) {
  430.                             //Log.i("MATRIX_OK", "is true to move");
  431.                             mMatrix.postTranslate(cX, cY);
  432.                             postInvalidate();
  433.                             mLastPointX = x;
  434.                             mLastPointY = y;
  435.                         }
  436.                         break;
  437.                     }
  438.                     Log.d("rect3"," -> "+mOriginContentRect);
  439.                     Log.d("rect3"," #> "+mContentRect);
  440.                     touchMoveCount++;
  441.                     return true;
  442.  
  443.             }
  444.         } else if (event.getPointerCount() == 2) {
  445.             Log.e("control", "two finger");
  446.             float x1 = event.getX(0);
  447.             float y1 = event.getY(0);
  448.             float x2 = event.getX(1);
  449.             float y2 = event.getY(1);
  450.  
  451.             float deltaX = event.getX(0) - event.getX(1);
  452.             float deltaY = event.getY(0) - event.getY(1);
  453.             double radians = Math.atan(deltaY / deltaX);
  454.             //Convert to degrees
  455.             int degrees = (int) (radians * 180 / Math.PI);
  456.  
  457.             switch (event.getActionMasked()) {
  458.                 case MotionEvent.ACTION_POINTER_DOWN:
  459.  
  460.                     if (mContentRect.contains(x1, y1) && mContentRect.contains(x2, y2)) {
  461.                         Log.e("control", "two finger get permission yes");
  462.                         mLastPointY = y1;
  463.                         mLastPointX = x1;
  464.                         mLastPointX1 = x2;
  465.                         mLastPointY1 = y2;
  466.                         startDistance = twofingerLength(event);
  467.                         initRotate = degrees;
  468.                         midPoint(midPoint, event);
  469.                         mInController = true;
  470.                     }
  471.                     break;
  472.                 case MotionEvent.ACTION_POINTER_UP:
  473.                     startDistance = (float) 0.0;
  474.  
  475.                 case MotionEvent.ACTION_CANCEL:
  476.                     mLastPointX = 0;
  477.                     mLastPointY = 0;
  478.                     mLastPointX1 = 0;
  479.                     mLastPointY1 = 0;
  480.                     mInController = false;
  481.                     mInMove = false;
  482.                     mInDelete = false;
  483.                     break;
  484.                 case MotionEvent.ACTION_MOVE:
  485.                     Log.e("control", "two finger moveing....");
  486.                     if (mInController) {
  487.                         Log.e("control", "two finger scale");
  488.  
  489.                         if ((degrees - initRotate) > 45) {
  490.                             //Going CCW across the boundary
  491.                             mMatrix.postRotate(-5, mPoints[8], mPoints[9]);
  492.                         } else if ((degrees - initRotate) < -45) {
  493.                             //Going CW across the boundary
  494.                             mMatrix.postRotate(5, mPoints[8], mPoints[9]);
  495.                         } else if (Math.abs(degrees - initRotate) > 1) {
  496.                             //Normal rotation, rotate the difference
  497.                             mMatrix.postRotate(degrees - initRotate, mPoints[8], mPoints[9]);
  498.                         }
  499.  
  500.                         float touchLenght = twofingerLength(event);
  501.                         if (Math.sqrt((startDistance - touchLenght) * (startDistance - touchLenght)) > 0.0f) {
  502.                             float scale = touchLenght / startDistance;
  503.                             float nowsc = mStickerScaleSize * scale;
  504.                             if (nowsc >= MIN_SCALE_SIZE && nowsc <= MAX_SCALE_SIZE) {
  505.                                 mMatrix.postScale(scale, scale, midPoint.x, midPoint.y);
  506.                                 Log.d("draw", "Touch.... " + count++);
  507.                                 mStickerScaleSize = nowsc;
  508.                             }
  509.                         }
  510.                         invalidate();
  511.                         Log.d("rect3"," -> "+mOriginContentRect);
  512.                         Log.d("rect3"," #> "+mContentRect);
  513.                         mLastPointY = y1;
  514.                         mLastPointX = x1;
  515.                         mLastPointX1 = x2;
  516.                         mLastPointY1 = y2;
  517.                         startDistance = touchLenght;
  518.                         initRotate = degrees;
  519.                         break;
  520.                     }
  521.  
  522.                     return true;
  523.             }
  524.         }
  525.         Log.d("matrix",mMatrix.toString());
  526.         if(isFocusable()){
  527.             FOCUS_VIEW = 1;
  528.         }
  529.         return true;
  530.     }
  531.  
  532.     private void doDeleteSticker() {
  533.         setVisibility(View.GONE);
  534.         if (mOnStickerListener != null) {
  535.             mOnStickerListener.onDelete(this);
  536.         }
  537.     }
  538.  
  539.     private void doMoreSticker() {
  540.         if (mOnStickerListener != null) {
  541. //            float[] values = new float[9];
  542. //            mMatrix.getValues(values);
  543. //            float width = Math.abs(values[Matrix.MSCALE_X]) * mBitmap.getWidth();
  544. //            float height = Math.abs(values[Matrix.MSCALE_Y]) * mBitmap.getHeight();
  545. //            Bitmap bitmap = Bitmap.createScaledBitmap(mBitmap, (int) width, (int) height, true);
  546.  //           Bitmap bitmap = Bitmap.createBitmap(mBitmap,0,0,mBitmap.getWidth(),mBitmap.getHeight(),mMatrix,true);
  547. //            Bitmap bitmap1 = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight());
  548. //            Bitmap bitmap = getBitmap();
  549.  //           Log.d("bitmap1", "W: " + bitmap.getWidth() + " H: " + bitmap.getHeight());
  550.    //         mOnStickerListener.onMoreSticker(mBitmap);
  551.             Log.d("rect1", ""+mOriginContentRect);
  552.             mOnStickerListener.onMoreSticker(mBitmap,mMatrix,mOriginContentRect,mOriginPoints);
  553.             //  mOnStickerListener.onMoreSticker(this);
  554.         }
  555.     }
  556.  
  557.     private boolean canStickerMove(float cx, float cy) {
  558.         float px = cx + mPoints[8];
  559.         float py = cy + mPoints[9];
  560.  
  561.  
  562.         if (mViewRect.contains(px, py)) {
  563.             return true;
  564.         } else {
  565.             return false;
  566.         }
  567.     }
  568.  
  569.  
  570.     private float caculateLength(float x, float y) {
  571.         float ex = x - mPoints[8];
  572.         float ey = y - mPoints[9];
  573.         return (float) Math.sqrt(ex * ex + ey * ey);
  574.     }
  575.  
  576.     private float twofingerLength(MotionEvent event) {
  577.         float x = event.getX(0) - event.getX(1);
  578.         float y = event.getY(0) - event.getY(1);
  579.  
  580.         return (float) Math.sqrt(x * x + y * y);
  581.     }
  582.  
  583.  
  584.     private float rotation(MotionEvent event) {
  585.         float originDegree = calculateDegree(mLastPointX, mLastPointY);
  586.         float nowDegree = calculateDegree(event.getX(), event.getY());
  587.         return nowDegree - originDegree;
  588.     }
  589.  
  590.     private float twoFingerRotation(MotionEvent event) {
  591.  
  592.         return -1;
  593.     }
  594.  
  595.     private float calculateDegree(float x, float y) {
  596.         double delta_x = x - mPoints[8];
  597.         double delta_y = y - mPoints[9];
  598.         double radians = Math.atan2(delta_y, delta_x);
  599.         return (float) Math.toDegrees(radians);
  600.     }
  601.  
  602.     public RectF getContentRect() {
  603.         return mContentRect;
  604.     }
  605.  
  606.     public void setOnStickerDeleteListener(OnStickerListener listener) {
  607.         mOnStickerListener = listener;
  608.     }
  609.  
  610.     public interface OnStickerListener {
  611.         public void onDelete(StickerView stickerView);
  612.  
  613.     //    public void onMoreSticker(Bitmap stickerBitmap);
  614.         public void onMoreSticker(Bitmap stickerBitmap,Matrix matrix,RectF rectF,float[] origenalPoints);
  615.         //   public void onMoreSticker(StickerView stickerView);
  616.     }
  617.  
  618.     /**
  619.      * Calculate the mid point of the first two fingers
  620.      */
  621.     private void midPoint(PointF point, MotionEvent event) {
  622.         float x = event.getX(0) + event.getX(1);
  623.         float y = event.getY(0) + event.getY(1);
  624.         point.set(x / 2, y / 2);
  625.     }
  626.  
  627.     public void flipBitmap() {
  628.         Matrix matrix = new Matrix();
  629.         matrix.preScale(-1.0f, 1.0f);
  630.         mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
  631.     }
  632.     public boolean checkTouch(float x,float y){
  633.  
  634.         return  check(mPoints[0],mPoints[1],mPoints[2],mPoints[3],mPoints[4],mPoints[5],mPoints[6],mPoints[7],x,y);
  635.     }
  636.     /* A utility function to calculate area of
  637.     triangle formed by (x1, y1), (x2, y2) and
  638.     (x3, y3) */
  639.     static float area(float x1, float y1, float x2, float y2, float x3, float y3)
  640.     {
  641.         return (float)Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0);
  642.     }
  643.     /* A function to check whether point P(x, y)
  644.     lies inside the rectangle formed by A(x1, y1),
  645.     B(x2, y2), C(x3, y3) and D(x4, y4) */
  646.     static boolean check(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float x, float y)
  647.     {
  648.  
  649.         /* Calculate area of rectangle ABCD */
  650.         float A = area(x1, y1, x2, y2, x3, y3)+ area(x1, y1, x4, y4, x3, y3);
  651.  
  652.         /* Calculate area of triangle PAB */
  653.         float A1 = area(x, y, x1, y1, x2, y2);
  654.  
  655.         /* Calculate area of triangle PBC */
  656.         float A2 = area(x, y, x2, y2, x3, y3);
  657.  
  658.         /* Calculate area of triangle PCD */
  659.         float A3 = area(x, y, x3, y3, x4, y4);
  660.  
  661.         /* Calculate area of triangle PAD */
  662.         float A4 = area(x, y, x1, y1, x4, y4);
  663.  
  664.         /* Check if sum of A1, A2, A3 and A4
  665.         is same as A */
  666.    //     return (A == A1 + A2 + A3 + A4);
  667.         Log.d("toucharea","#: "+A+" @: "+(A1 + A2 + A3 + A4)+" diff: "+Math.abs(A - (A1 + A2 + A3 + A4)));
  668.         if(Math.abs(A - (A1 + A2 + A3 + A4))<15000.0) return  true;
  669.         else return  false;
  670.     }
  671.     public  Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
  672.         Drawable drawable = ContextCompat.getDrawable(context, drawableId);
  673.         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  674.             drawable = (DrawableCompat.wrap(drawable)).mutate();
  675.         }
  676.  
  677.         Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
  678.                 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
  679.         Canvas canvas = new Canvas(bitmap);
  680.         drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  681.         drawable.draw(canvas);
  682.  
  683.         return bitmap;
  684.     }
  685. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement