美文网首页
View绘制

View绘制

作者: SMSM | 来源:发表于2017-12-26 18:10 被阅读8次
  • save restore Canvas保存和还原
  • drawLine drawColor 绘制图形
  • translate scale 平移 旋转 坐标轴梗着跟着变
  • clipRect clipPath 裁剪画布
    android.graphics.Region.Op.DIFFERENCE 用于决定前后两块画布之间如何配合展示比如子交补并

先绘制水晶球,画布变成了圆形,然后绘制矩形,根据高度不停的绘制实现动画效果

圆形的水晶球装水的过程,水平线一点点的升高
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        wellPath.reset();
        wellPath.addCircle(getWidth() / 2, getHeight() / 2, getWidth() * 2 / 5, Path.Direction.CCW);
        canvas.clipPath(wellPath);
        canvas.drawColor(getResources().getColor(R.color.well));

        //子交补并
//        waterRect.top = (int) startY;
//        canvas.drawRect(waterRect,waterPaint);

        //http://blog.csdn.net/eyishion/article/details/53728913
        canvas.save();
        waterRect.top = (int) startY;
        canvas.clipRect(waterRect, android.graphics.Region.Op.INTERSECT);
        canvas.drawColor(getResources().getColor(R.color.water));
        canvas.restore();


        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
        valueAnimator.setDuration(2000);
        valueAnimator.setRepeatCount(INFINITE);
        valueAnimator.setRepeatMode(REVERSE);
        valueAnimator.setInterpolator(new DecelerateInterpolator());


        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float progress = animation.getAnimatedFraction();
                Log.d(TAG, "onAnimationUpdate " + progress);
                startY = (getHeight() - 20) * (1 - progress);
                startY = startY + 10;
                invalidate();
            }
        });
        valueAnimator.start();

相关文章

网友评论

      本文标题:View绘制

      本文链接:https://www.haomeiwen.com/subject/symjgxtx.html