Skip to content Skip to sidebar Skip to footer

Android: How To Implement Canvas Draws Into A Exist View In Xml

I have a XML file in my program like:

Solution 1:

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;

// importsprivateclassCircleViewextendsView {

    publiciniView(Context context) {
        super(context);
    }

    @OverrideprotectedvoidonDraw(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

Solution 2:

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.

Solution 3:

try this :

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

Post a Comment for "Android: How To Implement Canvas Draws Into A Exist View In Xml"