使用新元素即时更新Android SurfaceView

时间:2010-08-05 19:26:37

标签: java android graphics surfaceview

我有一个数据库,其中包含以下格式的记录:。我希望我的应用程序做的是从外部数据库中选择记录,并使用SurfaceView在手机屏幕上显示这些记录。

目前,我有一个Activity和一个负责应用程序记录收集部分的服务。活动将意图传递给服务,服务通过返回需要显示的记录来响应。记录作为Data类的实例存储在我的程序中,并且仅具有在视图中绘制元素的位置的屏幕坐标(我只是为DB中的每个记录绘制一个圆圈)。

为了简洁起见,我不会包含该服务,但我将包含Activity类的骨架和我希望显示的数据。

public class Data{
    private int x;
    private int y;
    private int id;
    private int shape;

    /*
    * Getters and setters for class properties
    */
}

public class Displayer extends Activity {
    int ht;
    int wt;
    dataReceiver dreceiver; 
    public static Map<String, Data> Info; 
    private LinearLayout linear; 
    private static final int RADIUS = 20;
    Panel panel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        panel=new Panel(this);
        setContentView(panel);

        Intent scv = new Intent(this, DataService.class);
        startService(scv);
    }
    /*
    * Various other methods to take care of onDestroy, onPause, etc.
    */

    public class dataReceiver extends BroadcastReceiver {
        //Get new data from intents sent by the Service and store it in Info variable

        //Update the Testing Map data-structure in the Panel SurfaceView with new records
        panel.setData(Info);

    }
}

我遇到的问题与SurfaceView有关。我意识到很多人会建议我只使用常规View,但我的应用程序涉及很多元素,因此SurfaceView更适合我的需求。下面是我的SurfaceView类的框架,它包含一个用于管理线程的嵌套类。

public class Panel extends SurfaceView implements SurfaceHolder.Callback {
    private PanelThread _thread;

    private Paint circlepaint;
    private Paint textpaint;

    private static int CircleColor = Color.YELLOW;
    private static int TextColor = Color.RED;
    private static Map<String, Data> Testing;

    public Panel(Context context, Map<String, Data> entries) {
        super(context);

        getHolder().addCallback(this);
        _thread = new PanelThread(getHolder(), this);
        setFocusable(true);

        textpaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textpaint.setStyle(Style.FILL_AND_STROKE);
        textpaint.setColor(TextColor);

        Testing = new HashMap<String, Data>();
        // Log.d("TestingSize",String.valueOf(Testing.size()));
        if (!(Testing.isEmpty())) {
            Testing.clear();
        }

        for (Map.Entry<String, Data> e : entries.entrySet()) {
            String keyvalue = e.getKey();
            Data v = e.getValue();
            Panel.Testing.put(keyvalue, v);
        }

    }

    public Panel(Context context) {
        super(context);
        getHolder().addCallback(this);
        _thread = new PanelThread(getHolder(), this);
        setFocusable(true);
        textpaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textpaint.setStyle(Style.FILL_AND_STROKE);
        textpaint.setColor(TextColor);
        Testing = new HashMap<String, Victims>();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        _thread.setRunning(true);
        _thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        /* we have to tell thread to shut down 
        * & wait for it to finish, 
        * or else it might touch the Surface 
        * after we return and explode
        */

        boolean retry = true;
        _thread.setRunning(false);
        while (retry) {
            try {
                _thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }   
    /*If new records are received 
    * from the service, they can be updated 
    * using this method
    */
    public void setData(Map<String,Data>Info){
        Testing=Info;
    }

    /*
    * Iterate over all contents of Testing List and display them to the screen
    */
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!(Testing.isEmpty())) {
            for (Map.Entry<String, Victims> e : Testing.entrySet()) {
                Data d = e.getValue();      
                canvas.drawCircle(d.getX(), d.getY(), 10, circlepaint);
            }
        }

        canvas.save();
        canvas.restore();
    }

    /*
    * Nested class to manage the threads for the SurfaceView
    */
    class PanelThread extends Thread {
        private SurfaceHolder _surfaceHolder;
        private Panel _panel;
        private boolean _run = false;

        public PanelThread(SurfaceHolder surfaceHolder, Panel panel) {
            _surfaceHolder = surfaceHolder;
            _panel = panel;
        }

        public void setRunning(boolean run) {
            _run = run;
        }

        public SurfaceHolder getSurfaceHolder() {
            return _surfaceHolder;
        }

        @Override
        public void run() {
            Canvas c;
            while (_run) {
                c = null;
                try {
                    c = _surfaceHolder.lockCanvas(null);
                    synchronized (_surfaceHolder) {
                        _panel.onDraw(c);
                    }
                } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        _surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }//end of PanelThread

}//end of Panel

我遇到的问题是,一旦我初次调用Panel类,我将从服务中获取新记录,因此,我的Info Map数据结构将会更新。但是,我的Panel类只是陷入循环,从不接收任何新的Data对象。我发现的SurfaceViews的所有示例都涉及在SurfaceView类本身内更新它们的方法(例如触摸屏幕并创建新图像等)。遗憾的是,我对这个特定问题感到困惑。有没有更好的方法来设计我的Activity / SurfaceView交互?是否需要额外的视图?

1 个答案:

答案 0 :(得分:1)

通过快速查看代码,我认为问题是您正在尝试从UI线程更新数据结构,同时在PanelThread中对其进行操作。它看起来你的Activity类和Panel也不同步 - 我没有在Panel类中看到setData方法,但我认为它更新了测试静态成员变量。

我的建议:

1 - 测试不应该是静态的 - 它基本上与该类的实例相关联。  2 - 触摸数据时使用同步(测试)。这应该用于设置和读取数据。  3 - 在运行循环中进行睡眠或等待调用 - 这为活动UI线程提供了更新测试的时间。还节省了电池,并允许您选择帧速率进行更新。