使用RectF和帆布画圆角矩形? [英] Draw rounded rectangle using RectF and canvas?

查看:365
本文介绍了使用RectF和帆布画圆角矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制使用RectF和canvas.drawRoundRect()一个圆角矩形。请参考下面我的code:

I'm attempting to draw a rounded rectangle using RectF and canvas.drawRoundRect(). Please see my code below:

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);

        }
    }

}

该程序运行,但没有被抽即我只是得到我的白色背景画面。任何想法,为什么?

The program runs but nothing gets drawn i.e. I just get my white background screen. Any ideas as to why?

请注意:我在编程创建我相对布局,而不是使用XML换算目的

Note: I'm creating my relative layout programatically as opposed to using XML for scaling purposes.

推荐答案

其实这里您 RectF 重新presenting 不是长方形,这就是为什么你无法看到矩形 ...

Actually here your RectF representing Point not Rectangle, that's why you are unable to see Rect...

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

在这里 RectF

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

在这里左=右= 200 顶=底部= 400 从而重新presents一个

here left = right = 200 and top = bottom = 400 which represents a Point

如果你想画一个矩形 WIDTH = 200 高度= 400 ,那么你的 RectF

if you want to draw a Rect of width = 200 and height = 400, then your RectF should be

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

宽度= 400和高度= 200 RectF

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

这篇关于使用RectF和帆布画圆角矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆