gpt4 book ai didi

android - 使用 RectF 和 Canvas 绘制圆角矩形?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:02 24 4
gpt4 key购买 nike

我正在尝试使用 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 进行缩放。

最佳答案

实际上这里你的 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 = 200top = bottom = 400 代表一个 Point

如果你想画一个width = 200height = 400Rect,那么你的RectF应该是

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

对于 width = 400 and height = 200RectF 应该是

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

关于android - 使用 RectF 和 Canvas 绘制圆角矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21934531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com