Skip to content Skip to sidebar Skip to footer

How Do I Use A While Loop With An Ontouch Function? (android)

Well, I have been looking up answers, but none have worked for me. I have a button that when it is HELD DOWN, is suppose to continue to move an image across the screen. But for som

Solution 1:

Firstly, you are making a UI thread sleep that's why your UI is getting freeze.Here, i have created a async task where i am sleeping background thread and running a UI update on main thread as you can see below in doInBackground method.Here is your solution -:

publicclassMainActivityextendsActivity {

    private ImageView leftImageButton;
    privatebooleanleftButtonDown=false;
    private ImageView knight;
    privateAsyncTaskasyncTask=null;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        leftImageButton = (ImageView) findViewById(R.id.imageView2);
        knight = (ImageView) findViewById(R.id.imageView1);
        leftImageButton.setOnTouchListener(newLeftImageListener());
    }

    publicvoidstartTask() {
        asyncTask = newAsyncTask<Void, Void, Void>() {

            @Overrideprotected Void doInBackground(Void... params) {
                if (leftButtonDown) {
                    while (leftButtonDown) {
                        try {
                            Thread.sleep(10);
                            runOnUiThread(newRunnable() {

                                @Overridepublicvoidrun() {
                                    try {
                                        moveLeft(5);

                                    } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }

                                }
                            });

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                returnnull;
            }

        }.execute();
    }

    publicvoidmoveLeft(int speed)throws InterruptedException {
        knight.setLeft(knight.getLeft() - speed);
    }

    publicclassLeftImageListenerimplementsOnTouchListener {

        publicLeftImageListener() {
            // TODO Auto-generated constructor stub
        }

        @OverridepublicbooleanonTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                leftButtonDown = true;
                startTask();
                break;
            case MotionEvent.ACTION_UP:
                leftButtonDown = false;
                break;
            default:
                break;
            }

            returntrue;
        }

    }

}

Solution 2:

create a second thread was right but you should fill it with some action cause the thread you`ve created is an empty one :) and you dont need to call run after starting your thread.

Thx Abhishek Birdawade for doing the code work for me :)

Post a Comment for "How Do I Use A While Loop With An Ontouch Function? (android)"