這篇文章給大家分享的是有關(guān)Android如何自定義view實(shí)現(xiàn)車載可調(diào)整軌跡線的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
公司主營業(yè)務(wù):網(wǎng)站設(shè)計(jì)、做網(wǎng)站、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出萍鄉(xiāng)免費(fèi)做網(wǎng)站回饋大家。
具體內(nèi)容如下
同事做的view,拿過來做個(gè)記錄。
/** * */package com.text.myviewdemo.view; import org.apache.http.conn.routing.RouteInfo.LayerType; import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.DashPathEffect;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.Xfermode;import android.graphics.PorterDuff.Mode;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View; /** * @author chenhanrong * */public class CCView extends View implements android.view.View.OnClickListener{ private Paint paint; private float[] line_r,line_l,line_1,line_2,line_3,line_t;// private float line1YL,line1YR,line2YL,line2YR,line3YL,line3YR; public Context context; private float radiu; private boolean showPoint = false; private boolean cmP1=false; private boolean cmP2=false; private boolean cmP3=false; private boolean cmP4=false; private boolean cmP5=false; private boolean cmP6=false; private boolean cmP7=false; private boolean cmP8=false; private boolean cmP9=false; private boolean cmP10=false; private boolean isfirst = true; private boolean isMove = false; public final static int D_LEFT =0; public final static int D_RIGHT =1; public final static int TYPE_MIN =0; public final static int TYPE_MAX =1; public CCView(Context context) { this(context,null); } public CCView(Context context, AttributeSet attrs) { this(context,attrs,0); } public CCView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; init(); } /** * 初始化控件 */ private void init() { paint = new Paint(); /** * 去鋸齒 */ paint.setAntiAlias(true); /** * 設(shè)置paint的顏色 */ paint.setColor(Color.RED); /** * 設(shè)置paint的 style */ paint.setStyle(Paint.Style.FILL); /** * 設(shè)置paint的外框?qū)挾? */ paint.setStrokeWidth(10); setOnClickListener(this); setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } }); setLayerType(LAYER_TYPE_HARDWARE, paint); radiu = 20f; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);// paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER)); if(isfirst){ line_l = new float[]{getWidth()/4,getHeight()/4,getWidth()/8,getHeight()}; line_r = new float[]{3*getWidth()/4, getHeight()/4,7*getWidth()/8, getHeight()}; line_1 = new float[]{getPointX(line_l, line_l[1]*2f),line_l[1]*2f,getPointX(line_r, line_r[1]*2f), line_r[1]*2f}; line_2 = new float[]{getPointX(line_l, line_l[1]*2.5f), line_l[1]*2.5f, getPointX(line_r, line_r[1]*2.5f), line_r[1]*2.5f}; line_3 = new float[]{getPointX(line_l, line_l[1]*3f), line_l[1]*3f, getPointX(line_r, line_r[1]*3f), line_r[1]*3f}; isfirst = false; } int canvasWidth = canvas.getWidth(); int canvasHeight = canvas.getHeight(); int layerId = canvas.saveLayer(0, 0, canvasWidth, canvasHeight, null, Canvas.ALL_SAVE_FLAG);// paint.setXfermode(new PorterDuffXfermode(Mode.DST_OVER)); line_t = new float[]{getPointX(line_l, line_l[1]), line_l[1], getPointX(line_r, line_r[1]), line_r[1]}; if(showPoint){ //四條橫線 canvas.drawLines(line_t, paint); paint.setPathEffect(new DashPathEffect(new float[] {20, 5}, 0)); canvas.drawLines(line_1, paint); paint.setColor(Color.GREEN); canvas.drawLines(line_2, paint); paint.setColor(Color.YELLOW); canvas.drawLines(line_3, paint); paint.setColor(Color.RED); paint.setPathEffect(null); //左右兩邊線 canvas.drawLines(line_l, paint); canvas.drawLines(line_r, paint); //中間三條橫線交點(diǎn) canvas.drawCircle(line_1[0], line_1[1],radiu, paint); canvas.drawCircle(line_1[2], line_1[3],radiu, paint); paint.setColor(Color.GREEN); canvas.drawCircle(line_2[0], line_2[1],radiu, paint); canvas.drawCircle(line_2[2], line_2[3],radiu, paint); paint.setColor(Color.YELLOW); canvas.drawCircle(line_3[0], line_3[1],radiu, paint); canvas.drawCircle(line_3[2], line_3[3],radiu, paint); paint.setColor(Color.RED); //左右四點(diǎn) canvas.drawCircle(line_l[0], line_l[1],radiu, paint); canvas.drawCircle(line_l[2], line_l[3],radiu, paint); canvas.drawCircle(line_r[0], line_r[1],radiu, paint); canvas.drawCircle(line_r[2], line_r[3],radiu, paint); }else{ float lf=getDashLineLength(D_LEFT); float rf=getDashLineLength(D_RIGHT); canvas.drawLines(line_t, paint); paint.setPathEffect(new DashPathEffect(new float[] {20, 5}, 0)); canvas.drawLine(line_1[0],line_1[1],lf,getPointY(line_1, lf), paint); canvas.drawLine(rf,getPointY(line_1, rf),line_1[2],line_1[3], paint); paint.setColor(Color.GREEN); canvas.drawLine(line_2[0],line_2[1],lf,getPointY(line_2, lf), paint); canvas.drawLine(rf,getPointY(line_2, rf),line_2[2],line_2[3], paint); paint.setColor(Color.YELLOW); canvas.drawLine(line_3[0],line_3[1],lf,getPointY(line_3, lf), paint); canvas.drawLine(rf,getPointY(line_3, rf),line_3[2],line_3[3], paint); paint.setPathEffect(null); paint.setColor(Color.RED); //左右兩邊線 canvas.drawLines(line_l, paint); canvas.drawLines(line_r, paint); canvas.drawCircle(line_l[0], line_l[1],paint.getStrokeWidth()/2, paint); canvas.drawCircle(line_r[0], line_r[1],paint.getStrokeWidth()/2, paint); }// paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.RED); paint.setPathEffect(null); paint.setXfermode(null); canvas.restoreToCount(layerId); } /** * 獲取坐標(biāo)x * @param line 直線坐標(biāo)系 * @param y y點(diǎn) * @return */ private float getPointX(float[] line,float y){ float x = 0;// Log.d("chr", "line====>"+line.length+":::y====>"+y); float x1 = line[0]; float y1 = line[1]; float x2 = line[2]; float y2 = line[3]; x = ((y-y1)/(y2-y1))*(x2-x1)+x1; return x; } /** * 獲取坐標(biāo)y * @param line:直線坐標(biāo)系 * @param x:x點(diǎn) * @return */ private float getPointY(float[] line,float x){ float y = 0;// Log.d("chr", "line====>"+line.length+":::y====>"+y); float x1 = line[0]; float y1 = line[1]; float x2 = line[2]; float y2 = line[3]; y = ((x-x1)/(x2-x1))*(y2-y1)+y1; return y; } /** * 獲取點(diǎn)應(yīng)該移動(dòng)到的Y坐標(biāo) * @param line:線段坐標(biāo) * @param y:滑動(dòng)時(shí)的y坐標(biāo) * @return */ private float getMoveY(float[] line,float y){ if(y<=line[1]+ radiu){ y=line[1]+radiu*2.0f; } if(y>=line[3]-radiu*2.0f){ y=line[3]-radiu*2.0f; } return y; } /** * * @param direction 獲取虛線長度 * @return */ private float getDashLineLength(int direction){ float length = 0; int a = 20; switch (direction) { case D_LEFT: length = line_t[0]+(line_t[2]-line_t[0])/a; break; case D_RIGHT: length = line_t[0]+(a-1)*(line_t[2]-line_t[0])/a; break; } return length; } /** * 獲取最小值 * @return */ private float getMinPoint(float point[]){ float min = point[0]; for(int i=0;i
感謝各位的閱讀!關(guān)于“Android如何自定義view實(shí)現(xiàn)車載可調(diào)整軌跡線”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!