【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-ClickWheelV3.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package org.chapter10;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Path;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class ClickWheelV3 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 = "";
MediaPlayer mMediaPlayer = null;
private int [] sounds = {R.raw.o, R.raw.k, R.raw.menu,
R.raw.back, R.raw.fast, R.raw.play, R.raw.still};
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);
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(sounds[OK_BUTTON-1]);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
playSound(sounds[OK_BUTTON]);
break;
case M_BUTTON:
playSound(sounds[M_BUTTON]);
break;
case B_BUTTON:
playSound(sounds[B_BUTTON]);
break;
case F_BUTTON:
playSound(sounds[F_BUTTON]);
break;
case P_BUTTON:
if(bPP==false) {
playSound(sounds[P_BUTTON]);
bPP=true;
}
else{
playSound(sounds[P_BUTTON+1]);
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);
updat