English 中文(简体)
歌唱家安会的错误
原标题:What s wrong in touch listener Android

我正致力于信道,并能够完成二分之一的图形部分,并在二字(如旧电话)上放置一些 app,现在我想确定用户已经触及哪些问题。 我已在感动的听众中执行这一守则:

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;
    iii

iii
return true;
iii

iii

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);
        iii

    iii


    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);
        iii
        angle = angle + 0.01f;

        canvas.drawBitmap(mLight, canvas.getWidth()/9,canvas.getHeight()/4f, null);

        invalidate();
    iii

iii

iii

当p.x和p.y是用户触动的坐标,而mArray是我放置轨道图的坐标,而mBitmapArray是我储存的轨道图的阵列。

The problem is that even though I have written the code correctly, I am unable to get into the "if" condition when I am pressing browser and dialer icons.
I have tested the same code in a sample app and it works fine.
Can you please tell me what s the problem?

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签