【问题标题】:Dynamically draw outline oval over a view在视图上动态绘制椭圆轮廓
【发布时间】:2017-05-21 21:58:33
【问题描述】:

我正在使用自定义 UI 元素突出显示。所以我需要在运行时的特定视图上绘制一个半椭圆形。

例如: 我必须在白色椭圆上画一个黄色的半椭圆。

我们将不胜感激。

【问题讨论】:

  • canvas 可以用来画这个。你试过了吗?
  • @santosh 你能帮助我如何朝着这个方向前进吗?

标签: android xml android-layout android-shape android-shapedrawable


【解决方案1】:
public class MyView extends View {

    public MyView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {

        // TODO Auto-generated method stub
        super.onDraw(canvas);
        float width = (float) getWidth();
        float height = (float) getHeight();
        float radius;

        if (width > height) {
         radius = height / 4;
        } else {
         radius = width / 4;
        }

        Path path = new Path();
        path.addCircle(width / 2,
         height / 2, radius,
         Path.Direction.CW);

        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.FILL);

        float center_x, center_y;
        final RectF oval = new RectF();
        paint.setStyle(Paint.Style.STROKE);

        center_x = width / 2;
        center_y = height / 2;

        oval.set(center_x - radius,
            center_y - radius,
            center_x + radius,
            center_y + radius);
        canvas.drawArc(oval, 90, 180, false, paint);
    }
}

添加

new MyView(this)

【讨论】:

  • 轮廓视图不是一个圆圈,我希望它绘制在视图的边界上。
猜你喜欢
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 2020-04-10
相关资源
最近更新 更多