import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
public class MyRect extends View {
Paint paint = new Paint();
Rect r1 ;
Rect r2 ;
Rect r3 ;
public MyRect(Context context) {
super(context);
init();
}
private void init() {
r1 = new Rect(10, 10, 100, 100);
r2 = new Rect(110, 110, 180, 180);
//判断是否相等,重写了equals
Log.i("equals", r1.equals(r2) + "");//equals﹕ false
//重写了toString
Log.i("toString", r1.toString());//toString﹕ Rect(10, 10, 100, 100)
Log.i("toShortString", r1.toShortString());//toShortString﹕ [10,10][100,100]
Log.i("flattenToString", r1.flattenToString());//flattenToString﹕ 10 10 100 100
r3 = Rect.unflattenFromString("180 180 280 280");
Log.i("isEmpty", r1.isEmpty() + "");//isEmpty﹕ false
Log.i("r1-width", r1.width() + "");//r1-width﹕ 90
Log.i("r1-height", r1.height() + "");//r1-height﹕ 90
// 中心X坐标
Log.i("r1_centerX", r1.centerX() + "");//r1_centerX﹕ 55
//中心Y坐标
Log.i("r1_centerY", r1.centerY() + "");///r1_centerY﹕ 55
//精确的中心X坐标
Log.i("r1_exactCenterX", r1.exactCenterX() + "");//r1_exactCenterX﹕ 55.0
//精确的中心Y坐标
Log.i("r1_exacctCenterY", r1.exactCenterY() + "");//r1_exactCenterY﹕ 55.0
//重置为空
r2.setEmpty();
//设置坐标
r2.set(40, 40, 100, 100);
printCoordinates(r2, "a");
//left﹕ 40
//top﹕ 40
//right﹕ 100
//bottom﹕ 100
//使用已有的Rect进行设置
r2.set(r1);
printCoordinates(r2, "b");
//left﹕ 10
//top﹕ 10
//right﹕ 100
//bottom﹕ 100
//偏移
r1.offset(20, 20);
printCoordinates(r1, "c");
//left﹕ 30
//top﹕ 30
//right﹕ 120
//bottom﹕ 120
//偏移到
r1.offsetTo(40, 40);
printCoordinates(r1, "d");
//left:40
//top﹕ 40
//right﹕ 130
//bottom﹕ 130
r1.inset(20, 30);
printCoordinates(r1, "e");
//left﹕ 60
//top﹕ 70
//right﹕ 110
//bottom﹕ 100
//判断包含性
Log.e("contains1", r1.contains(50, 50) + "");// contains1﹕ false
Log.e("contains2", r2.contains(20, 20, 80, 80) + "");//contains2﹕true
Log.e("contains3", r3.contains(r2) + "");//contains3﹕ false
//判断相交
Log.e("intersect1", r1.intersect(50, 50, 60, 80) + "");//intersect1﹕false
Log.e("intersect2", r1.intersect(r2) + "");//intersect2﹕ true
Log.e("intersects3", r1.intersects(50, 50, 60, 80) + "");//intersects3﹕ false
Log.e("intersects4", Rect.intersects(r2, r3) + "");//intersects4﹕ false
//取相交部分
Log.e("setIntersect", r1.setIntersect(r2, r3) + "");//setIntersect﹕ false
printCoordinates(r1, "r1a");
//left﹕ 60r1a
//top﹕ 70r1a
//right﹕ 100r1a
//bottom﹕ 100r1a
//取并
r1.union(r2);
printCoordinates(r1, "r1b");
//left﹕ 10r1b
//top﹕ 10r1b
//right﹕ 100r1b
//bottom﹕ 100r1b
r1.union(8, 7);
printCoordinates(r1, "r1c");
//left﹕ 8r1c
//top﹕ 7r1c
//right﹕ 100r1c
//bottom﹕ 100r1c
r1.union(40, 20, 120, 130);
printCoordinates(r1, "r1d");
//left﹕ 8r1d
//top﹕ 7r1d
//right﹕ 120r1d
//bottom﹕ 130r1d
//交换
r1.sort();
printCoordinates(r1, "r1e");
//left﹕ 8r1e
//top﹕ 7r1e
//right﹕ 120r1e
//bottom﹕ 130r1e
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(r1, paint);
canvas.drawRect(r2, paint);
canvas.drawRect(r3, paint);
}
/**打印坐标*/
private void printCoordinates(Rect rect, String str) {
Log.i("left", rect.left + str);
Log.i("top", rect.top + str);
Log.i("right", rect.right + str);
Log.i("bottom", rect.bottom + str);
}
}
Rect rect=
new
Rect(
100
,
50
,
300
,
500
);
|
那么右下角(300,500)其实是不在这个矩形里面的,但是左上角(100,50)在,也就是说,这个矩形实际表示的区域是:(100,50,299,499)。另外,Rect计算出的Height和Width倒是对的。所以,在此告诫各位程序员,在涉及Rect运算的时候,尽量不要使用它的右下角左边,即right和bottom。因为他们是错的。