使用RectF和画布绘制圆角矩形?

6

我试图使用RectF和canvas.drawRoundRect()绘制圆角矩形。请看下面的代码:

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.graphics.RectF;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create main RL params
        RelativeLayout.LayoutParams rlMainlayoutParams 
                = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

        // Create main relative layout
        RelativeLayout rlMain = new RelativeLayout(this);
        rlMain.setLayoutParams(rlMainlayoutParams);
        //rlMain.setBackgroundResource(R.drawable.bgndlogin);
        rlMain.setBackgroundColor(Color.WHITE);

        RectF rectf = new RectF(200, 400, 200, 400);
        CustomRectangle customRectangle = new CustomRectangle(this, rectf, 5, 5, "#FFFF00");

        //
        rlMain.addView(customRectangle);

        setContentView(rlMain);
    }

    //!< Draw the log in rectangle shaped panel
    public class CustomRectangle extends View {
        Paint paint;
        float left_side, top_side;
        String color;
        RectF rectf;

        //!< Constructor for the log in rectangle shaped panel
        public CustomRectangle(Context context, RectF rectf, float left_side, float top_side, String color) {
            super(context);

            this.rectf = rectf;
            this.left_side= left_side;
            this.top_side = top_side;
            this.color = color;

        }

        //!< Implement to draw the rectangle
        @Override
        public void onDraw(Canvas canvas) {
            paint = new Paint();
            paint.setColor(Color.parseColor(color));
            paint.setStrokeWidth(3);
            //paint.setAlpha(61);

            canvas.drawRoundRect(rectf, left_side, top_side, paint);

        }
    }

}

程序运行,但是没有任何东西被绘制,也就是说我只看到了白色背景屏幕。有什么想法吗?
注意:我是通过编程方式创建相对布局而不是使用 XML 进行缩放。
4个回答

17

实际上,这里你的RectF代表的是Point而不是Rectangle,这就是为什么你看不到Rect的原因...

RectF rectF = new RectF(left, top, right, bottom);

这里是RectF

RectF rectf = new RectF(200, 400, 200, 400); // representing Point

这里 left = right = 200 并且 top = bottom = 400,表示一个 Point

如果你想要画一个宽为 200,高为 400Rect,那么你的 RectF 应该是:

RectF rectf = new RectF(0, 0, 200, 400);

对于 宽度=400和高度=200RectF 应该是

RectF rectf = new RectF(0, 0, 400, 200);

2

Android文档所述:

RectF保存了矩形的四个浮点坐标,该矩形由其4个边缘的坐标(左、上、右、下)表示。

RectF rectf = new RectF(left, top, right, bottom);

在这段代码片段中,正如文档所述,RectF参数表示矩形的边缘坐标。
总结一下, 宽度= |左-右| 高度= |上-下|
如果宽度和高度都为0,就像问题中一样,它将不能代表一个视图或对象, 在逻辑上,一个对象的高度、宽度和深度都为0是不可能存在的。

1

大家好。太棒了。我已经按照下面的方式纠正了我的代码,现在我可以看到我的矩形。

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.graphics.RectF;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create main RL params
        RelativeLayout.LayoutParams rlMainlayoutParams 
                = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

        // Create main relative layout
        RelativeLayout rlMain = new RelativeLayout(this);
        rlMain.setLayoutParams(rlMainlayoutParams);
        //rlMain.setBackgroundResource(R.drawable.bgndlogin);
        rlMain.setBackgroundColor(Color.WHITE);

        RectF rectf = new RectF(0, 0, 200, 300);
        CustomRectangle customRectangle = new CustomRectangle(this, rectf, 15, 15, "#FFFF00");

        //
        rlMain.addView(customRectangle);

        setContentView(rlMain);
    }

    //!< Draw the log in rectangle shaped panel
    public class CustomRectangle extends View {
        Paint paint;
        float left_side, top_side;
        String color;
        RectF rectf;

        //!< Constructor for the log in rectangle shaped panel
        public CustomRectangle(Context context, RectF rectf, float left_side, float top_side, String color) {
            super(context);

            this.rectf = rectf;
            this.left_side= left_side;
            this.top_side = top_side;
            this.color = color;

        }

        //!< Implement to draw the rectangle
        @Override
        public void onDraw(Canvas canvas) {
            paint = new Paint();
            paint.setColor(Color.parseColor(color));
            paint.setStrokeWidth(3);
            //paint.setAlpha(61);

            canvas.drawRoundRect(rectf, left_side, top_side, paint);

        }
    }

}

1
您尚未创建适当的矩形。请为您的矩形提供正确的左、上、右、下点,例如: RectF rectf = new RectF(0, 0, 480, 854);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接