2017-04-06 55 views
0

enter image description here绘图线编程上的布局

红色保证金表示AbsoluteLayout,我和“公告板”对象放置在屏幕上的任意数量。我想要的只是使用Board对象的坐标和屏幕中心在屏幕上绘制一条线。每个棋盘对象负责绘制这条线。

另外我希望线后面的董事会对象我猜我必须改变z-索引,或者可能在AbsoluteLayout上画线?

我有这样的事情:

public class Board { 
ImageView line; //Imageview to draw line on 
Point displayCenter; //Coordinates to the center of the screen 
int x; 
int y; 
Activity activity; 

Board(Point p, Point c, Activity activity) // Point c is the coordinates of the Board object 
{ 
    x = c.x 
    y = c.y 
    displayCenter.x = p.x; 
    displayCenter.y = p.y; 
    this.activity = activity; 

    updateLine(); 
} 
public void updateLine(){ 
    int w=activity.getWindowManager().getDefaultDisplay().getWidth(); 
    int h=activity.getWindowManager().getDefaultDisplay().getHeight(); 

    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(bitmap); 
    line.setImageBitmap(bitmap); 

    Paint paint = new Paint(); 
    paint.setColor(0xFF979797); 
    paint.setStrokeWidth(10); 
    int startx = this.x; 
    int starty = this.y; 
    int endx = displayCenter.x; 
    int endy = displayCenter.y; 
    canvas.drawLine(startx, starty, endx, endy, paint); 
} 

}

回答

1

第一AF所有,

你永远也不会使用绝对布局,它已经废弃了一个很好的理由

就是说你有两个选择。对于您需要实现自己的布局的两个选项。

对于选项没有。 1,你可以重写dispatchDraw(最后的Canvas画布),见下面。

public class CustomLayout extends AbsoluteLayout { 

    ... 

    @Override 
    protected void dispatchDraw(final Canvas canvas) { 
     // put your code to draw behind children here. 
     super.dispatchDraw(canvas); 
     // put your code to draw on top of children here. 
    } 

    ... 

} 

选项编号。 2如果你喜欢绘图发生在onDraw我你需要设置setWillNotDraw(false);因为默认情况下ViewGroups上的onDraw方法不会被调用。

public class CustomLayout extends AbsoluteLayout { 

    public CustomLayout(final Context context) { 
     super(context); 
     setWillNotDraw(false); 
    } 

    ... 

    @Override 
    protected void onDraw(final Canvas canvas) { 
     super.onDraw(canvas); 
     // put your code to draw behind children here. 
    } 

}