Advertisement
tifaout

DrawingPicture.java

Nov 8th, 2023
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.38 KB | None | 0 0
  1. package com.own.kidscoloringbook;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.graphics.BitmapShader;
  8. import android.graphics.Canvas;
  9. import android.graphics.Color;
  10. import android.graphics.CornerPathEffect;
  11. import android.graphics.Paint;
  12. import android.graphics.Path;
  13. import android.graphics.PorterDuff;
  14. import android.graphics.Shader;
  15. import android.util.AttributeSet;
  16. import android.view.MotionEvent;
  17. import android.view.View;
  18.  
  19. import androidx.core.view.ViewCompat;
  20.  
  21. import com.plattysoft.leonids.ParticleSystem;
  22.  
  23. import java.io.PrintStream;
  24.  
  25. public class DrawingPicture extends View {
  26.     public static Bitmap canvasBitmap;
  27.     DrawActivity a;
  28.     private Paint canvasPaint;
  29.     private Paint circlePaint;
  30.     private Canvas drawCanvas;
  31.     private boolean drawEraser;
  32.     private Paint drawPaint;
  33.     private Path drawPath;
  34.     private DrawingPicture drawingPicture;
  35.     public int gapPlaySound = 0;
  36.     public Bitmap kidBitmap;
  37.     public boolean kidBitmapDrawn;
  38.     public boolean kidBitmapNeedDrawn;
  39.     private float mX;
  40.     private float mY;
  41.  
  42.     public DrawingPicture(Context context, AttributeSet attributeSet) {
  43.         super(context, attributeSet);
  44.         this.a = (DrawActivity) context;
  45.         this.drawingPicture = this;
  46.         this.drawEraser = false;
  47.         setupDrawing();
  48.  
  49.     }
  50.  
  51.     private void setDefaultBrushSize() {
  52.         com.own.kidscoloringbook.MyConstant.BRUSH_SIZE = 20;
  53.         this.drawPaint.setStrokeWidth((float) com.own.kidscoloringbook.MyConstant.BRUSH_SIZE);
  54.     }
  55.  
  56.     private void setPointOfSparkImage(float f, float f2) {
  57.         DrawActivity.iv.setX(f);
  58.         DrawActivity.iv.setY(f2);
  59.     }
  60.  
  61.     private void setupDrawing() {
  62.         this.drawPath = new Path();
  63.         this.drawPaint = new Paint();
  64.         this.drawPaint.setAntiAlias(true);
  65.         this.drawPaint.setStyle(Paint.Style.STROKE);
  66.         this.drawPaint.setStrokeJoin(Paint.Join.ROUND);
  67.         this.drawPaint.setStrokeCap(Paint.Cap.ROUND);
  68.         this.drawPaint.setPathEffect(new CornerPathEffect(10.0f));
  69.         this.canvasPaint = new Paint(4);
  70.         this.circlePaint = new Paint(1);
  71.         this.circlePaint.setColor(ViewCompat.MEASURED_STATE_MASK);
  72.         setDefaultBrushSize();
  73.     }
  74.  
  75.  
  76.     @Override
  77.     public void dispatchDraw(Canvas canvas) {
  78.         this.gapPlaySound++;
  79.         if (this.gapPlaySound == 100) {
  80.             this.gapPlaySound = 0;
  81.         }
  82.         canvas.save();
  83.         canvas.drawBitmap(canvasBitmap, 0.0f, 0.0f, this.canvasPaint);
  84.         canvas.drawPath(this.drawPath, this.drawPaint);
  85.         Bitmap bitmap = this.kidBitmap;
  86.         if (bitmap != null) {
  87.             canvas.drawBitmap(bitmap, 0.0f, 0.0f, this.canvasPaint);
  88.         }
  89.         if (com.own.kidscoloringbook.MyConstant.SELECTED_TOOL == 2 && this.drawEraser) {
  90.             if (this.gapPlaySound % 30 == 0) {
  91.                 this.a.mediaPlayer.playSound(R.raw.eraser);
  92.             }
  93.             this.drawPaint.setShader((Shader) null);
  94.             this.circlePaint.setColor(ViewCompat.MEASURED_STATE_MASK);
  95.             canvas.drawCircle(com.own.kidscoloringbook.MyConstant.eraseX, com.own.kidscoloringbook.MyConstant.eraseY, com.own.kidscoloringbook.MyConstant.eraseR, this.circlePaint);
  96.             this.circlePaint.setColor(-1);
  97.             float f = com.own.kidscoloringbook.MyConstant.eraseX;
  98.             float f2 = com.own.kidscoloringbook.MyConstant.eraseY;
  99.             double d = (double) com.own.kidscoloringbook.MyConstant.eraseR;
  100.             Double.isNaN(d);
  101.             canvas.drawCircle(f, f2, (float) (d * 0.9d), this.circlePaint);
  102.         }
  103.         canvas.restore();
  104.         super.dispatchDraw(canvas);
  105.     }
  106.  
  107.  
  108.     @Override
  109.     public void onSizeChanged(int i, int i2, int i3, int i4) {
  110.         super.onSizeChanged(i, i2, i3, i4);
  111.         com.own.kidscoloringbook.MyConstant.onSizeCalled++;
  112.         if (com.own.kidscoloringbook.MyConstant.onSizeCalled < 2) {
  113.             if (com.own.kidscoloringbook.MyConstant.drawWidth == 0 || com.own.kidscoloringbook.MyConstant.drawHeight == 0) {
  114.                 com.own.kidscoloringbook.MyConstant.drawWidth = 700;
  115.                 com.own.kidscoloringbook.MyConstant.drawHeight = 700;
  116.             }
  117.             if (canvasBitmap == null) {
  118.                 canvasBitmap = Bitmap.createBitmap(i, i2, Bitmap.Config.ARGB_8888);
  119.                 this.drawCanvas = new Canvas(canvasBitmap);
  120.             }
  121.             com.own.kidscoloringbook.MyConstant.drawWidth = i;
  122.             com.own.kidscoloringbook.MyConstant.drawHeight = i2;
  123.             com.own.kidscoloringbook.MyConstant.pixels = new int[(com.own.kidscoloringbook.MyConstant.drawWidth * com.own.kidscoloringbook.MyConstant.drawHeight)];
  124.             System.err.println("  MyConstant.pixels:" + com.own.kidscoloringbook.MyConstant.pixels.length);
  125.             DrawActivity.getDrawActivity().insertBitmap();
  126.         }
  127.     }
  128.  
  129.     @Override
  130.     public boolean onTouchEvent(MotionEvent motionEvent) {
  131.         float x = motionEvent.getX();
  132.         float y = motionEvent.getY();
  133.         com.own.kidscoloringbook.MyConstant.eraseX = x;
  134.         com.own.kidscoloringbook.MyConstant.eraseY = y;
  135.         int action = motionEvent.getAction() & 255;
  136.         if (action == 0) {
  137.             this.mX = motionEvent.getX();
  138.             this.mY = motionEvent.getY();
  139.             if (com.own.kidscoloringbook.MyConstant.SELECTED_TOOL != 0) {
  140.                 this.drawPaint.setStrokeWidth((float) com.own.kidscoloringbook.MyConstant.BRUSH_SIZE);
  141.                 if (com.own.kidscoloringbook.MyConstant.SELECTED_TOOL != 2 && !DrawActivity.ispatternClicked) {
  142.                     setPathColor(com.own.kidscoloringbook.MyConstant.DRAW_COLOR);
  143.                 } else if (com.own.kidscoloringbook.MyConstant.SELECTED_TOOL == 2) {
  144.                     this.drawPaint.setShader((Shader) null);
  145.                     this.drawPaint.setColor(-1);
  146.                     this.drawPaint.setStrokeWidth((float) com.own.kidscoloringbook.MyConstant.ERASER_WIDTH);
  147.                 } else {
  148.                     this.drawPaint.setColor(-1);
  149.                 }
  150.                 this.drawPath.moveTo(x, y);
  151.                 this.drawEraser = true;
  152.             }
  153.         } else if (action == 1) {
  154.             setPointOfSparkImage(this.mX, this.mY);
  155.             this.a.mediaPlayer.playColorRandomSound();
  156.             startOneShotParticle(DrawActivity.iv);
  157.             if (com.own.kidscoloringbook.MyConstant.SELECTED_TOOL == 0) {
  158.                 if (this.kidBitmap != null && (!this.kidBitmapDrawn || this.kidBitmapNeedDrawn)) {
  159.                     PrintStream printStream = System.err;
  160.                     printStream.println("ooo::" + this.drawCanvas + "--" + this.kidBitmap + "---" + this.canvasPaint);
  161.                     this.drawCanvas.drawBitmap(this.kidBitmap, 0.0f, 0.0f, this.canvasPaint);
  162.                     this.kidBitmapDrawn = true;
  163.                     this.kidBitmapNeedDrawn = false;
  164.                 }
  165.                 Bitmap bitmap = canvasBitmap;
  166.                 int[] iArr = com.own.kidscoloringbook.MyConstant.pixels;
  167.                 int i = com.own.kidscoloringbook.MyConstant.drawWidth;
  168.                 bitmap.getPixels(iArr, 0, i, 0, 0, i, com.own.kidscoloringbook.MyConstant.drawHeight);
  169.                 int pixel = canvasBitmap.getPixel((int) this.mX, (int) this.mY);
  170.                 int red = Color.red(pixel);
  171.                 int green = Color.green(pixel);
  172.                 int blue = Color.blue(pixel);
  173.                 if (red < 255 && red == green && red == blue) {
  174.                     if (red <= 0) {
  175.                         return false;
  176.                     }
  177.                     pixel = -1;
  178.                 }
  179.                 invalidate();
  180.                 QueueLinearFloodFiller queueLinearFloodFiller = new QueueLinearFloodFiller(pixel, com.own.kidscoloringbook.MyConstant.DRAW_COLOR);
  181.                 queueLinearFloodFiller.setTolerance(60);
  182.                 try {
  183.                     queueLinearFloodFiller.floodFill((int) this.mX, (int) this.mY);
  184.                 }catch (Exception e){}
  185.                 Bitmap bitmap2 = canvasBitmap;
  186.                 int[] iArr2 = com.own.kidscoloringbook.MyConstant.pixels;
  187.                 int i2 = com.own.kidscoloringbook.MyConstant.drawWidth;
  188.                 bitmap2.setPixels(iArr2, 0, i2, 0, 0, i2, com.own.kidscoloringbook.MyConstant.drawHeight);
  189.             } else {
  190.                 this.kidBitmapNeedDrawn = true;
  191.                 this.drawPath.lineTo(this.mX, this.mY);
  192.                 this.drawCanvas.drawPath(this.drawPath, this.drawPaint);
  193.                 this.drawPath.reset();
  194.                 this.drawEraser = false;
  195.             }
  196.             Bitmap bitmap3 = this.kidBitmap;
  197.         } else if (action != 2) {
  198.             return false;
  199.         } else {
  200.             if (com.own.kidscoloringbook.MyConstant.SELECTED_TOOL != 0) {
  201.                 float abs = Math.abs(x - this.mX);
  202.                 float abs2 = Math.abs(y - this.mY);
  203.                 if (abs >= 0.0f || abs2 >= 0.0f) {
  204.                     Path path = this.drawPath;
  205.                     float f = this.mX;
  206.                     float f2 = this.mY;
  207.                     path.quadTo(f, f2, (f + x) / 2.0f, (f2 + y) / 2.0f);
  208.                     this.mX = x;
  209.                     this.mY = y;
  210.                 }
  211.             }
  212.         }
  213.         invalidate();
  214.         return true;
  215.     }
  216.  
  217.     public void setKidsImage() {
  218.         invalidate();
  219.         Bitmap bitmap = canvasBitmap;
  220.         if (bitmap != null) {
  221.             bitmap.eraseColor(-1);
  222.         } else {
  223.             onSizeChanged(com.own.kidscoloringbook.MyConstant.drawWidth, com.own.kidscoloringbook.MyConstant.drawHeight, com.own.kidscoloringbook.MyConstant.drawWidth, com.own.kidscoloringbook.MyConstant.drawHeight);
  224.         }
  225.         this.kidBitmapDrawn = false;
  226.     }
  227.  
  228.     public void setPathColor(int i) {
  229.         System.err.println("color cliked inside color");
  230.         this.drawPaint.setShader((Shader) null);
  231.         this.drawPaint.setColor(i);
  232.     }
  233.  
  234.     public void setPattern(String str) {
  235.         System.err.println("color cliked inside pattern");
  236.         invalidate();
  237.         Bitmap decodeResource = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(str, "drawable", this.a.getPackageName()));
  238.         Shader.TileMode tileMode = Shader.TileMode.REPEAT;
  239.         BitmapShader bitmapShader = new BitmapShader(decodeResource, tileMode, tileMode);
  240.         this.drawPaint.setColor(-1);
  241.         this.drawPaint.setShader(bitmapShader);
  242.     }
  243.  
  244.     public void startNew() {
  245.         this.kidBitmap = null;
  246.         this.drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
  247.         invalidate();
  248.     }
  249.  
  250.     public void startOneShotParticle(View view) {
  251.         new ParticleSystem((Activity) this.a, 5, (int) R.drawable.spark_bluedot, 200).setSpeedRange(0.45f, 0.75f).oneShot(view, 4);
  252.         new ParticleSystem((Activity) this.a, 5, (int) R.drawable.effect_star1, 300).setSpeedRange(0.35f, 0.7f).oneShot(view, 3);
  253.         new ParticleSystem((Activity) this.a, 5, (int) R.drawable.effect_star2, 400).setSpeedRange(0.3f, 0.68f).oneShot(view, 2);
  254.         new ParticleSystem((Activity) this.a, 5, (int) R.drawable.effect_star3, 250).setSpeedRange(0.42f, 0.6f).oneShot(view, 4);
  255.         new ParticleSystem((Activity) this.a, 5, (int) R.drawable.spark_yellowdot, 350).setSpeedRange(0.37f, 0.65f).oneShot(view, 3);
  256.     }
  257. }
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement