更新图像

时间:2010-06-21 07:21:01

标签: blackberry blackberry-storm

我正在开发一个使用一些图像的应用程序。我们需要在图像上打印一些文本,但是当更改变量值时会自动更改文本,这不是固定的意思。我们只是说我们在图像上打印一个变量?

1 个答案:

答案 0 :(得分:0)

试试这个

 public class Test extends UiApplication {

        public static void main(String[] arg) {
            Test app = new Test();
            app.enterEventDispatcher();
        }

        public Test() {
            MyScreen screen = new MyScreen();
            pushScreen(screen);
        }
    }
    class MyScreen extends MainScreen {
        LabelField label;
        public MyScreen() {
            label = new LabelField() {
                protected void paint(Graphics g) {
                    Bitmap bitmap = Bitmap.getBitmapResource("bgimage.jpg");
                    g.drawBitmap(0, 0, getWidth(), getHeight(), bitmap, 0, 0);
                    super.paint(g);
                }
            };

            ButtonField button = new ButtonField("update") {
                protected void fieldChangeNotify(int context) {
                    update();
                    super.fieldChangeNotify(context);
                }
            };

            add(label);
            add(button);
        }

        public void setMessage(String message) {
            synchronized (UiApplication.getEventLock()) {
                label.setText(message);
            }
        }

        private void update() {
            LocationHandler handler = new LocationHandler(this);
            handler.start();
        }
    }

    class LocationHandler extends Thread {
        private MyScreen screen;

        public LocationHandler(MyScreen screen) {
            this.screen = screen;
        }

        public void run() {

            Criteria criteria = new Criteria();
            criteria.setVerticalAccuracy(50);
            criteria.setHorizontalAccuracy(50);
            criteria.setCostAllowed(true);
            criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
            try {

                LocationProvider provider = LocationProvider.getInstance(criteria);
                Location location = provider.getLocation(-1);
                String speed = location.getSpeed() + "m/s";
                screen.setMessage(speed);
            } catch (LocationException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }