[android] I have a LinearLayout which contains 2 views, 1st is imageView and th 2nd one have canvas on it. if I do mLinearLayout.addView(i); and then mLinearLayout.addView(c); it shows both but if I do it in reverse oder (mLinearLayout.addView(c) and then mLinearLayout.addView(i)) the it only shows canvas. I wanted to share the screen between these 2 views. Can anybody help me on this?
@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);
}
}
public class DrawView extends View {
private ShapeDraw开发者_运维问答able 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);
}
Give the following a try.
Take your draw statement out of ondraw then call DrawView or some shape or what ever.
//some shape//
Also I Dont See your X And Y, or height or width.
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);
}
精彩评论