在定制视图上绘图需要帮助

时间:2011-03-13 21:19:10

标签: android view canvas drawing draw

我需要你的经验。 问题:我需要能够在FlipperView的一个部分上绘制东西(rect,circ等).... 我的main.xml有一个主要的linearLayout。在这个LinearLayout中,我有一个ViewFlipper,里面有2个linearlayout。第一个linearlayout有soms按钮,inputfiels等......第二个应该有一个特殊的视图,我可以在第一部分画出我选择的东西。 所以我创建了一个扩展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">

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

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

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

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

然后是我的主要活动 DRAWER.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...
}

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... }

所以我需要能够在我的main.xml中使用id = draw绘制事物VIEWCLASS ... 我怎样才能做到这一点 ?请帮我解释和解决方案而不只是一个解决方案: - )

感谢VeeTee

3 个答案:

答案 0 :(得分:1)

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

如果您对onDraw代码不确定,请尝试使用API​​演示中DrawPoints等示例中的代码。

答案 1 :(得分:0)

马修已经说过,你不要自己打电话给自己。这是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);

答案 2 :(得分:0)

似乎第一次没有工作的问题是......

    

当主要活动想要setContentView(...)...它崩溃.... 但是当我把它从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

它有效......