如何使用borderWidth = 3和borderColor = black绘制空矩形,而矩形内的部分没有内容或颜色。 Canvas中使用哪个函数
void drawRect(float left, float top, float right, float bottom, Paint paint)
void drawRect(RectF rect, Paint paint)
void drawRect(Rect r, Paint paint)
感谢。
我试试这个例子
Paint myPaint = new Paint();
myPaint.setColor(Color.rgb(0, 0, 0));
myPaint.setStrokeWidth(10);
c.drawRect(100, 100, 200, 200, myPaint);
它绘制矩形并用黑色填充它但我只想要“框架” 喜欢这张图片:
答案 0 :(得分:147)
试试paint.setStyle(Paint.Style.STROKE)
?
答案 1 :(得分:119)
假设“矩形中的部分没有内容颜色”意味着您希望矩形内有不同的填充;您需要在矩形内绘制一个矩形,然后使用笔触宽度0和所需的填充颜色。
例如:
<强> DrawView.java 强>
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(30, 30, 80, 80, paint);
paint.setStrokeWidth(0);
paint.setColor(Color.CYAN);
canvas.drawRect(33, 60, 77, 77, paint );
paint.setColor(Color.YELLOW);
canvas.drawRect(33, 33, 77, 60, paint );
}
}
启动它的活动:
<强> StartDraw.java 强>
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class StartDraw extends Activity {
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
}
......将会这样:
答案 2 :(得分:11)
//white background
canvas.drawRGB(255, 255, 255);
//border's properties
paint.setColor(Color.BLACK);
paint.setStrokeWidth(0);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(100, 100, 200, 200, paint);
答案 3 :(得分:7)
创建一个新类 MyView, Which extends View
。覆盖onDraw(Canvas canvas)
方法以在Canvas
上绘制矩形。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
Paint paint;
Path path;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawRect(30, 50, 200, 350, paint);
canvas.drawRect(100, 100, 300, 400, paint);
//drawRect(left, top, right, bottom, paint)
}
}
然后使用我们的自定义View,MyView.Call将您的Java活动移动到setContentView()
。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
有关详细信息,请访问此处
http://developer.android.com/reference/android/graphics/Canvas.html
答案 4 :(得分:6)
paint.setStrokeWidth(3);
paint.setColor(BLACK);
并且您的drawRect
中的任何一个都应该有用。
答案 5 :(得分:0)
不知道这是否为时已晚,但我解决这个问题的方法是绘制四个细长矩形,这些矩形组成一个大边框。用一个矩形绘制边框似乎是可以撤销的,因为它们都是不透明的,所以你应该分别绘制边框的每个边缘。
答案 6 :(得分:0)
代码很好,只需将paintStyle设置为STROKE
paint.setStyle(Paint.Style.STROKE);