Android - 无法在while循环中显示画布

时间:2014-01-14 12:37:52

标签: java android android-canvas

我是Android和Java的新手,我在尝试在while循环中重复显示画布时遇到了问题。在onCreate方法结束并且我的while循环位于onCreate方法之前,画布似乎不会显示。这是代码:

package com.example.clip_test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.os.SystemClock;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout ll=(LinearLayout)findViewById(R.id.rect);
        ll.setBackgroundColor(Color.WHITE);

        Bitmap bmp=Bitmap.createBitmap(480,800,Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(bmp);
        Paint paint=new Paint();

        while (true) {

            //DISPLAY RED SQUARE

            paint.setColor(Color.parseColor("#FF0000"));
            canvas.drawRect(50,50,200,200,paint);
            ll.setBackgroundDrawable(new BitmapDrawable(bmp));
            SystemClock.sleep(250);

            //DISPLAY BLUE SQUARE

            paint.setColor(Color.parseColor("#0000FF"));
            canvas.drawRect(50,50,200,200,paint);
            ll.setBackgroundDrawable(new BitmapDrawable(bmp));
            SystemClock.sleep(250);

        }

    }

}

我已经查看了如何定期更新画布的其他示例,但代码涉及其他并发症(例如处理触摸事件),在习惯画布之前我必须了解这些。谁能告诉我一个关于如何完成这项工作的简单代码?

2 个答案:

答案 0 :(得分:0)

尝试像这样更改您的代码

    private Handler handler;
    private BitmapDrawable bg1;
    private BitmapDrawable bg2;
    private LinearLayout ll;

    private Runnable setBgRunnable1 = new Runnable() {
        @Override
        public void run() {
            ll.setBackgroundDrawable(bg1);
            handler.postDelayed(setBgRunnable2, 250);
        }
    };

    private Runnable setBgRunnable2 = new Runnable() {
        @Override
        public void run() {
            ll.setBackgroundDrawable(bg2);
            handler.postDelayed(setBgRunnable1, 250);
        }
    };

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ll=(LinearLayout)findViewById(R.id.rect);
        ll.setBackgroundColor(Color.WHITE);

        Paint paint=new Paint();

        //DISPLAY RED SQUARE

        Bitmap bmp1=Bitmap.createBitmap(480,800,Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(bmp1);
        paint.setColor(Color.parseColor("#FF0000"));
        canvas.drawRect(50,50,200,200,paint);
        bg1 = new BitmapDrawable(bmp1);

        //CREATE BLUE SQUARE

        Bitmap bmp2=Bitmap.createBitmap(480,800,Bitmap.Config.ARGB_8888);
        canvas=new Canvas(bmp2);
        paint.setColor(Color.parseColor("#0000FF"));
        canvas.drawRect(50,50,200,200,paint);
        bg2 = new BitmapDrawable(bmp2);

        // CREATE HANDLER
        handler = new Handler();
        handler.postDelayed(setBgRunnable1, 250);
   }

答案 1 :(得分:-1)

声明要绘制的视图,然后覆盖该视图的onDraw方法以访问画布。这个答案(canvas not drawing in java android)有一个例子。

编辑:然后您可以将时间控制和while循环保留在onCreateonResume中。为每种颜色声明paint,并在onDraw

中添加类似的内容
@Override 
protected void onDraw(Canvas canvas) {   
    if (wantToDrawRed) {
        canvas.drawRect(50,50,200,200,redPaint);
    }
    if (wantToDrawBlue) {
        canvas.drawRect(50,50,200,200,bluePaint);
    }
    super.onDraw(canvas);
}

有几种方法可以做到。