android: how to implement canvas draws into a exis

2019-07-21 00:13发布

问题:

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)

回答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;

// 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



回答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.



回答3:

try this :

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