Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

java - Android how to add delay into method called in draw of SurfaceView

Hello I want to be able to add a method that reduce the size of my sprite e.g. 10px every 1s until it reaches small size but app freezes. I am trying like this:

public class GameSurface extends SurfaceView implements SurfaceHolder.Callback {

public ChibiCharacter chibi1;
private final Handler handlerReduceSizeOfChibi = new Handler();

(...) 

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    this.canvas = canvas;

    (...)

    if(chibiNode==17)
        reduceSizeOfChibi();
}

public void reduceSizeOfChibi() {
    int defaultChibiWidth = chibi1.getWidth();
    int defaultChibiHeight = chibi1.getHeight();
    int chibiXWhenCall  = chibi1.getX();
    int chibiYWhenCall  = chibi1.getY();
    Bitmap defaultChibiBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.chibi1);
    Runnable runnable = new Runnable() {
        public void run() {
            for(int i=0; i<50; i=i+10) {
                chibiBitmap = Bitmap.createScaledBitmap(defaultChibiBitmap, defaultChibiWidth-i,defaultChibiHeight, false);
                chibi1 = null;
                chibi1 = new ChibiCharacter(GameSurface.this, chibiBitmap, chibiXWhenCall, chibiYWhenCall);
                handlerReduceSizeOfChibi.postDelayed(this, 1000);
            }
        }
    };
    runnable.run();
}
}
question from:https://stackoverflow.com/questions/65922193/android-how-to-add-delay-into-method-called-in-draw-of-surfaceview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, try change

private final Handler handlerReduceSizeOfChibi = new Handler();

to

private final Handler handlerReduceSizeOfChibi = new Handler(Looper.getMainLooper());

Second, the way you using handler was wrong. That handler basically will call your Runnable every ~1 second for 5 times and that causes infinite loop. This is probably what you wanted.

public void reduceSizeOfChibi() {
    int defaultChibiWidth = chibi1.getWidth();
    int defaultChibiHeight = chibi1.getHeight();
    int chibiXWhenCall  = chibi1.getX();
    int chibiYWhenCall  = chibi1.getY();
    Bitmap defaultChibiBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.chibi1);
   for(int i=0; i<50; i+=10) {
    handlerReduceSizeOfChibi.postDelayed(new Runnable() {
        public void run() {
            chibiBitmap = Bitmap.createScaledBitmap(defaultChibiBitmap, defaultChibiWidth - i, defaultChibiHeight, false);
            chibi1 = new ChibiCharacter(GameSurface.this, chibiBitmap, chibiXWhenCall, chibiYWhenCall);
            GameSurface.this.invalidate(); //Update ChibiCharacter every 1 second.
        }
    }, 1000);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...