如何设置通过 onDraw 绘制的自定义视图的尺寸和位置?

How can I set the dimension and position of a custom view drawn via onDraw?

提问人:ema3272 提问时间:2/24/2023 更新时间:3/7/2023 访问量:31

问:

我通过扩展 .此类实现构造函数和方法。有了这些,就可以正确绘制视图。但是,它的尺寸是其父级的尺寸。我尝试添加 and 方法(在下面的代码中注释),但随后不再绘制自定义视图......ViewonDrawonMeasureonLayout

我的最终目标是能够在同一视图中绘制多个自定义视图,并能够为每个视图附加一个。 如何解决此问题?FrameLayoutSimpleOnGestureListener

public class ScArc extends View {
RectF oval = new RectF();
Path path = new Path();
Paint paint = new Paint();
float left = 0;
float top = 0;
float right = 0;
float bottom = 0;
float startAngle = 0;
float sweepAngle;

public ScArc(Context context, float X1, float Y1, float X2, float Y2, ScMain.Azimuth azimuth, int sweepAngle,
             String strokeWidthName, String strokeColorName, int idx, boolean isForcingChain) {
    super(context);

    ...
    
    oval.set(left, top, right, bottom);
    path.addArc(oval, startAngle, sweepAngle);

    this.setClickable(true);
    this.setEnabled(true);
}

@Override
public void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
}

//    @Override
//    public boolean onTouchEvent (MotionEvent event) {
//        if (((event.getSource() == SOURCE_MOUSE) || (event.getSource() == SOURCE_TOUCHPAD))
//                && (event.getAction() == MotionEvent.ACTION_DOWN)
//                && event.isButtonPressed(MotionEvent.BUTTON_SECONDARY)) {
//    
//            int tag = (int) this.getTag();
//            drawnStrongLinks.remove(drawnStrongLinks.indexOf(tag));
//    
//            FrameLayout drawingFrame = (FrameLayout) this.getParent();
//            drawingFrame.removeView(this);
//        }
//        return true;
//    }

//    @Override
//    protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
//        super.onLayout(changed, (int) oval.left, (int) oval.top, (int) oval.right, (int) oval.bottom);
//    }

//    @Override
//    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        this.setMeasuredDimension((int) (oval.right - oval.left), (int) (oval.bottom - oval.top));
//    }

}
Java Android OnDraw

评论


答:

0赞 ema3272 3/7/2023 #1

我没有在视图级别工作,而是在父级别实现了逻辑:

  • 自定义视图只有一个构造函数和方法onDraw

  • 父级有一个,我在其中分析触摸事件坐标以确定单击了哪个视图SimpleOnGestureListener

     @Override
     public boolean onDown(MotionEvent event) {
         if (((event.getSource() == SOURCE_MOUSE) || (event.getSource() == SOURCE_TOUCHPAD))
                 && (event.isButtonPressed(MotionEvent.BUTTON_SECONDARY))) {
             FrameLayout drawingFrame = SCFrame.findViewById(R.id.drawingFrame);
             if (drawingFrame.getChildCount() > 0) {
                 alignmentSquare = SCFrame.findViewById(R.id.alignmentSquare);
                 RectF touchZone = new RectF();
                 touchZone.set((float) (event.getX() - alignmentSquare.getWidth() - 0.5 * textViewWidth),
                             (float) (event.getY() - alignmentSquare.getHeight() - 0.5 * textViewHeight),
                             (float) (event.getX() - alignmentSquare.getWidth() + 0.5 * textViewWidth),
                             (float) (event.getY() - alignmentSquare.getHeight() + 0.5 * textViewHeight));
                 int tag = -1;
                 for (int i = 0; i < drawingFrame.getChildCount(); i++) {
                     View v = drawingFrame.getChildAt(i);
                     if ((v instanceof ScArc) && touchZone.intersect(((ScArc) v).oval)) {
                         tag = (int) v.getTag();
                         break;
                     }
                 }
    
             ...
             }
         }
     }