【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了TouchGrid.java提供大家参考,希望对大家有所帮助!
/*
* Copyright (C) 2010 The CSOW Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing per
missions and
* limitations under the License.
*/
package org.chapter14;
import java.io.ByteArrayInputStream;
import com.Chapter13.RD.ParseException;
import com.Chapter13.RD.RD_Parser;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
enum ActionType {
R, L, D, U,
}
public class TouchGrid implements OnTouchListener {
Context context;
static RD_Parser parser = null;
String sParse = "";
public int iComposed = 0, iComposedBak = -1;
float x, y, state;
int nowButtonX,nowButtonY,nowSiteX,nowSiteY,lastButtonX,lastButtonY;
int deltaRow = 0, deltaCol = 0;//纪录位移方向
String edgeFSM = "";
int colWidth, rowHeight;
int xStart, yStart;
int iIncDec = 0, iBak = 0;
boolean isMoveReset=true, isClick=true;
public TouchGrid(Context context, int row, int col) {
this.context = context;
colWidth = ((CSOW_Musicbox) context).width / col;
rowHeight = ((CSOW_Musicbox) context).height / row;
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
state = event.getAction();
if(state==MotionEvent.ACTION_DOWN||state==MotionEvent.ACTION_MOVE){
//设定当前所指Button
nowButtonX = (int) ((event.getY() / rowHeight));
nowButtonY = (int) ((event.getX() / colWidth));
if(isMoveReset==true){
nowSiteX=nowButtonX;
nowSiteY=nowButtonY;
isMoveReset=false;
}
if(state==MotionEvent.ACTION_MOVE) {
if(nowSiteX!=nowButtonX||nowSiteY!=nowButtonY) {
//设定离开所指Button
lastButtonX = (int) ((event.getY() / rowHeight));
lastButtonY = (int) ((event.getX() / colWidth));
isClick=false;
deltaRow = nowSiteX - lastButtonX;
deltaCol = nowSiteY - lastButtonY;
forward(); // 检查上、下、左、右
isMoveReset=true;
if( edgeFSM=="\\" ) //如果方向不对
init();
else {
restates();
if( edgeFSM!="\\" )
((CSOW_Musicbox)context).updateScreen();
}
}
}
}
else if(state==MotionEvent.ACTION_UP){//手指离开
if(isClick)
((CSOW_Musicbox)context).click(nowButtonX, nowButtonY);
else {
isClick=true;
}
init();
isMoveReset=true;
}
return true;
}// onTouch
public void forward(){//新一步的计算
if( deltaRow == -1 ) {edgeFSM += "D"; }
else if( deltaRow == 1 ) {edgeFSM += "U"; }
else if( deltaCol == -1 ) {edgeFSM += "R"; }
else if( deltaCol == 1 ) {edgeFSM += "L"; }
else {edgeFSM += "\\";}
};
public void restates(){
if(edgeFSM.length()<2) { // for shift gesture
int j = ((CSOW_Musicbox)context).iCounter;
switch( ActionType.valueOf( edgeFSM ) ){
case R://右
case L://左
//R.drawable.i1_00, R.drawable.i1_01, R.drawable.i1_11, R.drawable.i1_10, // 春
//R.drawable.i2_00, R.drawable.i2_01, R.drawable.i2_11, R.drawable.i2_10, // 夏
//R.drawable.i3_00, R.drawable.i3_01, R.drawable.i3_11, R.drawable.i3_10, // 秋
//R.drawable.i4_00, R.drawable.i4_01, R.drawable.i4_11, R.drawable.i4_10 // 冬
if(j==0) j= 3;
else if(j==3) j= 0;
else if(j==1) j= 2;
else if(j==2) j= 1;
break;
case D://下
case U://上
if(j==0) j= 1;
else if(j==1) j= 0;
else if(j==2) j= 3;
else if(j==3) j= 2;
break;
}
((CSOW_Musicbox)context).iCounter = j;
((CSOW_Musicbox)context).updateScreen();
}
else { // for wheel gesture, edgeFSM.length() >= 2
ByteArrayInputStream Bis1 = null;
try {
Bis1 = new ByteArrayInputStream((edgeFSM+"!").getBytes("UTF-8"));
parser = new RD_Parser(Bis1);
} catch(java.io.UnsupportedEncodingException e) {
} catch(Exception e) {
System.err.println( "parser exception!");
}
try {
sParse = parser.Goal();
try {
MathEvaluator m = new MathEvaluator(sParse);
iComposedBak = iComposed;
iComposed = m.getValue().intValue();
if(iComposed==2&&iIncDec==0) {
iIncDec=-1;
iBak = ((CSOW_Musicbox) context).iCounter;
}
else if (iComposed==-2&&iIncDec==0) {