2

下面的布局呢

第一次尝试是创建自定义RelativeLayout覆盖OnDraw如下

切片布局类:

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方式!

4

0 回答 0