android 车辆轨迹,Android自定义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 =

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android倒车轨迹线实现方法可以分为两种,一种是使用Camera2 API获取摄像头预览画面并在上面绘制轨迹线,另一种是使用第三方库实现。 1. 使用Camera2 API获取摄像头预览画面并在上面绘制轨迹线 首先需要在AndroidManifest.xml文件中添加摄像头权限: ```xml <uses-permission android:name="android.permission.CAMERA" /> ``` 然后在布局文件中添加TextureView控件用于预览摄像头画面: ```xml <TextureView android:id="@+id/textureView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 接下来,在Activity中获取TextureView控件并打开后置摄像头: ```java private TextureView textureView; private CameraDevice cameraDevice; private CameraCaptureSession cameraCaptureSession; private CaptureRequest.Builder captureRequestBuilder; private HandlerThread backgroundThread; private Handler backgroundHandler; private Size imageDimension; private void openCamera() { CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { String cameraId = manager.getCameraIdList()[0]; CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId); StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); imageDimension = map.getOutputSizes(SurfaceTexture.class)[0]; manager.openCamera(cameraId, stateCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } } private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() { @Override public void onOpened(@NonNull CameraDevice camera) { cameraDevice = camera; createCameraPreview(); } @Override public void onDisconnected(@NonNull CameraDevice camera) { cameraDevice.close(); } @Override public void onError(@NonNull CameraDevice camera, int error) { cameraDevice.close(); cameraDevice = null; } }; private void createCameraPreview() { SurfaceTexture texture = textureView.getSurfaceTexture(); texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight()); Surface surface = new Surface(texture); try { captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); captureRequestBuilder.addTarget(surface); cameraDevice.createCaptureSession(Collections.singletonList(surface), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession session) { if (cameraDevice == null) return; cameraCaptureSession = session; updatePreview(); } @Override public void onConfigureFailed(@NonNull CameraCaptureSession session) { } }, null); } catch (CameraAccessException e) { e.printStackTrace(); } } private void updatePreview() { if (cameraDevice == null) return; captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); try { cameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, backgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } ``` 以上代码中,首先通过CameraManager获取后置摄像头的ID,然后获取该摄像头的特性和输出尺寸,在onOpened()方法中创建CameraCaptureSession并设置预览画面,最后在updatePreview()方法中更新预览画面。 接下来,可以在预览画面上绘制倒车轨迹线。通过TextureView控件获取画布并绘制轨迹线: ```java private void drawPath() { Paint paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); paint.setColor(Color.RED); Bitmap bitmap = textureView.getBitmap(); Canvas canvas = textureView.lockCanvas(); canvas.drawBitmap(bitmap, 0, 0, null); Path path = new Path(); path.moveTo(0, textureView.getHeight() / 2); path.lineTo(textureView.getWidth(), textureView.getHeight() / 2); canvas.drawPath(path, paint); textureView.unlockCanvasAndPost(canvas); } ``` 以上代码中,首先获取TextureView控件的Bitmap对象,然后获取画布并绘制预览画面,最后绘制轨迹线并释放画布。 2. 使用第三方库实现 除了使用Camera2 API自行实现倒车轨迹线外,也可以使用第三方库实现。常用的第三方库有CameraView和CameraKit-Android。 以CameraView为例,首先需要在build.gradle文件中添加依赖: ```groovy implementation 'com.otaliastudios:cameraview:2.7.2' ``` 然后在布局文件中添加CameraView控件: ```xml <com.otaliastudios.cameraview.CameraView android:id="@+id/cameraView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 接下来,在Activity中获取CameraView控件并设置预览画面: ```java private CameraView cameraView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cameraView = findViewById(R.id.cameraView); cameraView.setLifecycleOwner(this); cameraView.addFrameProcessor(frame -> drawPath()); } ``` 以上代码中,首先获取CameraView控件并设置生命周期,然后添加FrameProcessor用于在预览画面上绘制倒车轨迹线。 最后,实现drawPath()方法绘制倒车轨迹线: ```java private void drawPath() { Paint paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); paint.setColor(Color.RED); Bitmap bitmap = cameraView.getBitmap(); Canvas canvas = cameraView.getPreviewCanvas(); Path path = new Path(); path.moveTo(0, cameraView.getHeight() / 2); path.lineTo(cameraView.getWidth(), cameraView.getHeight() / 2); canvas.drawPath(path, paint); } ``` 以上代码中,首先获取CameraView控件的Bitmap对象,然后获取预览画面的画布并绘制轨迹线

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值