我正在尝试使用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进行缩放。
答案 0 :(得分:14)
实际上,您的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
如果您想绘制Rect
width = 200
和height = 400
,那么您的RectF
应为
RectF rectf = new RectF(0, 0, 200, 400);
对于width = 400 and height = 200
,RectF
应为
RectF rectf = new RectF(0, 0, 400, 200);
答案 1 :(得分:1)
您尚未创建正确的矩形。给你的矩形正确的左,上,右,下点如:RectF rectf = new RectF(0, 0, 480, 854);
答案 2 :(得分:0)
干杯队员。发现。我已经更正了我的代码,如下所示,现在我可以看到我的矩形了。
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);
}
}
}
答案 3 :(得分:0)
RectF为矩形保存四个浮点坐标。矩形由其4条边(左,上,右下)的坐标表示。
RectF rectf = new RectF(left, top, right, bottom);
在代码段中,文档所说的RectF Arguments代表Rectangle的边坐标。
作为结论,
width = |左 - 右|
身高= |顶部 - 底部|
如果问题的宽度和高度为0,则它不代表视图或对象,
在逻辑上,对象不可能存在高度,宽度和深度为0.