需要在自定义视图上绘图的帮助

发布于 2024-10-21 21:34:13 字数 2425 浏览 2 评论 0原文

我需要你的经验。 问题:我需要能够在 FlipperView 的一部分上绘制东西(矩形、圆形等)...... 我的 main.xml 有一个主要的 LinearLayout。在这个 LinearLayout 中,我有一个 ViewFlipper,其中有 2 个线性布局。第一个线性布局有一些按钮、输入字段等...第二个线性布局应该有一个特殊的视图,我可以在其中绘制我在第一部分中选择的内容。 所以我创建了一个新视图,它扩展了 View 类,这样我就可以使用 ondraw 方法。但我无法让它发挥作用。 这就是我到目前为止所拥有的...... MAIN.XML

<LinearLayout android:orientation="vertical" android:layout_width="fill_parent"            android:layout_height="fill_parent" android:id="@+id/layout_main" xmlns:android="http://schemas.android.com/apk/res/android">

 <ViewFlipper android:id="@+id/details" android:layout_width="fill_parent"        android:layout_height="fill_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="match_parent">

//BUTTONS TEXTFIELDS ETC

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="match_parent">

//我的新 ViewClass 的实例

<Vierbergen.Tim.ViewClass
android:id="@+id/draw" android:layout_width="match_parent"
android:layout_height="match_parent"/>
    </LinearLayout>
</ViewFlipper>
</LinearLayout>

VIEWCLASS.java <代码>

public class ViewClass extends View {
    Paint paint = new Paint();
     public DrawView(Context context) {
 super(context); 
paint.setColor(Color.WHITE);
 }  
@Override
public void onDraw(Canvas canvas) {
//depending on some params....
draw this, draw that...
}

<代码>}

然后是我的主要活动 DRAWER.JAVA

public class SmsDraw extends Activity implements OnTouchListener{
ViewClass vClass;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

      vClass = (ViewClass) findViewById(R.id.draw);
    }

// 某处有一个由按钮执行的绘图函数 私有无效开始(){ //我在哪里可以获得画布?画布 c = new Canvas(); 布拉布拉尔巴 vClass.onDraw(c); }

所以我需要能够在 main.xml 中使用 id = draw 来绘制 VIEWCLASS 的内容... 我该怎么做?请帮助我解释和解决方案,而不仅仅是解决方案:-)

谢谢 VeeTee

I need your experience.
Problem : I need to be able to draw thing (rect, circ,etc) on one part of a FlipperView....
My main.xml has a main linearLayout. In this LinearLayout I have a ViewFlipper with 2 linearlayouts in it. The first linearlayout has soms buttons, inputfiels,etc... the second one should have a special view in wich I can draw the things I choose in the first part.
So I have created a new view wich extends the View class so I can play with the ondraw methode. But I can not get it to work.
This is what I have so far...
MAIN.XML

<LinearLayout android:orientation="vertical" android:layout_width="fill_parent"            android:layout_height="fill_parent" android:id="@+id/layout_main" xmlns:android="http://schemas.android.com/apk/res/android">

 <ViewFlipper android:id="@+id/details" android:layout_width="fill_parent"        android:layout_height="fill_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="match_parent">

//BUTTONS TEXTFIELDS ETC

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="match_parent">

//an instance of my new ViewClass

<Vierbergen.Tim.ViewClass
android:id="@+id/draw" android:layout_width="match_parent"
android:layout_height="match_parent"/>
    </LinearLayout>
</ViewFlipper>
</LinearLayout>

The VIEWCLASS.java

public class ViewClass extends View {
    Paint paint = new Paint();
     public DrawView(Context context) {
 super(context); 
paint.setColor(Color.WHITE);
 }  
@Override
public void onDraw(Canvas canvas) {
//depending on some params....
draw this, draw that...
}

}

and then my main activity
DRAWER.JAVA

public class SmsDraw extends Activity implements OnTouchListener{
ViewClass vClass;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

      vClass = (ViewClass) findViewById(R.id.draw);
    }

// with somewhere a draw function excecuted by a button
private void start() {
//where can I get a canvas ? Canvas c = new Canvas();
blablalba
vClass.onDraw(c);
}

So I need to be able to draw on the thing VIEWCLASS with id = draw in my main.xml...
How can I do this ? please help me with an explanation and solution and not just a solution :-)

Thanks VeeTee

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反差帅 2024-10-28 21:34:13

如果您的 View 附加到视图层次结构,则框架将调用您的 onDraw 方法。你不需要自己调用它。

如果您不确定 onDraw 代码,请尝试使用 DrawPoints

Your onDraw method will be called by the framework if your View is attached to the view hierarchy. You don't need to call it yourself.

If you're unsure about your onDraw code, try using the code from a sample like DrawPoints in API Demos.

ぶ宁プ宁ぶ 2024-10-28 21:34:13

正如马修所说,你不会自己调用 onDraw 。这是对 ViewClass 的一个非常简单的添加,它允许您绘制矩形或圆形。它尚未经过测试,因此请小心行事。



import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class DrawView extends View {
    private boolean drawRect;
    private boolean drawCircle;
    private float rect_w;
    private float rect_h;
    private int rect_color;
    private float circle_radius;
    private int circle_color;
    Paint paint;

    public DrawView(Context context) {
        super(context);
        paint = new Paint();
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
    }

    public DrawView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        paint = new Paint();
    }

    public void drawRect(float w, float h, int color) {

        // define these variables privately at the top of the class
        this.drawRect = true;
        this.drawCircle = false;
        this.rect_w = w;
        this.rect_h = h;
        this.rect_color = color;
    }

    public void drawCircle(float radius, int color) {

        // define these variables privately at the top of the class
        this.drawRect = false;
        this.drawCircle = true;
        this.circle_radius = radius;
        this.circle_color = color;
    }

    @Override
    public void onDraw(Canvas canvas) {

        if (drawRect) {
            paint.setColor(this.rect_color);
            canvas.drawRect(0, 0, rect_w, rect_h, paint);
        }
        if (drawCircle) {
            paint.setColor(this.circle_color);
            canvas.drawCircle(0, 0, circle_radius, paint);
        }
    }
}

然后在你的视图类中,这样调用它:


vClass = (DrawView) findViewById(R.id.draw);
vClass.drawRect(3,4,0x334434);

You don't call onDraw yourself, as Matthew has said. Here's a very simple addition to your ViewClass that would allow you to draw rectangles or circles. It hasn't been tested, so proceed carefully.



import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class DrawView extends View {
    private boolean drawRect;
    private boolean drawCircle;
    private float rect_w;
    private float rect_h;
    private int rect_color;
    private float circle_radius;
    private int circle_color;
    Paint paint;

    public DrawView(Context context) {
        super(context);
        paint = new Paint();
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
    }

    public DrawView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        paint = new Paint();
    }

    public void drawRect(float w, float h, int color) {

        // define these variables privately at the top of the class
        this.drawRect = true;
        this.drawCircle = false;
        this.rect_w = w;
        this.rect_h = h;
        this.rect_color = color;
    }

    public void drawCircle(float radius, int color) {

        // define these variables privately at the top of the class
        this.drawRect = false;
        this.drawCircle = true;
        this.circle_radius = radius;
        this.circle_color = color;
    }

    @Override
    public void onDraw(Canvas canvas) {

        if (drawRect) {
            paint.setColor(this.rect_color);
            canvas.drawRect(0, 0, rect_w, rect_h, paint);
        }
        if (drawCircle) {
            paint.setColor(this.circle_color);
            canvas.drawCircle(0, 0, circle_radius, paint);
        }
    }
}

Then in your view Class, call it like this:


vClass = (DrawView) findViewById(R.id.draw);
vClass.drawRect(3,4,0x334434);
爱的故事 2024-10-28 21:34:13

似乎第一次不起作用的问题是......

当主要活动想要设置ContentView(...)...它崩溃...
但是当我把它排除在 xml 之外时
并在运行时创建它,如下所示

viewClass = new ViewClass(this);

    layke = (LinearLayout) findViewById(R.id.layoutDraw);//wich is the second part of the flipperview
    layke.addView(viewClass); // to at it to the right layout

有用....

Seems like the problem it wasn't working the first time was...

when the main activity want to setContentView(...)... it crashes....
But when I leave it out of the xml
and create it at runtime like this

viewClass = new ViewClass(this);

    layke = (LinearLayout) findViewById(R.id.layoutDraw);//wich is the second part of the flipperview
    layke.addView(viewClass); // to at it to the right layout

it works....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文