java - 使用新元素即时更新 Android SurfaceView

标签 java android graphics surfaceview

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

目前,我有一个 Activity 和一个负责应用程序记录收集部分的服务。 Activity将 Intent 传递给Service,Service通过返回需要显示的记录来响应。这些记录作为数据类的实例存储在我的程序中,并且仅具有应在 View 中绘制元素的屏幕坐标(我只是为数据库中的每个记录绘制一个圆圈)。

为了简洁起见,我不会包含该服务,但我将包含 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 类进行初始调用,我将从服务中获取新记录,因此,我的信息 map 数据结构将得到更新。然而,我的 Panel 类只是陷入了循环,并且从未接收到任何新的 Data 对象。我发现的 SurfaceView 的所有示例都涉及更新 SurfaceView 类本身内的方法(例如触摸屏幕和创建新图像等)。遗憾的是,我对这个特定问题感到困惑。是否有更好的方法来设计我的 Activity/SurfaceView 交互?是否需要额外的 View ?

最佳答案

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

我的建议:

1 - 测试不应该是静态的 - 它从根本上与类的此实例相关联。 2 - 触摸数据时使用同步(测试)。设置和读取数据都应该这样做。 3 - 在运行循环中放置 sleep 或等待调用 - 这为 Activity UI 线程提供了更新测试的时间。还可以节省电池并允许您选择更新的帧速率。

关于java - 使用新元素即时更新 Android SurfaceView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3418402/

相关文章:

java - 如何防止paintComponent()从一开始就运行?

javascript - 绘制始终指向远离对象的箭头

java - 无法在已托管其他 Web 应用程序的 tomcat 中运行 Jenkins

java - Android:下载到 SD 时出现问题

java - 用 Java 显示 GUI 组件

android - 将 ViewPager fragment 化为 Play 商店应用程序?

java - Intent 服务并发

java - 王牌 :dataTable search dose not update command button action

java - 加载本地化资源

java - 使用 Visitor 和 Composite 模式构建过滤流