无效调用onDraw()

时间:2014-03-27 00:29:04

标签: java android xml drawing invalidation

每当按下drawCircle()时,我都会尝试调用onDraw()。我知道它被调用(通过xml中的onClick),因为我收到消息:“drawCircle被按下”。然而问题是,invalidate不会调用onDraw(),它会在画布上的随机位置绘制一个圆圈,颜色随机。我试过在DrawView中放置invalidate并在我的activity中调用drawCircle调用该方法,但它也不起作用。无效的任何想法都没有调用onDraw?

由于

在我的XML中,我定义了我的视图:

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#cccccc"
    android:paddingBottom="100dp"
    android:paddingTop="20dp" />

<com.example.genraterandomcircle.DrawView
    android:id="@+id/drawView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/generateBtnId"
    android:background="#000000" />

<Button
    android:id="@+id/generateBtnId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/drawView1"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="25dp"
    android:onClick="drawCircle"
    android:text="@string/generateBtnStr" />

<Button
    android:id="@+id/clearBtnId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/generateBtnId"
    android:layout_alignBottom="@+id/generateBtnId"
    android:layout_alignRight="@+id/drawView1"
    android:layout_marginRight="50dp"
    android:onClick="clearCircles"
    android:text="@string/clearBtnStr" />

GenerateCircles.java:

package com.example.genraterandomcircle;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

public class GenerateCircles extends Activity
{

    DrawView dv;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_generate_circles);
        dv = new DrawView(this);
    }

    public void drawCircle(View v)
    {
        System.out.println("drawCircle pressed");

        dv.invalidate();
    }

    public void clearCircles(View v)
    {
        System.out.println("clear pressed");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.generate_circles, menu);
        return true;
    }

}

DrawView类:

package com.example.genraterandomcircle;

import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

public class DrawView extends View
{
    Canvas c;
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    Random random = new Random();
    Bitmap bm;

    public DrawView(Context context)
    {
        super(context);
        init(context);
    }

    public DrawView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context)
    {

        p.setColor(Color.BLACK);
        bm = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
        c = new Canvas(bm);

        System.out.println("hi there");
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        //canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(bm, canvas.getWidth() / 2, 0, null);

        p.setColor(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
        canvas.drawCircle(random.nextInt(canvas.getWidth()), random.nextInt(canvas.getHeight()),
                    random.nextInt(canvas.getWidth() / 4), p);

    }
}

1 个答案:

答案 0 :(得分:7)

而不是

dv = new DrawView(this); 
在你的onCreate使用中

dv = (DrawView)findViewById(R.id.drawView1);

您没有使用您在内容视图中设置的布局中的drawview