Refresh view problems


Last Updated:

  1. tibo-k3

    tibo-k3 New Member This Topic's Starter

    Joined:
    Dec 7, 2009
    Messages:
    2
    Likes Received:
    0
    Hello everyone!

    I am actually programming a video game with android, and I a have a little problem. I am using a timer to increase two integers, which are the position of my picture on a view. However, when I am using this timer, the integers are increased but the view does not refresh, even if I use the method myview.Invalidate().

    This my code:

    package SpaceInvader.pack;

    import java.util.TimerTask;

    import android.graphics.Canvas;

    public class timertask extends TimerTask{

    int abscisse;
    int ordonnee;
    MyView m;
    Canvas c;

    public timertask(int x, int y, MyView myview, Canvas canvas ) {
    abscisse=x;
    ordonnee=y;
    m = myview;
    c=canvas;
    }

    @Override
    public void run() {
    c.drawBitmap(m.bLogo, abscisse, ordonnee, m.mPaint);
    abscisse++;
    ordonnee++;

    m.invalidate();
    //m.refreshDrawableState();
    //m.postInvalidate();
    //m.forceLayout();
    //m.loadLogo();
    //m.requestLayout();
    //m.setFocusable(true);
    }


    }

    Thanks for your help ;)!
     

    Advertisement
  2. sahil.hariram

    sahil.hariram New Member

    Joined:
    May 25, 2010
    Messages:
    2
    Likes Received:
    0
    I am also facing the same problem what could be the possible solution of this problem?
     
  3. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    I think the problem might be that you created the GUI elements in one thread but are updating them in another thread. I believe you can only update GUI elements from the thread that created them. One way around that would be for your TimerTask to communicate back to the main GUI thread, asking it to refresh them.

    Mark
     
  4. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    tibo-k3,

    I think markb is right - you should try sending a message back to your main thread to update/draw the widgets on screen. Doing that sort of thing from another thread usually causes problems like you're describing. If you need help on how to do that, then let us know and someone will help you out.

    Also, as a tip, please enclose any source code inside the "code" tags in your post. It makes it much easier to read. E.g.:
    Code (Text):
    1. public static final String USING_CODE_TAGS = "A Good Idea";
    2. TextView important = (TextView)findViewById(R.id.important);
    3. important.setText("Using code tags is " + USING_CODE_TAGS);
    4.  
     
  5. fabiocberg

    fabiocberg Active Member

    Joined:
    Apr 7, 2010
    Messages:
    27
    Likes Received:
    1
    Try something like this:
    Code (Text):
    1. public class MyGame extends Activity {
    2.     int abscisse;
    3.     int ordonnee;
    4.     Paint paint = new Paint();
    5.     Bitmap bLogo;
    6.  
    7.     class MyView extends SurfaceView {
    8.  
    9.         public MyView(Context context, AttributeSet attrs) {
    10.             super(context, attrs);
    11.             loop();
    12.         }
    13.  
    14.         private void loop() {
    15.             new Thread(new Runnable() {
    16.  
    17.                 @Override
    18.                 public void run() {
    19.                     while (true) {
    20.                         Canvas c = null;
    21.                         SurfaceHolder holder = getHolder();
    22.                         try {
    23.                             c = holder.lockCanvas(null);
    24.                             if (c != null) {
    25.                                 synchronized (holder) {
    26.                                     doUpdate();
    27.                                     doDraw(c);
    28.                                 }
    29.                             }
    30.                         } catch (Exception e) {
    31.                             e.printStackTrace();
    32.                         } finally {
    33.                             if (c != null) {
    34.                                 holder.unlockCanvasAndPost(c);
    35.                             }
    36.                         }
    37.                     }
    38.                 }
    39.             }).start();
    40.         }
    41.     }
    42.  
    43.     public void doUpdate() {
    44.         abscisse++;
    45.         ordonnee++;
    46.     }
    47.  
    48.     public void doDraw(Canvas c) {
    49.         c.drawBitmap(bLogo, abscisse, ordonnee, paint);
    50.     }
    51.  
    52.     @Override
    53.     public void onCreate(Bundle savedInstanceState) {
    54.         super.onCreate(savedInstanceState);
    55.         MyView my = new MyView(getApplicationContext(), null);
    56.         setContentView(my);
    57.         // load bLogo here
    58.     }
    59. }
    This is a basic code to you program a game. You can improve it a lot.
     

Share This Page

Loading...