当前位置: 代码迷 >> Android >> 高手一个android 绘图和线程的有关问题
  详细解决方案

高手一个android 绘图和线程的有关问题

热度:15   发布时间:2016-05-01 14:49:41.0
请教各位高手一个android 绘图和线程的问题
先看代码 再说说我遇到的问题
Java code
package com.zou.test;import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Handler;import android.os.Message;import android.util.DisplayMetrics;import android.view.View;public class Histogram extends View implements Runnable{    private int displayw = 0;    private int displayy = 0;    private static int cardinalX = 0; // 第一个矩形X轴基点    private static int cardinalY = 0; // 第一个矩形Y轴基点    private static int rectwidth = 30; // 矩形宽    private static int rectheight = 15; // 矩形高    private static int betweenrect = 2;    int x_end, y_end, temp;    Paint paint = new Paint();    boolean flag = true;    Canvas mCanvas;    public Histogram(Context context) {        super(context);        DisplayMetrics dm = MainActivity.getdm();        displayw = dm.widthPixels;        displayy = dm.heightPixels;        cardinalX = displayw - displayw / 5;        cardinalY = displayy - displayy / 5;        x_end = cardinalX + rectwidth;        y_end = cardinalY + rectheight;        temp = rectheight + betweenrect;        paint.setAntiAlias(true);        paint.setColor(Color.BLUE);                // TODO Auto-generated constructor stub    }    @SuppressLint("DrawAllocation")    @Override    protected void onDraw(Canvas canvas) {        // TODO Auto-generated method stub        super.onDraw(canvas);                mCanvas = canvas;        this.run();    }    Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            // TODO Auto-generated method stub            super.handleMessage(msg);        }    };    @Override    public void run() {        // TODO Auto-generated method stub                for(int i = 0; i < 6; i++){            mCanvas.drawRect(cardinalX, cardinalY -= temp, x_end, y_end -= temp,paint);                        try {                Thread.sleep(100);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}


现在的问题是在程序运行起来后 屏幕上什么都没有 等到过了延时时间的总和后绘制的图像就一下全出来了 看不到延时的效果 好像是在延时的时候界面没办法显示 等0.1*6秒之后才能显示界面 怎么回事呢

------解决方案--------------------
这样在子线程和主线程之间交互的问题还真有点复杂。我还不知道这个是怎么回事,我建议, 把那个runnable去掉,写个函数。
用一个for循环
 int t = 当前时间;
for(int i = 0; i < 6; i++){
sendMessageDelayed(Message,t + i *100);

}
这样就能依次延时100毫秒发消息,而且在UI线程。
然后那边handleMessage里面调用mCanvas.drawRect(),全部在UI线程做试试。

希望我的回答能给楼主帮助。
------解决方案--------------------
画一次重绘一次才会有效果- -~你只是画却没有重绘当然不行咯,另外这代码真是一团乱,既然是线程,又怎么会直接调用run()方法,这是违背java机制的……,直接调用run(),不是意味着在主线程中执行这个方法么?何来另起线程一说,这样根本无法让线程sleep嘛。给你改了一下,没做测试,自己试试吧
Java code
package com.android.market_billing;import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Handler;import android.os.Message;import android.util.DisplayMetrics;import android.view.View;public class Histogram extends View implements Runnable{    private int displayw = 0;    private int displayy = 0;    private static int cardinalX = 0; // 第一个矩形X轴基点    private static int cardinalY = 0; // 第一个矩形Y轴基点    private static int rectwidth = 30; // 矩形宽    private static int rectheight = 15; // 矩形高    private static int betweenrect = 2;    int x_end, y_end, temp;    Paint paint = new Paint();    boolean flag = true;    Canvas mCanvas;    public Histogram(Context context) {        super(context);        displayw = dm.widthPixels;        displayy = dm.heightPixels;        cardinalX = displayw - displayw / 5;        cardinalY = displayy - displayy / 5;        x_end = cardinalX + rectwidth;        y_end = cardinalY + rectheight;        temp = rectheight + betweenrect;        paint.setAntiAlias(true);        paint.setColor(Color.BLUE);        new Thread(this).start();  //启动线程,系统会自动调用run()方法,这样做才能使线程休眠                // TODO Auto-generated constructor stub    }    @SuppressLint("DrawAllocation")    @Override    protected void onDraw(Canvas canvas) {        // TODO Auto-generated method stub        super.onDraw(canvas);        mCanvas = canvas;    }    Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            // TODO Auto-generated method stub            super.handleMessage(msg);        }    };    @Override    public void run() {        // TODO Auto-generated method stub                for(int i = 0; i < 6; i++){            mCanvas.drawRect(cardinalX, cardinalY -= temp, x_end, y_end -= temp,paint);//            invalidate();//错误重绘,这个方法只能在主线程中执行,要用他的话,就通过mHandler来与主线程通信吧            postInvalidate();  //重绘画布,使用post方法,可以不用传到主线程中去更新界面             //注意:重绘画布,系统会调用onDraw(Canvas canvas)方法,所以你刚刚在onDraw()调用run()是不行的,重绘时会进入死循环            try {                Thread.sleep(100);              } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}
  相关解决方案