标签:
下载图片
private ImageView image_main; private Handler handler =new Handler() { @Override public void handleMessage(Message msg) { //处理其他线程传递过来的消息。 Bitmap bitmap = (Bitmap) msg.obj; image_main.setImageBitmap(bitmap); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image_main = (ImageView) findViewById(R.id.image_main); //开启一个线程下载图片。 startThead(); } private void startThead() { new Thread(new Runnable() { @Override public void run() { //执行在工作线程中。 String path = "http://img4.duitang.com/uploads/item/201306/20/20130620221932_FVnZK.thumb.600_0.jpeg"; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); ; int code = conn.getResponseCode(); if (code == 200) { InputStream is = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); //将图片设置给ImageView. //发送消息 Message msg = new Message(); //arg1,arg2--放的都是整数。 what---类型。也是整数。obj---放的是任意的对象 msg.obj = bitmap; handler.sendMessage(msg);//发送消息。 } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); }
广告轮播
private ViewPager vg_main; private List<ImageView> list; private int[] imags_id = {R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d,R.mipmap.e}; int position = 0; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { //定位 vg_main.setCurrentItem(++position,true); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vg_main = (ViewPager) findViewById(R.id.viewpager); initList(); MyPagerAdapter adapter = new MyPagerAdapter(); vg_main.setAdapter(adapter); new Thread(new Runnable() { @Override public void run() { //发送消息到主线程。 while(true) { handler.sendEmptyMessage(1); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } private void initList() { list = new ArrayList<ImageView>(); for(int i=0;i<imags_id.length;i++){ ImageView img = new ImageView(this); img.setImageResource(imags_id[i]); list.add(img); } } class MyPagerAdapter extends PagerAdapter{ @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position%list.size())); return list.get(position%list.size()); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(list.get(position%list.size())); } }
UI线程和工作线程互相传递
private TextView tv_msg; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { String str = (String) msg.obj; tv_msg.setText(str); } }; private Handler handler_work; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_msg = (TextView) findViewById(R.id.tv_message); } public void click(View view){ switch (view.getId()){ case R.id.btn_main: //Ui线程向工作线程发送数据。 Message msg = handler_work.obtainMessage(); msg.obj = "我是main线程发送过来的消息"; handler_work.sendMessage(msg); break; case R.id.btn_workThread: new Thread(new Runnable() { @Override public void run() { Message msg = handler.obtainMessage(); msg.obj = "我是工作线程发送过来的消息"; handler.sendMessage(msg); //创建handler handler_work = new Handler(){ @Override public void handleMessage(Message msg) { String text = (String) msg.obj; Log.i("work",text); } }; } }).start(); break; } }
倒计时
private Button btn_start; private TextView tv_time,tv_stop; private int total = 10; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { //设置 int timer = msg.arg1; tv_time.setText(msg.arg1 + ""); if(timer==0){ btn_start.setEnabled(true); tv_stop.setVisibility(View.VISIBLE); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { btn_start = (Button) findViewById(R.id.btn_start); tv_time = (TextView) findViewById(R.id.tv_time); tv_stop = (TextView) findViewById(R.id.tv_stop); } public void click(View view){ //按钮不再可用。 btn_start.setEnabled(false); tv_stop.setVisibility(View.INVISIBLE); //改变tv_time时间。 new Thread(new Runnable() { @Override public void run() { int total = 10; //马上设置。 Message msg = handler.obtainMessage(); msg.arg1 = total; handler.sendMessage(msg); for(int i=9;i>=0;i--){ Message msg2 = handler.obtainMessage(); msg2.arg1 = i; handler.sendMessage(msg2); Log.i("Main",msg2.arg1+""); // 睡眠。 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); }
标签:
原文地址:http://www.cnblogs.com/anni-qianqian/p/5383699.html