I am new at this. Trying to make my first game. I need to time delay transparent pngs to my canvas but cant seem to get it right.
public LessonsMain(Context context) {
super(context);
// TODO Auto-generated constructor stub
mFujiSensei = getResources().getDrawable(R.drawable.old_man_fuji);
mFujiSensei.setBounds(0,0,mFujiSensei.getIntrinsicWidth(),mFujiSensei.getIntrinsicHeight());
mBackground = getResources().getDrawable(R.drawable.lessons_background);
mBackground.setBounds(0,0,mBackground.getIntrinsicWidth(),mBackground.getIntrinsicHeight());
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
mBackground.draw(canvas);
TimerTask task = new TimerTask() {
public void run(){
mFujiSensei.draw(canvas);
}
}; timer.schedule(task, 3000);
}
Sorry still dont get it. I tried putting it in a different thread but it still doesnt work.
public class LessonsMain extends View{
Drawable mBackground;
Drawable mFujiSensei;
Timer timer;
Handler handler;
Runnable runnable;
public LessonsMain(Context context) {
super(context);
// TODO Auto-generated constructor stub
mFujiSensei = getResources().getDrawable(R.drawable.old_man_fuji);
mFujiSensei.setBounds(0,0,mFujiSensei.getIntrinsicWidth(),mFujiSensei.getIntrinsicHeight());
mBackground = getResources().getDrawable(R.drawable.lessons_background);
mBackground.setBounds(0,0,mBackground.getIntrinsicWidth(),mBackground.getIntrinsicHeight());
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
mBackground.draw(canvas);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
TimerTask task = new TimerTask() {
public void run(){
mFujiSensei.draw(canvas);
}
}; timer.schedule(task, 3000);
}
}).start();
}
}
The problem here is that when the timerTask executes, it is no longer on the UI thread so it can’t update the view. What you’ll have to do instead ispost to a Handler so the draw can happen on the UI thread. Check out this page for an in-depth explanation of updating the UI from a timer.