Blackberry中的图形和触摸事件?

时间:2011-05-23 00:49:13

标签: events blackberry graphics touch

最后我明白了如何处理Blackberry上的触摸和手势事件,但现在我有两个问题: 1)如何在能够处理触摸事件的同时将图形渲染到另一个图形上方? 2)如何在触摸事件上绘制简单的矩形,比如CLICK?

我的代码&屏幕:

package mypackage;

import net.rim.device.api.lcdui.game.BlackBerryGameCanvas;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchGesture;
import net.rim.device.api.ui.VirtualKeyboard;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.TouchEvent;


public class MyScreen extends MainScreen
{   
LabelField touch_type;

public boolean onMenu(int instance) {
    return instance == Menu.INSTANCE_CONTEXT ? false : super.onMenu(instance);
    }

public MyScreen()
{    
    super(NO_SYSTEM_MENU_ITEMS);
    getScreen().getVirtualKeyboard().setVisibility(VirtualKeyboard.HIDE_FORCE);


    VerticalFieldManager vf = new VerticalFieldManager();
    touch_type = new LabelField("SOME TEXT", FIELD_HCENTER);
    vf.add(touch_type);
    vf.add(new HandleTouch());
    add(vf);
}

/*
 * Implementing touch handler class
 */
class HandleTouch extends Field {

    protected void layout(int width, int height) {
        setExtent(360, 460);
    }

    public void paint(Graphics graphics) {
        graphics.drawBitmap(0, 0, this.getWidth(), this.getHeight(), Bitmap.getBitmapResource("bg.png"), 0, 0);
    }

    protected void drawFocus(Graphics g, boolean on) {}

    public boolean isFocusable() { return true;}

    public void drawBall(int x, int y) {

    }

    protected boolean touchEvent(TouchEvent message) {
        switch( message.getEvent() ) {
        case TouchEvent.CLICK:
            int x = message.getGlobalX(1);
            int y = message.getGlobalY(1);

            touch_type.setText("CLICK");
            return true;
        case TouchEvent.DOWN:
            //System.out.println("----------------------------->DOWN");
            touch_type.setText("DOWN");
            return true;    
        case TouchEvent.MOVE:
            //System.out.println("----------------------------->MOVE");
            touch_type.setText("MOVE");
            return true;    
        case TouchEvent.UNCLICK:
            //System.out.println("----------------------------->UNCLICK");
            touch_type.setText("UNCLICK");
            return true;
        case TouchEvent.GESTURE:
            TouchGesture gesture = message.getGesture();
            int gestureCode = gesture.getEvent();
                switch (gestureCode) {
                case TouchGesture.HOVER:
                    //System.out.println("----------------------------->HOVER");
                    touch_type.setText("HOVER");
                    return true;
                case TouchGesture.SWIPE:
                    //System.out.println("----------------------------->SWIPE");
                    touch_type.setText("SWIPE");
                    return true;    
                case TouchGesture.TAP:
                    //System.out.println("----------------------------->TAP");
                    touch_type.setText("TAP");
                    return true;
                case TouchGesture.CLICK_REPEAT:
                    //System.out.println("----------------------------->CLICK REPEAT");
                    touch_type.setText("CLICK REPEAT");
                    return true;
                case TouchGesture.DOUBLE_TAP:
                    //System.out.println("----------------------------->DOUBLE TAP");
                    touch_type.setText("DOUBLE TAP");
                    return true;    
            }   
        }
        //System.out.println("PRINT ME SOMETHING IN ANY CASE");
        super.touchEvent(message);
        return false;
    }

    public HandleTouch() {
    }
}

}

touch and gestures sample app

1 个答案:

答案 0 :(得分:0)

1。)我不确定究竟是什么问题。你能改写一下还是举个例子?一旦我能弄明白,我就会更新这个答案。

2.。)只需在DOWN事件上设置一个标志,保存触摸坐标,调用invalidate(),并在paint方法中检查所述标志,如果存在,则在坐标处/周围绘制矩形。在UP事件中取消设置标志,调用invalidate(),然后删除矩形。