【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-ClickWheelV4.java,希望本篇文章对您学习有所帮助。
package org.chapter10;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Path;
import android.os.Bundle;
import android.os.Vibrator;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class ClickWheelV4 extends Activity implements OnInitListener {
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 = "";
private TextToSpeech tts;
String [] words = {"OK", "Menu", "Backward", "Forward", "Play", "Pause"};
private boolean bPP = false;
public static Path mPath = new Path();
/** 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);
tts = new TextToSpeech(getApplicationContext(), this);
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
switch(buttonNo) {
case OK_BUTTON:
playSound(OK_BUTTON-1);
break;
case M_BUTTON:
playSound(M_BUTTON-1);
break;
case B_BUTTON:
playSound(B_BUTTON-1);
break;
case F_BUTTON:
playSound(F_BUTTON-1);
break;
case P_BUTTON:
if(bPP==false) {
playSound(P_BUTTON-1);
bPP=true;
}
else{
playSound(P_BUTTON);
bPP=false;
}
break;
}
}
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 void playSound(int which) {
tts.speak(words[which], TextToSpeech.QUEUE_FLUSH, null);
final long VIBRATE_DURATION = 100L;
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(VIBRATE_DURATION);
}
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;
mPath.moveTo(x, y); // for drawing path
if(buttonNo==WHEEL_BUTTON) {
TouchView.debugWheel = true;
v.invalidate(); //
}
} else if(action==MotionEvent.ACTION_UP){
mPath.reset(); //
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;
}
}
// for drawing path
else if(action==MotionEvent.ACTION_MOVE && buttonNo==WHEEL_BUTTON) {
dx = Math.abs(x - mX2);
dy = Math.abs(y - mY2);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX2, mY2, (x + mX2)/2, (y + mY2)/2);
mX2 = x;
mY2 = y;
TouchView.debugWheel = true;
v.invalidate();
}
}
else if(action==MotionEvent.ACTION_MOVE && buttonNo!=WHEEL_BUTTON) {
TouchView.debugWheel = false;
v.invalidate();
}
// end drawing path
return false;
}
private int clickModel(float x0, float y0) {
int xT = (int) x0 - TouchView.width/2;
int yT = (int) y0 - TouchView.height/2;
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);
上一篇:
ClickWheelV3.java
下一篇:
扛起年轻化大旗,哈弗F系玩转行业新风潮