我正在使用画布并且能够完成表盘的图形部分并在表盘上放置一些应用程序(就像旧电话一样),现在我想确定用户触摸了哪个图标。我已经为触摸侦听器实现了这个代码:
public class CanvasDemActivity extends Activity {
Bitmap mLight,mDial,mBrowser,mCamera,mMessage,mDialer,mHome,mLock;
float angle;
ArrayList<Double> mCurrentAngleArray;
ArrayList<PointF> mArray;
ArrayList<Bitmap> mBitmapArray;
PointF p1,p2,p3,p4,p5;
String mTIme;
Date date;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Dialer view = new Dialer(this);
setContentView(view);
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN) {
PointF p = new PointF();
p.x = event.getX(); p.y = event.getY();
for(int i=0;i<5;i++) {
if (p.x >= mArray.get(i).x && p.x < (mArray.get(i).x + mBitmapArray.get(i).getWidth())
&& p.y >= mArray.get(i).y && p.y < (mArray.get(i).y + mBitmapArray.get(i).getHeight())) {
System.out.println("presssed on icon " + i);
break;
}
}
return true;
}
}
public class Dialer extends View{
Context mcontext;
public Dialer(Context context) {
super(context);
mcontext = context;
mLight = BitmapFactory.decodeResource(mcontext.getResources(), R.drawable.light);
mDial = BitmapFactory.decodeResource(mcontext.getResources(), R.drawable.dial);
mBrowser = BitmapFactory.decodeResource(mcontext.getResources(), R.drawable.browser);
mCamera = BitmapFactory.decodeResource(mcontext.getResources(), R.drawable.camera);
mMessage = BitmapFactory.decodeResource(mcontext.getResources(), R.drawable.message);
mDialer = BitmapFactory.decodeResource(mcontext.getResources(), R.drawable.dialer);
mHome = BitmapFactory.decodeResource(mcontext.getResources(), R.drawable.home);
mLock = BitmapFactory.decodeResource(mcontext.getResources(), R.drawable.lock);
mArray = new ArrayList<PointF>();
mBitmapArray = new ArrayList<Bitmap>();
mCurrentAngleArray = new ArrayList<Double>();
mBitmapArray.add(mBrowser);
mBitmapArray.add(mCamera);
mBitmapArray.add(mMessage);
mBitmapArray.add(mDialer);
mBitmapArray.add(mHome);
date = new Date();
p1 = new PointF();
p1.x = 80.0f ; p1.y = 475;
mArray.add(p1);
p2 = new PointF();
p2.x = 120.0f ; p2.y = 320.0f;
mArray.add(p2);
p3 = new PointF();
p3.x = 260 ; p3.y = 275;
mArray.add(p3);
p4 = new PointF();
p4.x = 315 ; p4.y = 390;
mArray.add(p4);
p5 = new PointF();
p5.x = 330 ; p5.y = 460;
mArray.add(p5);
float delta_x,delta_y;
double mCurrentAngle;
for(int i=0; i<5 ; i++) {
delta_x = mArray.get(i).x - 248;
delta_y = mArray.get(i).y - 430;
mCurrentAngle = Math.atan2(delta_y, delta_x);
mCurrentAngleArray.add(mCurrentAngle);
}
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
canvas.drawPaint(paint);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(75);
if(date.getMinutes() > 9)
mTIme = date.getHours() + ":" + date.getMinutes();
else
mTIme = date.getHours() + ":0" + date.getMinutes();
canvas.drawText(mTIme, 140,125, paint);
String dayNames[] = new DateFormatSymbols().getWeekdays();
Calendar date2 = Calendar.getInstance();
String day = dayNames[date2.get(Calendar.DAY_OF_WEEK)];
java.util.Date d = new java.util.Date(Calendar.getInstance().getTimeInMillis());
String properformat = day+", "+ date.getDate() + " " +new SimpleDateFormat("MMMM").format(d);
paint.setTextSize(25);
canvas.drawText(properformat, 125, 175, paint);
TelephonyManager telephonyManager =((TelephonyManager) mcontext.getSystemService(Context.TELEPHONY_SERVICE));
String operatorName = telephonyManager.getNetworkOperatorName();
if(operatorName.length() == 0 )
operatorName = "Insert Sim Card";
paint.setTextSize(20);
canvas.drawText(operatorName, 175, 210, paint);
canvas.drawBitmap(mDial, canvas.getWidth()/10, canvas.getHeight()/4, null);
canvas.drawBitmap(mLock, canvas.getWidth()/10, canvas.getHeight()/4, null);
for(int i=0;i<5;i++) {
double X = 120 * Math.cos(mCurrentAngleArray.get(i) + angle);
double Y = 120 * Math.sin(mCurrentAngleArray.get(i) + angle);
canvas.drawBitmap(mBitmapArray.get(i),210 + (float) X, 395+ (float)Y, null);
}
angle = angle + 0.01f;
canvas.drawBitmap(mLight, canvas.getWidth()/9,canvas.getHeight()/4f, null);
invalidate();
}
}
}
其中p.x和p.y是用户触摸的坐标,mArray是我放置位图的坐标,mBitmapArray是我存储位图的arraylist。
问题是即使我已正确编写代码,当我按下浏览器和拨号器图标时,我无法进入“if”状态。
我在一个示例应用程序中测试了相同的代码,它工作正常
你能告诉我这是什么问题吗?