欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义view实现车载可调整轨迹线

程序员文章站 2023-08-15 22:37:08
本文实例为大家分享了android自定义view完成车载可调整轨迹线的具体代码,供大家参考,具体内容如下 同事做的view,拿过来做个记录。 /** *...

本文实例为大家分享了android自定义view完成车载可调整轨迹线的具体代码,供大家参考,具体内容如下

同事做的view,拿过来做个记录。

/**
 * 
 */
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);
 /**
  * 设置paint的颜色
  */
 paint.setcolor(color.red);
 /**
  * 设置paint的 style
  */
 paint.setstyle(paint.style.fill);
 /**
  * 设置paint的外框宽度
  */
 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);
 
 
 //中间三条横线交点
 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);
 //左右四点
 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);
 }
 /**
 * 获取坐标x
 * @param line 直线坐标系
 * @param y y点
 * @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;
 }
 /**
 * 获取坐标y
 * @param line:直线坐标系
 * @param x:x点
 * @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;
 }
 /**
 * 获取点应该移动到的y坐标
 * @param line:线段坐标
 * @param y:滑动时的y坐标
 * @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<point.length;i++){
 if(min>point[i]){
 min=point[i];
 }
 }
 return min;
 }
 
 /**
 * 获取最大值
 * @return
 */
 private float getmaxpoint(float point[]){
 float max = point[0];
 for(int i=0;i<point.length;i++){
 if(max<point[i]){
 max=point[i];
 }
 }
 return max;
 }
 /**
 * 获取y轴限制点
 * @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 =event.getx();
 float y =event.gety();
 
 switch (event.getaction()) {
 case motionevent.action_down:
 if(showpoint){ 
 ismove = false;
 log.d("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 super.ontouchevent(event);
 }
 
 
 @override
 public void onclick(view v) {
 }
 
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。