什么?
第一次尝试创建自定义RelativeLayout
并OnDraw
覆盖,如下所示
SliceLayout 类:
public class SliceLayout extends LinearLayout {
public SliceLayout(Context Context, Slice Slice) {
super(Context);
mPaint = new Paint();
mPaint.setStyle(Style.FILL);
mPaint.setColor(Color.RED);
mPath = Draw(Slice.A, Slice.B, Slice.C);
}
final static String TAG = "Slice";
Paint mPaint;
Path mPath;
private Path Draw(Point A, Point B, Point C) {
Log.e(TAG, "Draw");
Path Pencil = new Path();
Pencil.moveTo(A.x, A.y);
Pencil.lineTo(B.x, B.y);
Pencil.lineTo(C.x, C.y);
return Pencil;
}
@Override
protected void onDraw(Canvas canvas) {
Log.e(TAG, "Drawing");
canvas.drawPath(mPath, mPaint);
}
}
切片类:
public class Slice {
public enum Location {
N, S, W, NE, NW, SE, SW, EN, ES
}
public Point A;
public Point B;
public Point C;
private void Define(Point A, Point B, Point C) {
this.A = A;
this.B = B;
this.C = C;
}
public Slice(Point Display, Location Location) { //Display = Size of display
switch (Location) {
case N:
Define(new Point(0, 0), new Point(Display.x, 0), new Point(0,
Display.y));
case S:
Define(new Point(Display.x, 0), new Point(Display.x, Display.y),
new Point(0, Display.y));
default:
Define(new Point(0, 0), new Point(0, 0), new Point(0, 0));
}
}
}
它正在工作,但我不需要采用重型代码低性能方式,因为XML中的继承而无法初始化组件!
第二次尝试尝试创建相同的布局,但XML
版
问题:
我感到宽松,你能帮助我选择正确的方式或建议另一种proven
方式!