Android自定義view貝塞爾曲線
本文實(shí)例為大家分享了Android自定義view貝塞爾曲線,供大家參考,具體內(nèi)容如下
貝塞爾曲線
以一個(gè)簡(jiǎn)單的貝塞爾曲線為例,二階曲線原理

貝塞爾曲線很多功能都會(huì)用到,比如小火箭發(fā)射,再比如淘寶的購(gòu)物車功能
所幸的是Android有封裝好的貝塞爾曲線,我們直接拿過來用就可以了:
//二階貝賽爾? public void quadTo(float x1, float y1, float x2, float y2)? public void rQuadTo(float dx1, float dy1, float dx2, float dy2)? //三階貝賽爾? public void cubicTo(float x1, float y1, float x2, float y2,float x3, float y3)? public void rCubicTo(float x1, float y1, float x2, float y2,float x3, float y3)
自定義view代碼如下
public class MyView extends View {
? ? private Point controlPoint = new Point(200, 200);
? ? public MyView(Context context) {
? ? ? ? super(context);
? ? }
? ? public MyView(Context context, @Nullable AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? }
? ? public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? }
? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);
? ? ? ? Paint paint = new Paint();
// ? ? ? ?畫筆設(shè)置描邊
? ? ? ? paint.setStyle(Paint.Style.STROKE);
// ? ? ? ?顏色
? ? ? ? paint.setColor(Color.BLACK);
// ? ? ? ?描邊寬度
? ? ? ? paint.setStrokeWidth(10);
//路徑
? ? ? ? Path path = new Path();
// ? ? ? ?moveTo 不會(huì)進(jìn)行繪制,只用于移動(dòng)移動(dòng)畫筆。
? ? ? ? path.moveTo(100, 500);
//繪制貝塞爾曲線,controlPoint.x, controlPoint.y控制點(diǎn)和700, 500終點(diǎn)坐標(biāo)
? ? ? ? path.quadTo(controlPoint.x, controlPoint.y, 700, 500);
? ? ? ? //繪制路徑
? ? ? ? canvas.drawPath(path, paint);
? ? ? ? //繪制輔助點(diǎn)
? ? ? ? canvas.drawPoint(controlPoint.x,controlPoint.y,paint);
? ? }
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? switch (event.getAction()) {
? ? ? ? ? ? case MotionEvent.ACTION_MOVE:
? ? ? ? ? ? ? ? controlPoint.x = (int) event.getX();
? ? ? ? ? ? ? ? controlPoint.y = (int) event.getY();
// ? ? ? ? ? ? invalidate();重繪 刷新
? ? ? ? ? ? ? ? invalidate();
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? return true;
? ? }
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程獲取并設(shè)置Activity亮度的方法
這篇文章主要介紹了Android編程獲取并設(shè)置Activity亮度的方法,涉及Android針對(duì)屏幕亮度的相關(guān)操作技巧,需要的朋友可以參考下2015-12-12
Android Studio導(dǎo)入Project與Module的方法及實(shí)例
這篇文章主要介紹了Android Studio導(dǎo)入Project與Module的方法及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android中Binder詳細(xì)學(xué)習(xí)心得
這篇文章主要介紹了Android中Binder詳細(xì)學(xué)習(xí)心得,并分析了Binder的詳細(xì)用法,需要的朋友參考下吧。2018-01-01
簡(jiǎn)單實(shí)現(xiàn)Android端搜索框示例詳解
這篇文章主要為大家介紹了簡(jiǎn)單實(shí)現(xiàn)Android端搜索框示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android中兩個(gè)Activity之間數(shù)據(jù)傳遞及返回問題
本篇文章主要介紹了Android中兩個(gè)Activity之間數(shù)據(jù)傳遞及返回問題,這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02

