path加速球

自定义View

public class MyPath extends View {
    private int width;
    private int height;
    private Paint mPaint;
    private Path path;
    private Paint mPaintPoint;
    private int countX=80;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0x23:
                    countX--;
                    invalidate();
                    if (countX<0){
                        countX=80;
                        handler.sendEmptyMessageDelayed(0x23,1000);
                    }

                    break;
            }
        }
    };
    public MyPath(Context context) {
        super(context);
    }

    public MyPath(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint=new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaintPoint=new Paint();
        mPaintPoint.setColor(Color.GREEN);
        mPaintPoint.setStrokeWidth(5);
        path=new Path();
        PorterDuffXfermode mode=new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        mPaintPoint.setXfermode(mode);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width=getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
        height=getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
        setMeasuredDimension(width,height);

        mBitMap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        mCanvas=new Canvas(mBitMap);

    }
    private Canvas mCanvas;
    private Bitmap mBitMap;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLUE);
        mCanvas.drawCircle(300,300,150,mPaint);
        handler.sendEmptyMessage(0x23);
        path.reset();
        path.moveTo(500,200);
        path.lineTo(500,500);
        path.lineTo(countX, 500);
        path.lineTo(countX,200);
        for (int i = 0; i <20; i++) {
            path.rQuadTo(20,20,40,0);
            path.rQuadTo(20,-20,40,0);
        }
        path.close();
            //path.addCircle(200,100,100, Path.Direction.CW);
            //canvas.drawPath(path,mPaint);
        mCanvas.drawPath(path,mPaintPoint);
        canvas.drawBitmap(mBitMap,0,0,null);


    }
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:myview="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" tools:context=".MainActivity">

    <com.lingzhuo.test.mypath.MyPath
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值