Reputation: 771
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?
Note: I'm creating my relative layout programatically as opposed to using XML for scaling purposes.
Upvotes: 6
Views: 23251
Reputation: 21
As in Android Docs
RectF holds four float coordinates for a rectangle. The rectangle is represented by the coordinates of its 4 edges (left, top, right bottom).
RectF rectf = new RectF(left, top, right, bottom);
In the snippet, the RectF Arguments as the docs say represents the edges coordinates of the Rectangle.
As a conclusion,
width = | left - right |
height = | top - bottom |
If the width and height were 0 as in the question it would not represent a view nor an object,
which in logic is impossible for an object to exist of the height, width and depth of 0.
Upvotes: 2
Reputation: 771
Cheers guys. Spot on. I've corrected my code as seen below and now I can see my rectangle.
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);
}
}
}
Upvotes: 1
Reputation: 2613
You have not created proper rectangle. Give your rectangle proper left ,top, right, bottom points like: RectF rectf = new RectF(0, 0, 480, 854);
Upvotes: 1
Reputation: 11131
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);
and here RectF
is
RectF rectf = new RectF(200, 400, 200, 400); // representing Point
here left = right = 200
and top = bottom = 400
which represents a Point
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);
and for width = 400 and height = 200
, RectF
should be
RectF rectf = new RectF(0, 0, 400, 200);
Upvotes: 17