机器人:如何实现画布上绘制成XML格式的存在视图 [英] android: how to implement canvas draws into a exist view in XML

查看:120
本文介绍了机器人:如何实现画布上绘制成XML格式的存在视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在我的程序一个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);
    }

但我想我的设置成漆这两LinearLayout中。

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)

推荐答案

一个解决办法是提取initView(应BTW驼峰),然后将其注入与类视图为一体的全路径名的XML标签名称。让我们来看一个例子。得到的类应该是这样的:

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
    }

}

而在XML应该是这样的:

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

虽然你可能想在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.

希望它能帮助

这篇关于机器人:如何实现画布上绘制成XML格式的存在视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆