直播商城系统源码,自定义View实现方向控制控件,可拖拽中间圆

发布时间 2023-08-02 14:06:11作者: 云豹科技-苏凌霄

直播商城系统源码,自定义View实现方向控制控件,可拖拽中间圆

 

public class DirectionView extends View implements View.OnTouchListener {
    private int width;
    private int height;
    private int halfWidth;
    private int halfHeight;
    private int smallCircleRadius;
 
    private int sideWidth = 4;//外层边框宽度
 
    private Paint paintCircleSide;
    private Paint paintCircleBg;
    private Paint paintCircleCenter;
    private Paint paintDirection;
 
    private int pressDirectionH = 0;
    private int pressDirectionV = 0;
 
    private int smallCircleCenterX = -1;
    private int smallCircleCenterY = -1;
    private int halfTouchWidth = 65;
 
    private BlurMaskFilter blurMaskFilter;
    private Path path = new Path();
 
    public DirectionView(Context context) {
        super(context);
        init(context);
    }
 
    public DirectionView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
 
    public DirectionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
 
    private void init(Context context)
    {
        paintCircleSide = createPaintWithCommon(sideWidth, "#66ffffff", Paint.Style.STROKE);
        paintCircleBg = createPaintWithCommon(2, "#662a3845", Paint.Style.FILL);
        paintCircleCenter = createPaintWithCommon(2, "#ffffff", Paint.Style.FILL);
        paintDirection = createPaintWithCommon(6, "#ffffff", Paint.Style.STROKE);
 
        blurMaskFilter = new BlurMaskFilter(20, BlurMaskFilter.Blur.SOLID);
 
        this.setOnTouchListener(this);
    }
 
    private Paint createPaintWithCommon(int strokeWidth, String color, Paint.Style style)
    {
        Paint paint = new Paint();
        paint.setStrokeWidth(strokeWidth);
        paint.setColor(Color.parseColor(color));
        paint.setStyle(style);
        paint.setAntiAlias(true);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        return paint;
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
 
        canvas.drawCircle(halfWidth, halfHeight, halfWidth - sideWidth / 2, paintCircleBg);
        canvas.drawCircle(halfWidth, halfHeight, halfWidth - sideWidth, paintCircleSide);
 
        if(this.smallCircleCenterY != -1)
        {
//            paintCircleCenter.setMaskFilter(blurMaskFilter);
            canvas.drawCircle(smallCircleCenterX, smallCircleCenterY, smallCircleRadius, paintCircleCenter);
        }
        else
        {
//            paintCircleCenter.setMaskFilter(null);
            canvas.drawCircle(halfWidth, halfHeight, smallCircleRadius, paintCircleCenter);
        }
 
        int offsetSize = 25;
        int offsetPosition = 20;
 
        //上
        if(pressDirectionV == PtzAction.Direction.Up)
            paintDirection.setColor(Color.parseColor("#0000ff"));
        else
            paintDirection.setColor(Color.parseColor("#ffffff"));
 
        path.reset();
        path.moveTo(halfWidth - offsetSize, halfHeight / 2 - offsetPosition);
        path.lineTo(halfWidth, halfHeight / 2 - offsetSize - offsetPosition);
        path.lineTo(halfWidth + offsetSize, halfHeight / 2 - offsetPosition);
        canvas.drawPath(path, paintDirection);
 
        //右
        if(pressDirectionH == PtzAction.Direction.Right)
            paintDirection.setColor(Color.parseColor("#0000ff"));
        else
            paintDirection.setColor(Color.parseColor("#ffffff"));
 
        path.reset();
        path.moveTo(halfWidth + halfWidth / 2 + offsetPosition, halfHeight - offsetSize);
        path.lineTo(halfWidth + halfWidth / 2 + offsetPosition + offsetSize, halfHeight);
        path.lineTo(halfWidth + halfWidth / 2 + offsetPosition, halfHeight + offsetSize);
        canvas.drawPath(path, paintDirection);
 
        //下
        if(pressDirectionV == PtzAction.Direction.Down)
            paintDirection.setColor(Color.parseColor("#0000ff"));
        else
            paintDirection.setColor(Color.parseColor("#ffffff"));
 
        path.reset();
        path.moveTo(halfWidth - offsetSize, halfHeight + halfHeight / 2 + offsetPosition);
        path.lineTo(halfWidth, halfHeight + halfHeight / 2 + offsetSize + offsetPosition);
        path.lineTo(halfWidth + offsetSize, halfHeight + halfHeight / 2 + offsetPosition);
        canvas.drawPath(path, paintDirection);
 
        //左
        if(pressDirectionH == PtzAction.Direction.Left)
            paintDirection.setColor(Color.parseColor("#0000ff"));
        else
            paintDirection.setColor(Color.parseColor("#ffffff"));
 
        path.reset();
        path.moveTo(halfWidth / 2 - offsetPosition, halfHeight - offsetSize);
        path.lineTo(halfWidth / 2 - offsetPosition - offsetSize, halfHeight);
        path.lineTo(halfWidth / 2 - offsetPosition, halfHeight + offsetSize);
        canvas.drawPath(path, paintDirection);
    }

 

 以上就是直播商城系统源码,自定义View实现方向控制控件,可拖拽中间圆, 更多内容欢迎关注之后的文章