android繪圖Path類仿加速球水面波動效果

來源:互聯網
上載者:User

android繪圖Path類仿加速球水面波動效果

android提供的Path類,他可以預先在View上連成路徑,然後調用Canvas的drawPath(path,paint),沿著路徑畫出來即可;
【1】畫三角形

//        path.moveTo(100, 100);//        path.lineTo(0, 200);//        path.lineTo(200, 200);//        path.close();//串連首尾//        canvas.drawPath(path, paint);//        canvas.drawTextOnPath(我沿著這條線走, path, 0, 0, paint);

【畫貝茲路徑】

 //貝茲路徑//        path.moveTo(100,100);//        path.quadTo(100, 400, 300, 300);//兩個點:控制點;結束點//        canvas.drawPath(path, paint);//canvas.drawPoint(100, 100, paintPoint);//canvas.drawPoint(100,400,paintPoint);//canvas.drawPoint(300,300,paintPoint);

【仿水邊波動效果】

public class MyPathView extends View {    private Paint paint;    private Path path;    private Paint paintPoint;    private int width;    private int height;    private int count = 0;    private int size = 0;    private boolean isAdd=true;    private Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            switch (msg.what){                case 0x23:                    count+=5;                    if(count>=80){                        count=0;                    }                    if (isAdd){                        size++;                        if(size>10){                            isAdd = false;                        }                    }else{                        size--;                        if(size<=-10){                            isAdd =true;                        }                    }                    invalidate();                    sendEmptyMessageDelayed(0x23,100);                    break;            }        }    };    public MyPathView(Context context, AttributeSet attrs) {        super(context, attrs);        paint = new Paint();        paint.setAntiAlias(true);        paint.setTextSize(30);        paint.setStyle(Paint.Style.STROKE);        paintPoint = new Paint();        paintPoint.setStrokeWidth(10);        paintPoint.setColor(Color.GREEN);        paintPoint.setStyle(Paint.Style.FILL);        path = new Path();        handler.sendEmptyMessageDelayed(0x23, 1000);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        path.reset();        path.moveTo(count, 100);        for (int i = 0; i < 10; i++) {            path.rQuadTo(20, 20, 40, 0);            path.rQuadTo(20, -20, 40, 0);        }        canvas.drawPath(path, paint);        canvas.drawRect(200, 0, 400, 200, paint);    }    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);        height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);        setMeasuredDimension(width, height);    }}

布局

    

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.