开发者

Need help with drawing on a custom made view

开发者 https://www.devze.com 2023-02-16 09:16 出处:网络
I need your experience. Problem : I need to be able to draw thing (rect, circ,etc) on one part of a FlipperView....

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(Co开发者_开发百科ntext 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


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.


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);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消