仅仅是个练习而已!只做了第一个菜单的事件。其他都没事件。
代码片段
public mPayCanvas(Context context) {
super(context);
//获得屏幕宽高
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
Log.i("TAG", "screenWidth="+width);//320
Log.i("TAG", "screenHeight="+height);//480
width=320;
height=430;
// TODO Auto-generated constructor stub
text=new String[10];
paint=new Paint[10];
rect=new RectF[10];
col=3;//菜单列数
state=ST_Instruction;
setST(state);
setFocusable(true); // 如果要响应按键,必须加上这句
clearFocus();//必须加,以防第一次按键时无效。
// setClickable(true);
setFocusableInTouchMode(true);
// setPressed(true);
setLongClickable(false);
setTitleColor(0xffff0000);
}
private void draw_Menu(Canvas canvas){
// canvas.drawText("菜单", (getWidth()-(int)paint[0].measureText(text[0]))>>1, getHeight()>>1, paint[0]);
for(int i=0;i<drawable.length;i++){
drawable[i].draw(canvas);
canvas.drawText(menu_item[i], drawable[i].getBounds().left, drawable[i].getBounds().bottom+paint[2].getTextSize(), paint[2]);
}
RectF rect_menu = new RectF(drawable[index].getBounds().left, drawable[index].getBounds().top, drawable[index].getBounds().right, drawable[index].getBounds().bottom+paint[2].getTextSize()+5);
canvas.drawRect(rect_menu, paint[3]);
drawButton(canvas,true,true);
}
private void touchEvent_Menu(MotionEvent event){
float x=event.getX();
float y=event.getY();
if(x>rect[0].left&&x<rect[0].right&&y>rect[0].top&&y<rect[0].bottom){
switch (index) {
case 0:
setST(ST_InputNumber);
break;
case 1:
case 2:
case 3:
Log.i("TAG", "index====="+index);
break;
default:
break;
}
return;
}else if(x>rect[1].left&&x<rect[1].right&&y>rect[1].top&&y<rect[1].bottom){
setST(ST_Exit);
return;
}
int xx=drawable[0].getBounds().left;
int yy=drawable[0].getBounds().top;
int w=drawable[0].getIntrinsicWidth();//32
int h=drawable[0].getIntrinsicHeight()+(int)paint[2].getTextSize();//48+
int gapX=xx;
int gapY=this.gapY-(int)paint[2].getTextSize();
// Log.i("TAG", "w=="+w);
// Log.i("TAG", "h=="+h);
// Log.i("gapX", "gapX=="+gapX);
// Log.i("gapY", "gapY=="+gapY);
int indexX=0;
int indexY=0;
for(int i=0;i<drawable.length;i++){
indexX=i%col;
indexY=i/col;
if (x >= xx+indexX*gapX + indexX * w && x < xx+indexX*gapX + (indexX + 1) * w && y >= yy+indexY*gapY + indexY * h && y < yy+indexY*gapY + (indexY + 1) * h) {
this.index=i;
this.indexX=indexX;
this.indexY=indexY;
// Log.i("TAG", "index=="+index);
// Log.i("TAG", "indexX=="+indexX);
// Log.i("TAG", "indexY=="+indexY);
// Log.i("TAG", "==============================================");
keyDown_Menu(KeyEvent.KEYCODE_DPAD_CENTER,null);
break;
}
}
}
private void drawButton(Canvas canvas,boolean hasLeft,boolean hasRight){
if(hasLeft){
canvas.drawText(text[8], rect[0].left+2, rect[0].bottom-5, paint[0]);
// canvas.drawText(text[8], x+5, height-5, paint[0]);
// rect[0] = new RectF(2, getHeight()-paint[0].getTextSize()-2-5, 2+paint[0].measureText(text[8])+5, getHeight()-2);
canvas.drawRect(rect[0], paint[3]);
}
if(hasRight){
canvas.drawText(text[9], rect[1].left+2, rect[1].bottom-5, paint[0]);
// canvas.drawText(text[9], width-paint[0].measureText(text[9])-2, height-5, paint[0]);
// rect[1] = new RectF(getWidth()-paint[0].measureText(text[9])-5, getHeight()-paint[0].getTextSize()-2-5, getWidth()-2, getHeight()-2);
canvas.drawRect(rect[1], paint[3]);
}
}
private void drawGrid(Canvas canvas){
String[][] pad={{"1","2","3"},{"4","5","6"},{"7","8","9"},{"退格","0","全清"}};
RectF rect=null;
int col=pad[0].length;
int row=pad.length;
int w=80;
int h=60;
int x=((width-col*w)>>1);
int y=(height>>1)-h*2;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
rect = new RectF(x+j*w,y+i*h,x+j*w+w,y+h+i*h);
canvas.drawRect(rect, paint[3]);
canvas.drawText(pad[i][j], rect.left+((int)(w-paint[0].measureText(pad[i][j]))>>1),rect.top+(h>>1)+5, paint[0]);
}
}
}
- 大小: 12.5 KB
- 大小: 12.1 KB
- 大小: 25 KB
- 大小: 13.5 KB
- mPay.rar (68.7 KB)
- 下载次数: 18
- 大小: 24.5 KB
- 查看图片附件