Android--垃圾桶控件

2020-07-03 11:14:48 浏览数 (1)

运用Path和Canvas旋转实现的效果
代码语言:javascript复制
/**
 * 垃圾桶
 */
public class BinView extends View {
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Bitmap bottomBitmap;
    //下面的高度
    private int bottomHeight = 50;
    //盖子宽度
    private int gaiziWidth = 100;
    //盖子初始Y坐标
    private int gaiziOffset;
    private Path gaiziPath;
    //旋转角度
    private float rotate = 1;

    public BinView(Context context) {
        super(context);
    }

    public BinView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(5);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int centerX = canvas.getWidth() / 2;
        int centerY = canvas.getHeight() / 2;

        if (bottomBitmap == null) {//缓冲
            bottomBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
            Canvas bottomCanvas = new Canvas(bottomBitmap);

            //画中间三条竖线
            int spac = 10;//每条线的间距

            Path path1 = new Path();
            path1.moveTo(centerX, centerY);
            path1.lineTo(centerX, centerY   bottomHeight);

            Path path2 = new Path();
            path2.moveTo(centerX - spac, centerY);
            path2.lineTo(centerX - spac, centerY   bottomHeight);

            Path path3 = new Path();
            path3.moveTo(centerX   spac, centerY);
            path3.lineTo(centerX   spac, centerY   bottomHeight);

            bottomCanvas.drawPath(path1, mPaint);
            bottomCanvas.drawPath(path2, mPaint);
            bottomCanvas.drawPath(path3, mPaint);

            //底部
            int bottomWidth = 40;
            Path path4 = new Path();
            path4.moveTo(centerX - bottomWidth / 2, centerY   spac   bottomHeight);
            path4.lineTo(centerX   bottomWidth / 2, centerY   spac   bottomHeight);
            bottomCanvas.drawPath(path4, mPaint);

            //画左边斜线
            bottomCanvas.save();
            bottomCanvas.rotate(-15, centerX - bottomWidth / 2, centerY   spac   bottomHeight);
            Path path5 = new Path();
            path5.moveTo(centerX - bottomWidth / 2, centerY   spac   bottomHeight);
            path5.lineTo(centerX - bottomWidth / 2, centerY - spac);
            bottomCanvas.drawPath(path5, mPaint);
            bottomCanvas.restore();

            //画右边斜线
            bottomCanvas.save();
            bottomCanvas.rotate(15, centerX   bottomWidth / 2, centerY   spac   bottomHeight);
            Path path6 = new Path();
            path6.moveTo(centerX   bottomWidth / 2, centerY   spac   bottomHeight);
            path6.lineTo(centerX   bottomWidth / 2, centerY - spac);
            bottomCanvas.drawPath(path6, mPaint);
            bottomCanvas.restore();

            gaiziOffset = spac;
        }

        //画底部缓冲
        canvas.drawBitmap(bottomBitmap, 0, 0, mPaint);
        //画盖子
        if (gaiziPath == null) {
            gaiziPath = new Path();
            gaiziPath.moveTo(centerX - gaiziWidth / 2, centerY - gaiziOffset);
            gaiziPath.lineTo(centerX   gaiziWidth / 2, centerY - gaiziOffset);
            int gaiziHeadWidth = 10;
            gaiziPath.addRect(centerX - gaiziHeadWidth, centerY - gaiziHeadWidth - gaiziOffset, centerX   gaiziHeadWidth, centerY - gaiziOffset, Path.Direction.CW);
        }
        canvas.save();
        canvas.rotate(45 * (1 - rotate), centerX   gaiziWidth / 2, centerY - gaiziOffset);
        canvas.drawPath(gaiziPath, mPaint);
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            startAnime();
        }
        return super.onTouchEvent(event);
    }

    private ValueAnimator valueAnimator;

    public void startAnime() {
        if (valueAnimator == null) {
            valueAnimator = ValueAnimator.ofFloat(1);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.setDuration(1000);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    rotate = (float) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
        }

        valueAnimator.start();
    }
}
项目地址:https://gitee.com/aruba/CanvasApplication.git

0 人点赞