SoFunction
Updated on 2025-03-02

Android custom view enables adjustable track lines on the vehicle

This article shares the specific code for Android custom view to complete the on-board adjustment track line for your reference. The specific content is as follows

Take the view made by your colleague and make a record.

/**
 * 
 */
package ;
 
import ;
 
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
 
/**
 * @author chenhanrong
 *
 */
public class CCView extends View implements {
 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);
  = context;
 init();
 }
 
 
 
 /**
  * Initialize the control
  */
 private void init() {
 paint = new Paint();
  /**
    * Remove the serration
    */
 (true);
 /**
   * Set the paint color
   */
 ();
 /**
   * Set the paint style
   */
 ();
 /**
   * Set the outer frame width of paint
   */
 (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) {
 (canvas);
 (0x00000000, );
// (new PorterDuffXfermode(.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 = ();
  int canvasHeight = ();
  int layerId = (0, 0, canvasWidth, canvasHeight, null, Canvas.ALL_SAVE_FLAG);
// (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){ 
 //Four horizontal lines (line_t, paint);
 (new DashPathEffect(new float[] {20, 5}, 0)); 
 (line_1, paint);
 ();
 (line_2, paint);
 ();
 (line_3, paint);
 ();
 (null);
 
 //The left and right sides (line_l, paint); 
 (line_r, paint);
 
 
 //The intersection of three horizontal lines in the middle (line_1[0], line_1[1],radiu, paint);
 (line_1[2], line_1[3],radiu, paint);
 ();
 (line_2[0], line_2[1],radiu, paint);
 (line_2[2], line_2[3],radiu, paint);
 ();
 (line_3[0], line_3[1],radiu, paint);
 (line_3[2], line_3[3],radiu, paint);
 ();
 //Four o'clock left and right (line_l[0], line_l[1],radiu, paint);
 (line_l[2], line_l[3],radiu, paint);
 
 (line_r[0], line_r[1],radiu, paint);
 (line_r[2], line_r[3],radiu, paint);
 
 }else{
 float lf=getDashLineLength(D_LEFT);
 float rf=getDashLineLength(D_RIGHT);
 (line_t, paint);
 (new DashPathEffect(new float[] {20, 5}, 0));
 (line_1[0],line_1[1],lf,getPointY(line_1, lf), paint);
 (rf,getPointY(line_1, rf),line_1[2],line_1[3], paint);
 (); 
 (line_2[0],line_2[1],lf,getPointY(line_2, lf), paint);
 (rf,getPointY(line_2, rf),line_2[2],line_2[3], paint);
 ();
 (line_3[0],line_3[1],lf,getPointY(line_3, lf), paint);
 (rf,getPointY(line_3, rf),line_3[2],line_3[3], paint);
 (null);
 ();
 //The left and right sides (line_l, paint); 
 (line_r, paint);
 (line_l[0], line_l[1],()/2, paint);
 (line_r[0], line_r[1],()/2, paint);
 }
// ();
 ();
 (null);
 (null);
 (layerId);
 }
 /**
  * Get coordinate x
  * @param line linear coordinate system
  * @param y point
  * @return
  */
 private float getPointX(float[] line,float y){
 float x = 0;
// ("chr", "line====>"++":::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;
 }
 /**
  * Get coordinate y
  * @param line: Linear coordinate system
  * @param x:x dots
  * @return
  */
 
 private float getPointY(float[] line,float x){
 float y = 0;
// ("chr", "line====>"++":::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;
 }
 /**
  * Get the Y coordinate to which the point should be moved
  * @param line: Line segment coordinates
  * @param y: y coordinates when sliding
  * @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 Get the dotted line length
  * @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;
 }
 
 /**
  * Get the minimum value
  * @return
  */
 private float getMinPoint(float point[]){
 float min = point[0];
 for(int i=0;i<;i++){
 if(min>point[i]){
 min=point[i];
 }
 }
 return min;
 }
 
 /**
  * Get the maximum value
  * @return
  */
 private float getMaxPoint(float point[]){
 float max = point[0];
 for(int i=0;i<;i++){
 if(max<point[i]){
 max=point[i];
 }
 }
 return max;
 }
 /**
  * Get the y-axis limit point
  * @param point
  * @param y
  * @param type
  * @return
  */
 private float getLimtMoveY(float point[],float y,int type){
 
 if(type == TYPE_MIN){
 float limt = getMinPoint(point);
 if(y>limt-radiu*2.0f){
 y = limt-radiu*2.0f;
 }
 }
 if(type == TYPE_MAX){
 float limt2 = getMaxPoint(point);
 if(y<limt2+radiu*2.0f){
 y = limt2+radiu*2.0f;
 }
 }
 
 return y;
 }
 
 
 public void proofView(int direction){
 switch (direction) {
 case D_LEFT:
 line_1[3] = line_1[1];
 line_1[2] = getPointX(line_r, line_1[1]);
 
 line_2[3] = line_2[1];
 line_2[2] = getPointX(line_r, line_2[1]);
 
 line_3[3] = line_3[1];
 line_3[2] = getPointX(line_r, line_3[1]);
 
 break;
 
 case D_RIGHT:
 line_1[1] = line_1[3];
 line_1[0] = getPointX(line_l, line_1[3]);
 
 line_2[1] = line_2[3];
 line_2[0] = getPointX(line_l, line_2[3]);
 
 line_3[1] = line_3[3];
 line_3[0] = getPointX(line_l, line_3[3]);
 
 break;
 }
 
 invalidate();
 }
 
 
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 // TODO Auto-generated method stub
 float x =();
 float y =();
 
 switch (()) {
 case MotionEvent.ACTION_DOWN:
 if(showPoint){ 
 isMove = false;
 ("chr", "MotionEvent.ACTION_DOWN::x====>"+x+"::::y===>"+y);
 if(x<=(line_l[0]+radiu*2.0f) 
  && x>=(line_l[0]-radiu*2.0f)
  && y>=(line_l[1]-radiu*2.0f)
  &&y<=(line_l[1]+radiu*2.0f)){
 cmP1 = true;
 }
 else if(x<=(line_r[0]+radiu*2.0f) 
  && x>=(line_r[0]-radiu*2.0f)
  && y>=(line_r[1]-radiu*2.0f)
  &&y<=(line_r[1]+radiu*2.0f)){
 cmP2 = true;
 }else if(x<=(line_l[2]+radiu*2.0f )
  && x>=(line_l[2]-radiu*2.0f)
  && y>=(line_l[3]-radiu*2.0f)
  &&y<=(line_l[3]+radiu*2.0f)){
 cmP3 = true;
 }else if(x<=(line_r[2]+radiu*2.0f )
  && x>=(line_r[2]-radiu*2.0f)
  && y>=(line_r[3]-radiu*2.0f)
  &&y<=(line_r[3]+radiu*2.0f)){
 cmP4 = true;
 }else if(x<=(line_1[0]+radiu*2.0f )
  && x>=(line_1[0]-radiu*2.0f)
  && y>=(line_1[1]-radiu*2.0f)
  &&y<=(line_1[1]+radiu*2.0f)){
 cmP5 = true;
 }else if(x<=(line_1[2]+radiu*2.0f )
  && x>=(line_1[2]-radiu*2.0f)
  && y>=(line_1[3]-radiu*2.0f)
  &&y<=(line_1[3]+radiu*2.0f)){
 cmP6 = true;
 }else if(x<=(line_2[0]+radiu*2.0f )
  && x>=(line_2[0]-radiu*2.0f)
  && y>=(line_2[1]-radiu*2.0f)
  &&y<=(line_2[1]+radiu*2.0f)){
 cmP7 = true;
 }else if(x<=(line_2[2]+radiu*2.0f)
  && x>=(line_2[2]-radiu*2.0f)
  && y>=(line_2[3]-radiu*2.0f)
  &&y<=(line_2[3]+radiu*2.0f)){
 cmP8 = true;
 }else if(x<=(line_3[0]+radiu *2.0f)
  && x>=(line_3[0]-radiu*2.0f)
  && y>=(line_3[1]-radiu*2.0f)
  &&y<=(line_3[1]+radiu*2.0f)){
 cmP9 = true;
 }else if(x<=(line_3[2]+radiu*2.0f )
  && x>=(line_3[2]-radiu*2.0f)
  && y>=(line_3[3]-radiu*2.0f)
  &&y<=(line_3[3]+radiu*2.0f)){
 cmP10 = true;
 }
 }
 
 break;
 
 case MotionEvent.ACTION_MOVE:
 float[] point_L = new float[]{line_1[1],line_2[1],line_3[1]};
 float[] point_R = new float[]{line_1[3],line_2[3],line_3[3]};
 if(cmP1){
 isMove = true;
 
 line_l[0] =x;
 line_l[1] =getLimtMoveY(point_L, y, TYPE_MIN);
 line_1[0]= getPointX(line_l,line_1[1]);
 line_2[0]= getPointX(line_l,line_2[1]);
 line_3[0]= getPointX(line_l,line_3[1]);
 }else if(cmP2){
 isMove = true;
 line_r[0] = x;
 line_r[1] = getLimtMoveY(point_R, y, TYPE_MIN);
 line_1[2]= getPointX(line_r,line_1[3]);
 line_2[2]= getPointX(line_r,line_2[3]);
 line_3[2]= getPointX(line_r,line_3[3]);
 }else if(cmP3){
 isMove = true;
 line_l[2] =x;
 line_l[3] =getLimtMoveY(point_L, y, TYPE_MAX);
 line_1[0]= getPointX(line_l,line_1[1]);
 line_2[0]= getPointX(line_l,line_2[1]);
 line_3[0]= getPointX(line_l,line_3[1]);
 }else if(cmP4){
 isMove = true;
 line_r[2] = x;
 line_r[3] = getLimtMoveY(point_R, y, TYPE_MAX);
 line_1[2]= getPointX(line_r,line_1[3]);
 line_2[2]= getPointX(line_r,line_2[3]);
 line_3[2]= getPointX(line_r,line_3[3]);
 }else if(cmP5){
 isMove = true;
 y=getMoveY(line_l,y);
 line_1[0]= getPointX(line_l, y);
 line_1[1]= y;
 }else if(cmP6){
 isMove = true;
 y=getMoveY(line_r,y);
 line_1[2]= getPointX(line_r, y);
 line_1[3]= y;
 }else if(cmP7){
 isMove = true;
 y=getMoveY(line_l,y);
 line_2[0]= getPointX(line_l, y);
 line_2[1]= y;
 }else if(cmP8){
 isMove = true;
 y=getMoveY(line_r,y);
 line_2[2]= getPointX(line_r, y);
 line_2[3]= y;
 }else if(cmP9){
 isMove = true;
 y=getMoveY(line_l,y);
 line_3[0]= getPointX(line_l, y);
 line_3[1]= y;
 }else if(cmP10){
 isMove = true;
 y=getMoveY(line_r,y);
 line_3[2]= getPointX(line_r, y);
 line_3[3]= y;
 } 
 invalidate(); 
 break;
 case MotionEvent.ACTION_UP:
 cmP1= false;
 cmP2= false;
 cmP3= false;
 cmP4= false;
 
 cmP5= false;
 cmP6= false;
 
 cmP7= false;
 cmP8= false;
 
 cmP9= false;
 cmP10= false;
 if(!isMove)
 showPoint = !showPoint;
 invalidate();
 break;
 }
 return (event);
 }
 
 
 @Override
 public void onClick(View v) {
 }
 
 
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.