1

I want to move the image on screen using thread I have tried but its not working fine due to improper code of thread. I am able to get the image and i want to move the image on screen In this code i want to move ovalshaped image and i dont want that it must effect the jerry image but the problem is thread is also implementing over it The margenMaxX, margenxy are the maximum width and height

    public class AnimatedView extends ImageView {
            public AnimatedView(Context context) {
                super(context);

Resources res = context.getResources();
            drawable = res.getDrawable(R.drawable.jerry);
        mDrawable =  new ShapeDrawable(new OvalShape());
                mDrawable.getPaint().setColor(0xffffAC23);
    } 
     protected void onDraw(final Canvas cc) {
            final Context context = null;
drawable.setBounds(x , y , x + width, y + height);
            drawable.draw(cc);
            invalidate();
                Thread thread = new Thread()
                            {
                                @Override
                                public void run() {
                                    try {
                                        while(i<=margenMaxX && j<=margenmaxy) {
                                            sleep(100);
                                           // context.runOnUiThread(new Runnable() {
                                            //handler.post(r);
                                            runOnUiThread(new Runnable() {

                                         @Override
                                         public void run() {
                                                 mDrawable.setBounds(i, j ,i+ width, i+ height);
                                                 mDrawable.draw(cc);

                                        }

                                    });
                                            //invalidate();
                                            i=i+10;
                                            j=j+10;
                                        }
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };

                            thread.start();

            }
nawaab saab
  • 1,892
  • 2
  • 20
  • 36

1 Answers1

0

You have to update UI within UI thread. Try this, its not tested but it should work

protected void onDraw(final Canvas cc) {
    Thread thread = new Thread()
                {
                    @Override
                    public void run() {
                        try {
                            while(i<=margenMaxX && j<=margenmaxy) {
                                sleep(100);
                                //handler.post(r);
                               context.runOnUiThread(new Runnable() {

                             @Override
                             public void run() {
                                     mDrawable.setBounds(i, j ,i+ width, i+ height);
                                     mDrawable.draw(cc);

                            }

                        });
                                //invalidate();
                                i=i+10;
                                j=j+10;
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };

                thread.start();

}}

see this for details

Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30
  • thanks but its giving error in line context.runOnUiThread(new Runnable() protected void onDraw(final Canvas cc) { final Context context = null; Thread thread = new Thread() { @Override public void run() { try { while(i<=margenMaxX && j<=margenmaxy) { sleep(100); context.runOnUiThread(new Runnable() { – nawaab saab Mar 24 '14 at 07:50
  • Its saying add cast to context than i did this ((Activity) context).runOnUiThread(new Runnable() but its showing exception in this.....03-24 13:16:27.070: E/AndroidRuntime(10277): at com.game2.MainActivity$AnimatedView$1.run(MainActivity.java:218) – nawaab saab Mar 24 '14 at 07:53
  • @Abhi whats on line number 219 in MainActivity.java ? – Mukesh Kumar Singh Mar 24 '14 at 08:28
  • thanks for your help the ball is moving over the screen but it is also making the another image to sleep and i want ball to move slowly i increased time but its not working – nawaab saab Mar 24 '14 at 09:36