android: how to implement canvas draws into a exist view in XML

I have a XML file in my program like:

 <LinearLayout android:orientation="horizontal"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1">
            <LinearLayout android:layout_width="fill_parent"
                       android:layout_height="fill_parent"
                       android:layout_weight="1"
                       android:background="#ef3"
                       android:id="@+id/img01"/>
            <LinearLayout android:layout_width="fill_parent"
                       android:layout_height="fill_parent"
                       android:layout_weight="1"
                       android:background="#E8A2B4"
                       android:id="@+id/img02"/>
    </LinearLayout>

Also, I have a canvas draw function in my activity.

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

    private class iniView extends View {

        public iniView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            //set background color
            canvas.drawColor(Color.WHITE);
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(3);
            canvas.drawCircle(this.getWidth()/2, this.getHeight()/2, 30, paint);
        }

    }
}

I read some articles from Internet. Some people create there own view in activity and set the paint into it.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyView myView=new MyView(this);
        setContentView(myView);
    }

But I want to set my paint into this two LinearLayout.

<LinearLayout android:layout_width="fill_parent"
                       android:layout_height="fill_parent"
                       android:layout_weight="1"
                       android:background="#ef3"
                       android:id="@+id/img01"/>
            <LinearLayout android:layout_width="fill_parent"
                       android:layout_height="fill_parent"
                       android:layout_weight="1"
                       android:background="#E8A2B4"
                       android:id="@+id/img02"/>

How can I do that? (I want to draw two same circles in the center of this two linear layout)

Answers


One solution would be to extract the initView (should be CamelCase btw) and then inject it into the xml with the full path name of the class of the View as the tag name. Let's see an example. The resultant class should look like this:

package your.package;

// imports

private class CircleView extends View {

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

    @Override
    protected void onDraw(Canvas canvas) {
        // your painting stuff
    }

}

And the the xml should be something like this:

<your.package.CircleView 
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_weight="1"
     android:background="#ef3"
     android:id="@+id/img01"/>
<your.package.CircleView 
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_weight="1"
     android:background="#E8A2B4"
     android:id="@+id/img02"/>

Although you may want to have a look at the available Drawable resources in Android: http://developer.android.com/guide/topics/resources/drawable-resource.html

There is a Shape Drawable type that may allow you to do what you want in a simpler way and separated from the business logic of the Activity.

Hope it helps


You can render your xml view using this:

View viewalias = (View) this.findViewById(R.layout.yourxml);, then draw on it as you like, for more details, see this.


try this :

...
import android.util.AttributeSet;
...
public iniView(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
}
...

Need Your Help

how to define a pow2 macro in C++

c++ performance macros euclidean-distance

I want to have definition for pow2C as follows. But my compiler shows it (__tmpDouble__) is already defined. Please help me define it.

How Exactly Do You Build A GUI Application in Squeak

smalltalk pharo squeak

All the tools that i searched are 2005 or 2006 and so i dont even dare to try those. I understand that morphic is ok , but i am looking for something like Morphic Designer.