Android timer implements picture transformation
In Android, to make ui updates every second, you need to use the combination of timer, handler and message. If you do not use handler, you cannot achieve the effect of updating ui. My understanding is that there is a queue problem in handler, which can ensure that there is no blockage.
The code is as follows:
import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { private LinearLayout linearlayout; private ImageView main_imageview; private int i = 0; Timer timer = new Timer(); private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { ("@@@", i + ""); //index=; if (i > 6) { i = 0; } else { switch (i) { case 1: main_imageview.setImageResource(.loader_frame_1); break; case 2: main_imageview.setImageResource(.loader_frame_2); break; case 3: main_imageview.setImageResource(.loader_frame_3); break; case 4: main_imageview.setImageResource(.loader_frame_4); break; case 5: main_imageview.setImageResource(.loader_frame_5); break; case 6: main_imageview.setImageResource(.loader_frame_6); break; default: break; } (); } (msg); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); initView(); } public void initView() { linearlayout = (LinearLayout) findViewById(.background_main); main_imageview = (ImageView) findViewById(.main_imageview); (new TimerTask() { @Override public void run() { // TODO Auto-generated method stub i++; Message mesasge = new Message(); = i; (mesasge); } }, 0, 500); } @Override protected void onDestroy() { // TODO Auto-generated method stub (); (); } }
There are two points to note in this code:
First: After updating the picture, the entire layout needs to be refreshed, ();
Second: After using up the timer timer, the timer timer must be destroyed while the activity is killed.
The above is the application of Android timer. If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!