I'm using ViewFlipper
to flip several views in an activity.
Therefore I include my view layouts in the activity-layout in this way:
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/profileSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<include android:id="@+id/inc1" layout="@layout/chart" />
<include android:id="@+id/inc2" layout="@layout/temp" />
The flipping does well.
My layout chart
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View android:id="@+id/viewChart" android:layout_width="match_parent"
android:layout_height="fill_parent" />
<TextView android:text="chart" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#fff" />
</LinearLayout>
I want to draw by canvas on the view viewChart
. So I thought I can address the view in the activity with this code:
viewChart = (View) findViewById(R.id.viewChart);
Canvas c = new Canvas();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setColor(Color.RED);
c.drawCircle(180, 20, 15, paint);
viewChart.draw(c);
viewChart.invalidate();
But when doing this, the screen stays empty (black. I think this is the background color of the theme).
My question is: How to draw directly on this view by canvas? What I'm doing wrong? Thanks!
First your canvas should be derived from a bitmap, which means you need to create a bitmap of a ViewChart's size and derive canvas from it Canvas c = new Canvas(myBitmap)
Secondly, since ViewChart is not a custom view I don't think you have a direct access to its canvas, hence you could try to make ViewChart to be an ImageView and just set an image (chart) to it with viewChart.setImageBitmap(myBitmap) instead of draw(c) and invalidate.