【Java开源代码栏目提醒】:本文主要为网学会员提供ClickWheelV2.java,希望对需要ClickWheelV2.java网友有所帮助,学习一下!
package org.chapter9;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class ClickWheelV2 extends Activity {
private TouchView v;
float x0 = -1.0f, y0 = -1.0f;
private int mCounter = 0;
public float mX, mY, dx, dy;
public final float TOUCH_TOLERANCE = 4;
// for speed calculation
long diffTime, temp;
int speed = 1;
int dir;
final int CLICK = 0, RIGHT = 1, LEFT = 2, DOWN = 3, UP = 4;
//
String pts = null;
public float mX2, mY2;
private int buttonNo = -1;
final int NONE_BUTTON = -1, WHEEL_BUTTON = 0, OK_BUTTON = 1;
final int M_BUTTON = 2, B_BUTTON = 3, F_BUTTON = 4, P_BUTTON = 5;
final int RU = 1, LU = 2, LD = 3, RD = 4;
private int iTouchCount = 0, degreeOld, degreeNew;
String curDir = "";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
v = (TouchView) findViewById(R.id.View01);
v.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
float x = event.getX();
float y = event.getY();
if(event.getAction()==MotionEvent.ACTION_DOWN)
buttonNo = clickModel(x, y); // for modeling
if(isClicked(event.getAction(), x, y)) {
mCounter=0;
updateCounter(CLICK, 1);
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
temp = System.currentTimeMillis(); // for speed calculation
x0 = x;
y0 = y;
break;
case MotionEvent.ACTION_MOVE:
int iCX = TouchView.width/2;
int iCY = TouchView.height/2;
System.out.println("iCX= "+iCX+", "+"iCY= "+iCY);
double absX = Math.abs(x - iCX);
double absY = Math.abs(y - iCY);
double radian = Math.asin( (y-iCY) /Math.sqrt(absX*absX + absY*absY));
int t;
if( buttonNo==WHEEL_BUTTON) {
if(x >= iCX && y >= iCY) {
t = (int) degreeOf(RD, radian);
setTitle("("+((int)x)+", "+((int)y)+")"+": 4th, theda= "+t);
}
else if(x >= iCX && y < iCY) {
t = (int) degreeOf(RU, radian);
setTitle("("+((int)x)+", "+((int)y)+")"+": 1st, theda= "+t);
}
else if(x < iCX && y >= iCY) {
t = (int) degreeOf(LD, radian);
setTitle("("+((int)x)+", "+((int)y)+")"+": 3rd, theda= "+t);
}
else {//if(x < iCX && y < iCY)
t = (int) degreeOf(LU, radian);
setTitle("("+((int)x)+", "+((int)y)+")"+": 2nd, theda= "+t);
}
if(thedaChanged(t)) {
curDir = directionOfRotation();
setTitle(getTitle()+ ", " + curDir);
updateCounter(curDir, t);
}
}
break;
case MotionEvent.ACTION_UP:
buttonNo = NONE_BUTTON;
iTouchCount = 0;
break;
}
return true;
}} );
}
private boolean thedaChanged(int t) {
if(iTouchCount==0) {
degreeOld = t;
iTouchCount=1;
}
else { //if(iTouchCount==1) {
if(degreeOld==t)
return false;
else {
degreeOld = degreeNew;
degreeNew = t;
}
}
return true;
}
private String directionOfRotation() {
if(degreeOld > degreeNew || degreeOld - degreeNew == 359)
return "CW";
else if(degreeOld < degreeNew || degreeOld - degreeNew == -359)
return "CCW";
return curDir;
}
private boolean isClicked(int action, float x, float y) {
if(action==MotionEvent.ACTION_DOWN){
mX = x;
mY = y;
mX2 = x;
mY2 = y;
if(buttonNo==WHEEL_BUTTON) {
TouchView.debugWheel = true;
v.invalidate(); //
}
} else if(action==MotionEvent.ACTION_UP){
TouchView.debugWheel = false;
v.invalidate();
dx = Math.abs(x - mX);
dy = Math.abs(y - mY);
if (dx < TOUCH_TOLERANCE && dy < TOUCH_TOLERANCE
&& buttonNo!=WHEEL_BUTTON && buttonNo!=NONE_BUTTON) {
return true;
}
}
return false;
}
private int clickModel(float x0, float y0) {
int xT = (int) x0 - TouchView.width/2; // 参数 x0
int yT = (int) y0 - TouchView.height/2; // 参数 y0
int aT = xT*xT+yT*yT;
if(aT>TouchView.radiusBig*TouchView.radiusBig) {
setTitle("x= "+xT+", y="+yT+" outside 0");
return NONE_BUTTON;
}
else {
if(aT<=TouchView.radiusSmall*TouchView.radiusSmall) {
setTitle("x= "+xT+", y="+yT+" inside 1"); // 中心圆圈之内
return OK_BUTTON;
}
for(int i=0; i<8; i+=2) {
xT = (int) x0 - TouchView.buttonCoors[i];
yT = (int) y0 - TouchView.buttonCoors[i+1];
aT = xT*xT+yT*yT;
if(aT<=TouchView.radiusButton*TouchView.radiusButton) {
setTitle("x= "+xT+", y="+yT+" inside "+(i/2+2)); // 四个功能圆圈内
return (i/2+2); // M_BUTTON = 2, B_BUTTON = 3, F_BUTTON = 4, P_BUTTON = 5
}
}
setTitle("x= "+xT+", y="+yT+" inside 0"); // 外圆圈之内
return WHEEL_BUTTON;
}
}
private double degreeOf(int quadrant, double radian) {
double degree = Math.toDegrees(radian);
switch(quadrant) {
case RU:
degree *= (-1);
break;
case LU:
degree += 180;
break;
case LD:
degree += 180;
break;
case RD:
degree *= (-1);
degree += 360;
break;
}
return degree;
}
private void updateCounter(int dir, int speed) {
switch(dir) {
case CLICK: return;
case RIGHT: mCounter+=speed;
break;
case LEFT: mCounter-=speed;
break;
case DOWN: mCounter+=(2*speed);
break;
case UP: mCounter-=(2*speed);
break;
}
setTitle(getTitle()+", C="+String.valueOf(mCounter));
}
private void updateCounter(String dir, int speed) {
if(dir.equals("CW")) {
mCounter+=speed;
}
else { //if(dir.equals("CCW"))
mCounter-=speed;
}
setTitle(getTitle()+", C="+String.valueOf(mCounter));
}
}
上一篇:
ClickWheelV1.java
下一篇:
法律专业开题报告范文