Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Canvas al ajillo

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Canvas al ajillo

My expierence with canvas and drawing in Android

Avatar for Adrián Lomas

Adrián Lomas

December 02, 2016

Other Decks in Programming

Transcript

  1. Los detalles importan Adapta las vistas a las necesidades no

    las necesidades a las vistas Un mundo diferente y divertido
  2. public Loading(Context context) { super(context); initialize(); } public Loading(Context context,

    AttributeSet attrs) { super(context, attrs); initialize(); } public Loading(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initialize(); } private void initialize() { circlePaint = new Paint(); circlePaint.setColor(circleColor); circlePaint.setAntiAlias(true); circlePaint.setStyle(Paint.Style.STROKE); circlePaint.setStrokeWidth(circleStroke); }
  3. @Override protected void onSizeChanged(int w, int h, int oldw, int

    oldh) { super.onSizeChanged(w, h, oldw, oldh); width = w; height = h; createCircle(); } private void createCircleArea() { int circleAreawidth = width - strokeWidth; int circleAreaHeight = heihgt - strokeWidth; circleArea = new RectF(strokeWidth, strokeWidth, circleAreawidth, circleAreaHeight); }
  4. private float circleStartDegree = 0; private float circleEndDegree = 360;

    private float actualCircleEndDegree = circleEndDegree; @Override protected void onDraw(Canvas canvas) { drawCircle(canvas); } private void drawCircle(Canvas canvas) { canvas.drawArc(circleArea, circleStartDegree, actualCircleEndDegree, false, circlePaint); }
  5. private void initializeAnimator() { valueAnimator = ValueAnimator.ofInt(circleStartDegree, circleEndDegree); valueAnimator.setDuration(1000); valueAnimator.addUpdateListener(new

    ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { int degreeValue = (int) valueAnimator.getAnimatedValue(); actualCircleEndDegree = degreeValue; invalidate(); } }); } private void initialize() { initializePainter(); initializeAnimator(); }
  6. blurPainter = new Paint(circlePaint); blurPainter.setColor(Color.RED); blurPainter.setStrokeWidth(circleStroke * 1.20F); blurPainter.setMaskFilter(new BlurMaskFilter(blurRadius,

    BlurMaskFilter.Blur.NORMAL)); private void drawCircle(Canvas canvas) { canvas.drawArc(internalCircle, circleStartDegree, circleEndDegree, false, blurPainter); canvas.drawArc(internalCircle, circleStartDegree, circleEndDegree, false, circlePaint); }
  7. @Override protected void onSizeChanged(int w, int h, int oldw, int

    oldh) { super.onSizeChanged(w, h, oldw, oldh); width = w; height = h; createPath(); } private void createPath() { curvedPath = new Path(); curvedPath.moveTo(0, 0); curvedPath.lineTo(width, 0); } @Override protected void onDraw(Canvas canvas) { canvas.drawPath(curvedPath, pathPaint); }
  8. private void createPath() { float pathCurvatureY = height / 2;

    float middleX = width / 2; curvedPath = new Path(); curvedPath.moveTo(0, 0); curvedPath.quadTo(middleX, pathCurvatureY, width, 0); } 0,0 middleX, pathCurvatureY width, 0
  9. private void createPath() { float pathCurvatureY = height / 2;

    float middleX = width / 2; curvedPath = new Path(); curvedPath.moveTo(0, 0); curvedPath.quadTo(middleX, pathCurvatureY, width, 0); curvedPath.lineTo(width, height); } width, height
  10. private void createPath() { float pathCurvatureY = height / 2;

    float middleX = width / 2; curvedPath = new Path(); curvedPath.moveTo(0, 0); curvedPath.quadTo(middleX, pathCurvatureY, width, 0); curvedPath.lineTo(width, height); curvedPath.lineTo(0, height); } 0, height
  11. private void createPath() { float pathCurvatureY = height / 2;

    float middleX = width / 2; curvedPath = new Path(); curvedPath.moveTo(0, 0); curvedPath.quadTo(middleX, pathCurvatureY, width, 0); curvedPath.lineTo(width, height); curvedPath.lineTo(0, height); curvedPath.close(); }
  12. @Override protected void onSizeChanged(int w, int h, int oldw, int

    oldh) { super.onSizeChanged(w, h, oldw, oldh); width = w; height = h; createPath(); createTouchRegion(); } private void createTouchRegion() { RectF pathBoundsRect = new RectF(); curvedPath.computeBounds(rectF, true); regionToCheck = new Region(pathBoundsRect.left, pathBoundsRect.top, pathBoundsRect.right, pathBoundsRect.bottom); regionToCheck.setPath(curvedPath, regionToCheck); }
  13. @Override public boolean onTouchEvent(MotionEvent event) { boolean result = false;

    int pointX = (int) event.getX(); int pointY = (int) event.getY(); if (isPointInsidePathArea(pointX, pointY)) { result = super.onTouchEvent(event); } return result; } private boolean isPointInsidePathArea(int x, int y) { return regionToCheck.contains(x, y); }
  14. private float[] getPathCoordinates(Path path, float fraction) { float aCoordinates[] =

    { 0f, 0f }; PathMeasure pm = new PathMeasure(path, false); pm.getPosTan(pm.getLength() * fraction, aCoordinates, null); return aCoordinates; }
  15. @Override public void onAnimationUpdate(ValueAnimator animation) { float value = animation.getAnimatedFraction();

    float[] coordinates = getPathCoordinates(infinitePath, value); this.ballX = coordinates[0]; this.ballY = coordinates[1]; invalidate() }
  16. @Override protected void onDraw(Canvas canvas) { if (mRunning) { if

    (mShowLeftEye) { canvas.drawCircle(mLeftEyePos[0], mLeftEyePos[1], mEyeCircleRadius, mCirclePaint); } if (mShowRightEye) { canvas.drawCircle(mRightEyePos[0], mRightEyePos[1], mEyeCircleRadius, mCirclePaint); } if (mFirstStep) { mArcPath.reset(); mArcPath.addArc(mRectF, mStartAngle, mSweepAngle); canvas.drawPath(mArcPath, mArcPaint); } else { mArcPath.reset(); mArcPath.addArc(mRectF, mStartAngle, mSweepAngle); canvas.drawPath(mArcPath, mArcPaint); } } else { canvas.drawCircle(mLeftEyePos[0], mLeftEyePos[1], mEyeCircleRadius, mCirclePaint); canvas.drawCircle(mRightEyePos[0], mRightEyePos[1], mEyeCircleRadius,mCirclePaint); canvas.drawPath(mArcPath, mArcPaint); } }