2011-07-20 8 views
1

[アンドロイド] 2つのビューを含むLinearLayoutを持っています.1番目はimageView、2番目はキャンバスです。 私がmLinearLayout.addView(i)を実行した場合。次にmLinearLayout.addView(c);をクリックします。それは両方を示していますが、逆の場合(mLinearLayout.addView(c)、次にmLinearLayout.addView(i))、キャンバスが表示されます。 この2つのビューの間で画面を共有したいと考えました。誰も私にこれを助けることができますか?アンドロイドでキャンバスのサイズを変更することはできますか?

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mLinearLayout = new LinearLayout(this); 

    mLinearLayout.setBackgroundColor(0xff74AC23); 
    ImageView i = new ImageView(this); 
    View c = new DrawView(this); 

    i.setImageResource(R.drawable.bol_groen); 
    i.setAdjustViewBounds(true); 

    mLinearLayout.addView(i); 
    mLinearLayout.addView(c); 
    setContentView(mLinearLayout); 

} 

}

パブリッククラスDrawViewビュー{

private ShapeDrawable mDrawable; 

public DrawView(Context context) { 
    super(context); 
    setFocusable(true); //necessary for getting the touch events 

    mDrawable = new ShapeDrawable(new OvalShape()); 
    mDrawable.getPaint().setColor(0xff74AC23); 
    mDrawable.setBounds(x, y, x + width, y + height); 

} 

@Override protected void onDraw(Canvas canvas) { 

    canvas.drawColor(0xFFCCCCCC); 
    mDrawable.draw(canvas); 

}

+0

キャンバスのみを表示しているとはどういう意味ですか? DrawViewは画面全体を占めるか、または適切なサイズですが、ImageViewは表示されません。また、XMLでこれをやっていない理由もありますか? – Idistic

+0

この記事をご覧ください。希望はあなたを助けるでしょう - http://stackoverflow.com/questions/11071258/scale-a-canvas-to-fit-a-view-programmatically –

答えて

0

は試し以下を与える拡張します。

ドロー・ステートメントをondrawから取り出し、DrawViewや何らかの形やそれ以前のものを呼び出します。

//some shape// 

また、XとY、または高さまたは幅を表示しません。

public DrawView(Context context) {  
    super(context); 
    setFocusable (true); //necessary for getting the touch events 
    mDrawable = new ShapeDrawable(new OvalShape()); 
    mDrawable.getPaint().setColor(0xff74AC23); 

    mDrawable.setBounds(x, y, x + width, y + height); 
    mDrawable.draw(canvas); 
} 
関連する問題