Android 倒车影像车道线

1、直接上代码

public class LaneLineView extends View {    private Paint paint;    private Camera camera;    private Matrix matrix;    //尺度标记得长短,可调整    private int tran = 200;    //笔粗细,由近到远是由粗到细,变化跟倾斜得角度有关,可调整    private int strokeWidth = 70;    public LaneLineView(Context context) {        super(context);        init();    }    public LaneLineView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        init();    }    private void init(){        paint = new Paint();        paint.setAntiAlias(true);        paint.setStyle(Paint.Style.FILL_AND_STROKE);        paint.setStrokeWidth(strokeWidth);        //矩阵变化所需要的东西        camera = new Camera();        matrix = new Matrix();    }}

2、然后开始画线,重写onDraw方法

    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);                nt height = getHeight();        int width = getWidth();        //每个刻度之间的长度,可调整        int h_coefficient = 1000;        //左右边界距离,可调整        int w_coefficient = width / 6;        //可适当调整俩条线之间的角度        int skewing = 300;        //计算画布旋转角度        double v = Math.sqrt(Math.pow(h_coefficient, 2) - Math.pow(skewing, 2));        double angle = angle(v, skewing, h_coefficient);        long round = Math.round(angle);        //需要将画布旋转得角度        if(round != 0){            canvas.rotate(-round, w_coefficient, height);        }        paint.setColor(Color.parseColor("#dc474e"));        canvas.drawLine(w_coefficient, height, w_coefficient, height - h_coefficient, paint);        paint.setColor(Color.parseColor("#f2f542"));        canvas.drawLine(w_coefficient, height - h_coefficient, w_coefficient, height - h_coefficient * 2, paint);        paint.setColor(Color.parseColor("#4ee826"));        canvas.drawLine(w_coefficient, height - h_coefficient * 2, w_coefficient, height - h_coefficient * 3, paint);        canvas.drawLine(w_coefficient, height - h_coefficient * 3, w_coefficient, height - h_coefficient * 4, paint);        paint.setColor(Color.parseColor("#dc474e"));        canvas.drawLine(w_coefficient - strokeWidth / 2, height - h_coefficient, w_coefficient - strokeWidth / 2 + tran, height - h_coefficient, paint);        paint.setColor(Color.parseColor("#f2f542"));        canvas.drawLine(w_coefficient - strokeWidth / 2, height - h_coefficient * 2, w_coefficient - strokeWidth / 2 + tran, height - h_coefficient * 2, paint);        paint.setColor(Color.parseColor("#4ee826"));        canvas.drawLine(w_coefficient - strokeWidth / 2, height - h_coefficient * 3, w_coefficient - strokeWidth / 2 + tran, height - h_coefficient * 3, paint);        canvas.drawLine(w_coefficient - strokeWidth / 2, height - h_coefficient * 4, w_coefficient - strokeWidth / 2 + tran, height - h_coefficient * 4, paint);        if(round != 0){            //上面画布被逆时针旋转了,所以先要把画布归位,再旋转画布            canvas.rotate(round, w_coefficient, height);            canvas.rotate(round, getWidth() - w_coefficient, height);        }        paint.setColor(Color.parseColor("#dc474e"));        canvas.drawLine(getWidth() - w_coefficient, height, getWidth() - w_coefficient, height - h_coefficient, paint);        paint.setColor(Color.parseColor("#f2f542"));        canvas.drawLine(getWidth() - w_coefficient, height - h_coefficient, getWidth() - w_coefficient, height - h_coefficient * 2, paint);        paint.setColor(Color.parseColor("#4ee826"));        canvas.drawLine(getWidth() - w_coefficient, height - h_coefficient * 2, getWidth() - w_coefficient, height - h_coefficient * 3, paint);        canvas.drawLine(getWidth() - w_coefficient, height - h_coefficient * 3, getWidth() - w_coefficient, height - h_coefficient * 4, paint);        paint.setColor(Color.parseColor("#dc474e"));        canvas.drawLine(getWidth() - w_coefficient + strokeWidth / 2, height - h_coefficient, getWidth() - w_coefficient + strokeWidth / 2 - tran, height - h_coefficient, paint);        paint.setColor(Color.parseColor("#f2f542"));        canvas.drawLine(getWidth() - w_coefficient + strokeWidth / 2, height - h_coefficient * 2, getWidth() - w_coefficient + strokeWidth / 2 - tran, height - h_coefficient * 2, paint);        paint.setColor(Color.parseColor("#4ee826"));        canvas.drawLine(getWidth() - w_coefficient + strokeWidth / 2, height - h_coefficient * 3, getWidth() - w_coefficient + strokeWidth / 2 - tran, height - h_coefficient * 3, paint);        canvas.drawLine(getWidth() - w_coefficient + strokeWidth / 2, height - h_coefficient * 4, getWidth() - w_coefficient + strokeWidth / 2 - tran, height - h_coefficient * 4, paint);        if(round != 0){            //将画布转回原位            canvas.rotate(-round, getWidth() - w_coefficient, height);        }   }      //计算旋转的角度,余弦公式应该知道吧    private double angle(double a, int b, int c){        if(b == 0)return 0;        double a2 = Math.pow(a, 2);        double b2 = Math.pow(b, 2);        double c2 = Math.pow(c, 2);        double acos = Math.acos((a2 + c2 - b2) / (2 * a * c));        double v = Math.toDegrees(acos);        return v;    }

3、到这里指示线就画完了,接下来用矩阵旋转画布,达到3D的效果

@Overrideprotected void onDraw(Canvas canvas) {    super.onDraw(canvas);    camera.save();    matrix.reset();    //z轴移动,z可调整,-放大,+缩小    camera.translate(0, 0, 0);    //旋转度数可以调整    camera.rotateX(50);    camera.getMatrix(matrix);    camera.restore();    //设置坐标位置    matrix.preTranslate(-(getWidth() / 2), -getHeight());    matrix.postTranslate(getWidth() / 2, getHeight());    //将变换后的矩阵给canvashao    canvas.concat(matrix);}

没了,是不是很简单,最后附上代码地址

https://download.csdn.net/download/itacmen1vip/11584446

更多相关文章

  1. Android利用android:indeterminateDrawable来实现ProgressBar三
  2. Android(安卓)笔记
  3. Android计算地图上两点距离
  4. Android(安卓)官方文档:(一)动画和图像 —— 1.5 画布和画图
  5. Android(安卓)图片旋转(使用Matrix.setRotate方法)
  6. Android(安卓)3D 旋转的三角形(四)
  7. ANDROID实现圆形图形不断旋转的动画
  8. android 图片进度条
  9. Android实现图片缩放与旋转

随机推荐

  1. Android Gson类型转换错误解决 com.googl
  2. Android:控件ProgressBar进度条
  3. android自定义带图片的title
  4. android数据库操作
  5. 开源项目收集整理
  6. android 4.2 compile on 64 bits Ubuntu
  7. android:duplicateParentState="true"
  8. Android仿QQ圆形头像
  9. android 模拟器 得到GPS
  10. Android特色开发(5):账户管理