Android--垃圾桶控件

binView.gif
运用Path和Canvas旋转实现的效果
/**
 * 垃圾桶
 */
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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值