开发者

Using multiple views inside a single activity with a canvas

Basically what I am trying to accomplish is I want a canvas I can pass 开发者_StackOverflowbitmaps to, to be displayed on the screen with the rest of my view objects (buttons, textviews, etc).

I created a class for the canvas view like so (it doesn't do much yet):

public class Foo extends View {

    public Foo(Context context) {
        super(context);
    }

    @Override
    public void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);

        // draw a solid blue circle
        paint.setColor(Color.BLUE);
        canvas.drawCircle(20, 20, 15, paint);
    }
}

Now here is my main activity:

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button switchLeft = (Button) findViewById(R.id.switch_left);
        switchLeft.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               //TODO 
            }
        });

        Button switchRight = (Button) findViewById(R.id.switch_left);
        switchRight.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //TODO
            }
        });

        Foo myCanvas = new Foo(this);
        setContentView(myCanvas);
    }
}

When I call setContentView to the canvas view object, the rest of my views disappear from the screen. How can I call up this view object without losing the rest of my views?

THanks.


you could refer to you own view in your "main"-layout

...
<xxx.yyy.Foo
        android:background="@drawable/red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜