骑猪闯天下

J2ME随笔,记录成长的脚步

统计

留言簿(3)

阅读排行榜

评论排行榜

[J2ME]触摸屏功能实现代码一


触摸屏功能的实现大致来说要在三处添加代码:
1.在Screen类中重写触摸功能;
2.建立一个触摸屏对象,使要实现触摸功能的控件以对象的方式保存在Vector中;
3.在获取页面时,把要实现的触摸控件保存在Vector中;

以项目中的源码为例,这三个部分的代码如下:

1.Screen类:
(本源码最后两个方法为:字符占屏幕宽度及高度的计算方法)

/**
 * Data:2010.02.25
 * Time:15:18:20
 
*/

package com.xuudoo.ewl;

import
 java.util.Vector;

import
 javax.microedition.lcdui.Canvas;
import
 javax.microedition.lcdui.Command;
import
 javax.microedition.lcdui.CommandListener;
import
 javax.microedition.lcdui.Display;
import
 javax.microedition.lcdui.Displayable;
import
 javax.microedition.lcdui.Font;
import
 javax.microedition.lcdui.Graphics;
import
 javax.microedition.lcdui.Image;
import
 javax.microedition.midlet.MIDlet;

import
 com.xuudoo.booking.BizWindow;
import
 com.xuudoo.booking.BookingMidlet;
import
 com.xuudoo.booking.BusinessWindow;
import
 com.xuudoo.booking.FirstPage;
import
 com.xuudoo.booking.IOManager;
import
 com.xuudoo.booking.UIManager;

public class EWLScreen extends Canvas // FullCanvas

        implements CommandListener {

    
public static final int SCREEN_WIDTH =  360;//屏宽

    public static final int SCREEN_HEIGHT =  640;//屏高
    
    
public static final int KEY_CODE_LEFT_SOFT_KEY = -6;//左软键

    public static final int KEY_CODE_RIGHT_SOFT_KEY = -7;//右软键
    
    
public static EWLScreen instance = new
 EWLScreen();
    
public static
 Display display;
    
protected
 EWLWindow activeWindow;
    
protected
 EWLWindow popupWindow;
    
    
public static Image splashImg = null;//初始化图片

    private static String splashImgPath = "/LogoXD.png";//初始化图片地址
    public static Image main_bg = null;//背景图片
    private static String main_bgPath = "/bg.png";//背景图片地址
    public static boolean connectAgain = false;//用cmnet联网失败后再连接
    public static boolean connectPrompt = true;//第一此联网询问时是否连接网络
    public static boolean phoneNoMoney = false;//手机欠费标志位
    
    
public EWLScreen() 
{
        setFullScreenMode(
true
);
    }


    
public static void init(MIDlet midlet) {
        display 
=
 Display.getDisplay(midlet);
        instance.iniLogoPic();
    }


    
public int getHeight() {
        
return
 SCREEN_HEIGHT;
    }


    
public int getWidth() {
        
return
 SCREEN_WIDTH;
    }


    
/**
     * 第一次登陆,画Logo登录 图片
     
*/

    
public void iniLogoPic() {
        
//图片资源

        try{
            splashImg 
=
 Image.createImage(splashImgPath);
            main_bg 
=
 Image.createImage(main_bgPath);
        }
catch(Exception e)
        
{
            
if
(BookingMidlet.DEBUG)
                e.printStackTrace();
        }

    }

    
    
//调试时显示信息之用
    public static String prompt1 = null;
    
public static String prompt2 = null
;
    
public static String prompt3 = null
;
    
protected void paint(Graphics g) 
{
        
//登陆时的提示界面

        if(UIManager.state == UIManager.STATE_LOGIN){
            
if(splashImg != null
)
//
                g.drawImage(splashImg, (EWLScreen.SCREEN_WIDTH - splashImg.getWidth()) / 2,
//                        (EWLScreen.SCREEN_HEIGHT - splashImg.getHeight()) / 2, 20);

                g.drawImage(splashImg, 0,020);
            drawConnectPrompt(g);
//画提示信息

            String leftSoftStr="登陆";
            
if
(connectAgain)
                leftSoftStr
=null
;
            
this.drawSoftKeys(g, leftSoftStr, "退出"0xff0000);//画提示界面左右软键

            return;
        }
else{
//
            if(main_bg != null)
//                g.drawImage(main_bg,0,0,20);

            if (activeWindow != null){
                activeWindow.paint(g);
            }

            
if (popupWindow != null){
                popupWindow.paint(g);
            }

        }

        
//-----------------------调试信息-----------------------------
//
            g.setColor(0xff0000);
//
            if(prompt1 != null )
//
                g.drawString(prompt1, 10, 260, 0);
//
            if(prompt2 != null )
//
                g.drawString(prompt2, 10, 280, 0);
//
            if(prompt3 != null )
//                g.drawString(prompt3, 10, 240, 0);

    }


    
/**
     * 画联网提示,并兼容原来系统 的字体和颜色
     
*/

    
private void drawConnectPrompt(Graphics g){
        
int nowColor =
 g.getColor();
        Font nowFont 
=
 g.getFont();
        g.setColor(
0x000000
);
        g.setFont(UIManager.my_smallFont);
        
        String httpConMode
=""
;
        String promptStr1 
=""
;
        String promptStr2 
=""
;
        
if(BookingMidlet.useProxy)
{
            promptStr1 
="友情提示:请选用移动梦网"
;
            promptStr2 
="连接方式。"
;
            httpConMode
="移动梦网"
;
        }
else{
            promptStr1 
="友情提示:请选用CMNET"
;
            promptStr2 
="或WLAN连接方式。"
;
            httpConMode
="CMNET或WLAN"
;
        }

        
        
if(connectAgain){
            promptStr1
="友情提示:请重新登录"
;
            
if(!connectPrompt)
{
                promptStr2
="并同意使用网络"
;
            }
else{
                promptStr2
="选用"+
httpConMode;
                
if
(phoneNoMoney)
                    promptStr2
="确认网络信号或手机话费"
;
            }

        }

        
int promptStr_X= (EWLScreen.SCREEN_WIDTH-getStringWidth(g.getFont(),promptStr1))/2;
        
int promptStr_Y= EWLScreen.SCREEN_HEIGHT*3/5
;
        g.drawString(promptStr1, promptStr_X, promptStr_Y, 
0
);
        g.drawString(promptStr2, promptStr_X, promptStr_Y
+g.getFont().getHeight(), 0
);
        g.setColor(nowColor);
        g.setFont(nowFont);
    }

    
/**
     * 绘制左右软键
     *
     * 
@param lSoftKeyName 左软键名称 如果null则不绘制
     * 
@param
 rSoftKeyName 右软键名称 如果null则不绘制
     * 
@param
 color        绘制软键的颜色
     * 
     
*/

    
private void drawSoftKeys(Graphics g, String lSoftKeyName,
            String rSoftKeyName, 
int
 color)
    
{
        
if (lSoftKeyName != null
)
        
{// 左软键
            int x = FirstPage.LRSoft_LeftAndTopPosition[0][0]+ FirstPage.LRSoft_length_high[0]/2;
            
int y = FirstPage.LRSoft_LeftAndTopPosition[0][1]+ FirstPage.LRSoft_length_high[1]/2
;
//            g.setClip(x, y, getStringWidth(g.getFont(), lSoftKeyName), g.getFont().getHeight());

            g.drawString( lSoftKeyName, x, y, Graphics.HCENTER|Graphics.BASELINE);
        }

        
if (rSoftKeyName != null)
        
{// 右软键
            int x = FirstPage.LRSoft_LeftAndTopPosition[1][0]+ FirstPage.LRSoft_length_high[0]/2;
            
int y = FirstPage.LRSoft_LeftAndTopPosition[1][1]+ FirstPage.LRSoft_length_high[1]/2
;
//            g.setClip(x, y, getStringWidth(g.getFont(), rSoftKeyName), g.getFont().getHeight());

            g.drawString(rSoftKeyName, x, y, Graphics.HCENTER|Graphics.BASELINE);
        }

        
    }

    
    
    
/**
     * 初始化提示界面时的交互处理
     * 
@param keyCode
     
*/

    
private void stateLoginKeyPressed(int keyCode){
        
int gameCode = 0
;
        
try
{
            gameCode 
= this
.getGameAction(keyCode);
        }
catch(Exception e){
            System.out.println(
"get gameaction key error:"+
keyCode);
        }

        
if(gameCode==Canvas.UP || gameCode==Canvas.LEFT
                
|| gameCode==Canvas.RIGHT || gameCode==
Canvas.DOWN
                
|| gameCode==
Canvas.FIRE)
        
{
            ;
        }

        
else if (keyCode == EWLScreen.KEY_CODE_LEFT_SOFT_KEY
                
|| keyCode == EWLScreen.KEY_CODE_RIGHT_SOFT_KEY) 
{
            gameCode 
=
 keyCode;
        }

        
else if(keyCode==21||keyCode==-21)
            gameCode 
=
 EWLScreen.KEY_CODE_LEFT_SOFT_KEY;
        
else if(keyCode==22||keyCode==-22
)
            gameCode 
=
 EWLScreen.KEY_CODE_RIGHT_SOFT_KEY;
        
else

            
return;
        
        
switch (gameCode) 
{
            
case
 Canvas.FIRE:
            
case EWLScreen.KEY_CODE_LEFT_SOFT_KEY://
 左软键
                
//向服务器验证身份,打开首页面

                UIManager.state=UIManager.STATE_LOGIN_httpConnecting;
                BizWindow bw 
= new
 BizWindow();
                bw.commitUserAuth();
                
//在用户向服务器发起请求返回成功后再显示首页面。

                break;
            
case EWLScreen.KEY_CODE_RIGHT_SOFT_KEY://
 右软键
                
//退出。

                UIManager.state=UIManager.STATE_DEFAULE;
                UIManager._flag 
= 0
;
                UIManager._instance.doLoop();
                
break
;
        }

    }

    
    
protected void keyPressed(int keyCode) {
        
//处理初始化时的提示界面

        if(UIManager.state == UIManager.STATE_LOGIN){
            
if(!connectAgain)
{
                stateLoginKeyPressed(keyCode);
            }
else
{
                
if(keyCode == -7 || keyCode == -22 || keyCode == 22
)
                    UIManager._flag 
= 0
;
                    UIManager._instance.doLoop();
                    
return
;
            }

        }

        
else if(UIManager.state == UIManager.STATE_LOGIN_httpConnecting){
            
//只对返回键做处理

            if(keyCode == -7 || keyCode == -22 || keyCode == 22){
                connectAgain 
= true
;
                connectPrompt 
= true
;
                UIManager.state
=
UIManager.STATE_LOGIN;
                hidePopup();
            }

        }
else if(UIManager.state == UIManager.STATE_DEFAULE){
            
try 
{
                
if (popupWindow != null
)
                    popupWindow.keyPressed(keyCode);
                
else if (activeWindow != null
)
                    activeWindow.keyPressed(keyCode);
            }
 catch (Throwable e) {
                e.printStackTrace();
            }

        }

    }


    
protected void keyReleased(int keyCode) {
        
if(UIManager.state == UIManager.STATE_LOGIN ||

           UIManager.state 
== UIManager.STATE_LOGIN_httpConnecting ){
            
return
;
        }

        
try {
            
if (popupWindow != null
)
                popupWindow.keyReleased(keyCode);
            
else if (activeWindow != null
)
                activeWindow.keyReleased(keyCode);
        }
 catch (Throwable e) {
            e.printStackTrace();
        }

    }


    
protected void keyRepeated(int keyCode) {
        
if(UIManager.state == UIManager.STATE_LOGIN ||

           UIManager.state 
== UIManager.STATE_LOGIN_httpConnecting ){
            
return
;
        }

        
try {
            
if (popupWindow != null
)
                popupWindow.keyRepeated(keyCode);
            
else if (activeWindow != null
)
                activeWindow.keyRepeated(keyCode);
        }
 catch (Throwable e) {
            e.printStackTrace();
        }

    }


    
/**
     * 笔触 点击
     * 
@param a_x: x 坐标
     * 
@param
 a_y: y 坐标
     
*/

    
protected void pointerPressed(int a_x,int a_y)
    
{
        
//对九宫格界面处理

        if(UIManager._flag == 20){
            
boolean clickFocus =
 getFocusFromPointer( a_x, a_y);
            
if(clickFocus)
{
                keyReleased(
-5
);
                
return
;
            }
else{
                repaint();
            }

//            return;
        }

        
//对左右软件处理
        String clickLRSoft = getFocusFromPointer_LRSoft( a_x, a_y);
        
if(!clickLRSoft.equals(""))
{
            
if(clickLRSoft.equals("LEFTSOFT"))
{
                keyPressed(
-6
);
                keyReleased(
-6
);
            }

            
else if(clickLRSoft.equals("RIGHTSOFT")){
                keyPressed(
-7
);;
                keyReleased(
-7
);
            }

            
return;
        }

        
//对触摸屏对象处理
        if(UIManager._instance.i_clipArea != null){
            
boolean browseClicpFocus =
 browseClip(a_x, a_y);
            
if(browseClicpFocus)
{
                keyPressed(
-5
);;
                keyReleased(
-5
);
            }
else{
                repaint();
            }

        }

    }

    
    
protected void pointerReleased(int x, int y)
    
{
        
//在pointerPressed中处理

    }

    
    
/**
     * 笔触 拖拉
     * x : x 坐标
     * y : y 坐标
     
*/

    
protected void pointerDragged(int x,int y)
    
{
        
//暂未实现

    }

    
    
/**
     * 列表界面的触摸屏判断
     * 
@param a_x
     * 
@param
 a_y
     * 
@return

     
*/

    
protected boolean browseClip(int a_x, int a_y){
        
boolean clickAreaIsFocus = false
;
        Vector i_clipArea 
=
 UIManager._instance.i_clipArea;
        
if(i_clipArea==null
)
            
return false
;
        ClipRegion a_clipRegion 
= null
;
        
for (int i = 0; i < i_clipArea.size(); i++
{
            a_clipRegion 
=
 (ClipRegion) i_clipArea.elementAt(i);
            
boolean findClipArea =
 a_clipRegion.inClipRegion(a_x, a_y);
            
if(findClipArea)
{//点击区域
                clickAreaIsFocus = activeWindow.pointerPressSetFocus(a_x ,i);
                
break
;
            }

        }

        i_clipArea
=null;
        
return
 clickAreaIsFocus;
    }

    
    
    
/**
     * 通过x,y坐标,获得笔触的焦点值
     * 
@param a_x:笔触的x
     * 
@param
 a_y:笔触的y
     * 
@param
 focus_X:笔触所在九宫格的起始x
     * 
@param
 focus_Y:笔触所在九宫格的起始y
     * 
@return

     
*/

    
protected boolean getFocusFromPointer(int a_x, int a_y){
        
int focus_X = 0
;
        
int focus_Y = 0
;
        
boolean clickAreaIsFocus = false
;
        
for(int i=0; i<9; i++
)
        
{
            focus_X 
= FirstPage.itemsPosition[i][0
];
            focus_Y 
= FirstPage.itemsPosition[i][1
];
            
            
if(focus_X <= a_x &&

               focus_Y 
<= a_y &&
               focus_X 
+ FirstPage.VSize >= a_x &&
               focus_Y 
+ FirstPage.HSize >= a_y)
            
{//笔触所在的九宫格
                if(i==FirstPage.currentItem)
                    clickAreaIsFocus 
= true
;
                FirstPage.currentItem 
=
 i;
                
break
;                
            }

        }

        
return clickAreaIsFocus;
        
    }

    
    
/**
     * 通过x,y坐标,获得笔触的焦点值
     * 
@param a_x:笔触的x
     * 
@param
 a_y:笔触的y
     * 
@param
 focus_X:左、右软件的起始x
     * 
@param
 focus_Y:左、右软件的起始y
     * 
@return

     
*/

    
protected String getFocusFromPointer_LRSoft(int a_x, int a_y){
        
int focus_X = 0
;
        
int focus_Y = 0
;
        String pointSoftKey
=""
;
        
for(int i=0; i<2; i++
)
        
{
            focus_X 
= FirstPage.LRSoft_LeftAndTopPosition[i][0
];
            focus_Y 
= FirstPage.LRSoft_LeftAndTopPosition[i][1
];
            
            
if(focus_X <= a_x &&

               focus_Y 
<= a_y &&
               focus_X 
+ FirstPage.LRSoft_length_high[0>= a_x &&
               focus_Y 
+ FirstPage.LRSoft_length_high[1>= a_y)
            
{//笔触所在的九宫格
                if(i==0)
                    pointSoftKey
="LEFTSOFT"
;
                
else

                    pointSoftKey
="RIGHTSOFT";
                
break
;                
            }

        }

        
return pointSoftKey;
        
    }

    
public void commandAction(Command c, Displayable d) {
        
try 
{
            
if (d == this
)
                
if (instance.popupWindow != null
)
                    instance.popupWindow.onCommand(c);
                
else if (instance.activeWindow != null
)
                    instance.activeWindow.onCommand(c);
        }
 catch (Throwable e) {
            e.printStackTrace();
        }

    }


    
public static EWLWindow getTopWindow() {
        
if (instance.popupWindow != null
)
            
return
 instance.popupWindow;
        
else

            
return instance.activeWindow;
    }


    
public static EWLWindow getCurrent() {
        
return
 instance.activeWindow;
    }


    
public static void setCurrent(EWLWindow window) {
        instance.activeWindow 
=
 window;
        instance.repaint();
    }


    
public static EWLWindow getPopup() {
        
return
 instance.popupWindow;
    }


    
public static void popup(EWLWindow window) {
        instance.popupWindow 
=
 window;
        instance.repaint();
    }


    
public static void hidePopup() {
        
if (instance.popupWindow instanceof
 BusinessWindow)
            IOManager._interrupt 
= true
;
        instance.popupWindow 
= null
;
        instance.repaint();
    }


    
public void clearScreen() {
        repaint();
        serviceRepaints();
    }


    
/***************************************************************************
     * 获取String屏幕宽度操作 开始 ***********************************************
     *************************************************************************
*/

    
private static byte[] i_smallWidth = null;
    
private static byte[] i_mediumWidth = null
;
    
private static byte[] i_largeWidth = null
;
    
public static byte getCharWidth(Font a_font, char
 a_c)
    
{
        initFontWidth();
        
int type =
 a_font.getSize();
        
byte
[] widthType;
        
if(type ==
 Font.SIZE_SMALL)
            widthType 
=
 i_smallWidth;
        
else if(type ==
 Font.SIZE_MEDIUM)
            widthType 
=
 i_mediumWidth;
        
else if(type ==
 Font.SIZE_LARGE)
            widthType 
=
 i_largeWidth;
        
else

            widthType 
= i_smallWidth;
        
if(a_c<0 || a_c>=128
)
            
return widthType[128
];
        
else

        
{
            
return
 widthType[a_c];
        }

    }

    
    
/**
     * 获取字符的宽度
     * 
@param a_font
     * 
@param
 a_str
     * 
@return

     
*/

    
public static int getStringWidth(Font a_font, String a_str)
    
{
        
if(a_str ==null
)
            
return 0
;
        
int width = 0
;
        
char
 currChar;
        
for(int i=0;i<a_str.length();i++
)
        
{
            currChar 
=
 a_str.charAt(i);
            width 
+=
 getCharWidth(a_font, currChar);
        }

        
return width;
    }

    
private static void initFontWidth()
    
{
        
if(i_smallWidth!=null
)
            
return
;

        i_smallWidth 
= new byte[129
];
        i_mediumWidth 
= new byte[129
];
        i_largeWidth 
= new byte[129
];

        Font i_fontSmall 
=
 Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        Font i_fontMedium 
=
 Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
        Font i_fontLarge 
=
 Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
        
for(int i=0;i<128;i++
)
        
{
            i_smallWidth[i] 
= (byte)i_fontSmall.charWidth( ((char
)i) );
            i_mediumWidth[i] 
= (byte)i_fontMedium.charWidth( ((char
)i) );
            i_largeWidth[i] 
= (byte)i_fontLarge.charWidth( ((char
)i) );
        }


        i_smallWidth[
128= (byte)i_fontSmall.charWidth( '' );
        i_mediumWidth[
128= (byte)i_fontMedium.charWidth( ''
 );
        i_largeWidth[
128= (byte)i_fontLarge.charWidth( ''
 );
    }

    
/***************************************************************************
     * 获取String屏幕宽度操作结束************************************************
     *************************************************************************
*/

}

posted on 2010-02-25 15:23 骑猪闯天下 阅读(800) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: