2017-04-06 10 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すべてを、

あなたは絶対的なレイアウトを使用することはありませんが、それが正当な理由のために推奨されていません。

それには2つの選択肢があります。どちらのオプションについても、独自のレイアウトを実装する必要があります。

オプション番号は、 1では、dispatchDraw(最終キャンバスキャンバス)をオーバーライドすることができます。

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. 
    } 

}