开发者

using handler in android to create series of png's

开发者 https://www.devze.com 2023-03-16 00:21 出处:网络
I am trying to put an image on the screen and change it every five seconds.This is not in an Activity class. Sorry if is a stupid question.This is a huge learning curve for me.

I am trying to put an image on the screen and change it every five seconds. This is not in an Activity class. Sorry if is a stupid question. This is a huge learning curve for me.

public class Panel extends SurfaceView implements SurfaceHolder.Callback {
  private Handler mHandler = new Handler();
  //stuff

  public void doDraw(Canvas canvas) {
        int counter = 0;
        canvas.drawColor(Color.BLACK);
        dot1.doDraw(getResources(), canvas, counter);
            mHandler.removeCallbacks(panelDraw);
                mHandler.postDelayed(panelDraw, 5000);
}

private Runnable panelDraw = new Runnable() {
       public void run() {

       }
};

(Panel->)mPanel.doDraw(canvas) in another class calls the doDraw listed in Panel. This is in a loop.

As far as I can tell, the first image isn't stopping for five seconds, and due to other code the cat pic flashes all over the screen. Can I get away with putting nothing in run()? I wanted to put the dot1.doDraw(getResources(), canvas, counter) one in there, but I couldn't/didn't think I could give void run() parameters or Runnable panelDraw resources, canvas, counter parameters.

Any help is appreciated. Let me know if I need to give more code.


A little more about my game: Kitten photos should appear one at random locations on the screen. If one does not click on the current kitty image with an allotted time, the game ends. These pictures last < 1 sec in the end (right now it's more for testing purposes). If you don't click the kitty in that time span, the game ends.

I heard that if you need to do a lot of drawing, you're better off using SurfaceView like in the tutorial I followed very closely: http://www.droidnova.com/2d-tutorial-series-part-v,848.html My Activity class (Scene1) has setContentView(new Panel(this)), so I don't know how to put stuff in the Activity class when it all goes in the Panel class. Like where do I put the ImageView code if the Activity thread doesn't really do much and hands it all off to Panel? Also if I have a ViewThread (public class ViewThread extends Thread) that handles the running:

public void run() {
    Canvas canvas = null;
    while (mRun1) {
     do {
        canvas = mHolder.lockCanvas();
            if (canvas != null) {
               mPanel.doDraw(canvas);
               mRun1 = true;
               mRun1 = mPanel.get_run();
               mHolder.unlockCanvasAndPost(canvas);
            }
    } while (mRun1 == true);

The dot class has something like:

public void firstRing(Resources res, Canvas canvas, int counter){
    Random rand = new Random();
        switch(counter) {
            case 0:  mBitmap = BitmapFactory.decodeResource(res,     
                         R.drawable.cat1_m);
                break;
            case 1:  mBitmap = BitmapFactory.decodeResource(res, 
                         R.drawable.cat2_m);
                break;
            case 2:  mBitmap = BitmapFactory.decodeResource(res,                   
                         R.drawable.cat2_m);
      开发者_运维问答          break;
            case 3:  mBitmap = BitmapFactory.decodeResource(res,   
                         R.drawable.cat2_m);
                break;
            case 4:  mBitmap = BitmapFactory.decodeResource(res,                          
                         R.drawable.cat2_m);
                            break;
            case 5:  mBitmap = BitmapFactory.decodeResource(res, 
                         R.drawable.cat1_m);
                break;
        }
        mX = rand.nextInt((int)(Panel.mWidth - mBitmap.getWidth()));
        mY = rand.nextInt((int)(Panel.mHeight - mBitmap.getHeight()));
    canvas.drawBitmap(mBitmap, mX, mY, null);

and

 public void doDraw(Resources res, Canvas canvas, int counter) {
      firstRing(res, canvas, counter);
}

Thanks for the info, Barry, although I am a little confused. Do I try ImageView or do I stick with Handler(), although I don't know how to use it and it's not working now? Do I need something in public void run()?


This code should work better than my last answer, but I haven't compiled or tested it. Even if it doesn't work it will point you in the right direction. I assume you have an Activity subclass somewhere so just copy my code into your activity. Your layout should have an <ImageView> tag in it somewhere.

The code below creates a Handler in Activity.onCreate(), which is run in the UI thread, guaranteeing all calls to the Handler will also be run in the UI thread. It then calls showNextKittyImage() to display the first image. showNextKittyImage() sets the Drawable resource id for the ImageView, and then calls Handler.sendEmptyMessageDelayed() to call the handler 5 seconds later. The Handler calls showNextKittyImage() and the cycle repeats.

An ImageView is still preferable to a SurfaceView because it is designed to display images. All you need to do is pass it the Drawable resource id instead of messing with Bitmaps. If you really want or need to do it with a SurfaceView then I cannot help you.

This code will also not make the kittens appear at random locations. Get it working in one location first, then add code to move the ImageView around the screen randomly. If you need help at that point I strongly recommend you post a new question as you've asked A LOT for one posting.

Good luck,

Barry

public class KittyActivity extends Activity {

    Handler mHandler;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Inflate your view
        setContentView(R.layout.YOUR_LAYOUT);

        // Create a handler ON THE UI THREAD
        mHandler = new Handler(Message result) {
            public void handleMessage(Message result) {
                showNextKittyImage();
            }
        };

        // Show the first kitty
        showNextKittyImage();
    }

    public void showNextKittyImage() {
        int kitty = getNextKitty();
        ImageView iv = findViewById(R.id.YOUR_IMAGE_VIEW_ID);
        iv.setImageResource(kitty);

        // OPTIONAL: Move the ImageView to a new random location

        mHandler.sendEmptyMessageDelayed(0, 5000);
    }


    private int getNextKitty() {
        // Your code to get the next kitty drawable id
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消