首页 > 代码库 > Thread与Handler

Thread与Handler

     好吧,废话就少说了。开始觉着这个线程真的就不费事,后来,到现在。。。嘿嘿,要是牵涉到一些活跃的赋值以及得到东西,为了线程的安全,好像我们都离不开它哦!

今天,不是要做一个可以从网上动态获取数据的应用嘛,所以,这里在获取网络图片的时候,我遇到一个问题:

    first:网络图片都是URl的,而我等待赋值的这些控件除了ImageView还有Button以及布局。。。。imageview以及button还好说咯可是布局呢?

   也就是说这里必须要将Url转换成Drawable类型的。

   而且是获得多张,于是,我是这样打算的:

        先定义一个数组,然后将这些url全部写出来,然后将他转换成drawable类型的,然后再利用handler去更新界面。。。。不错不错。。。

    其实网上有很多这样关于这两者的代码,可是就是木有获取一连串的字符(url)然后来进行转换的。所以,就记下来了,方便自己,也方便大家嘛。。。

 

  

public class MyThread extends Thread {
        String[] str = {
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appStoreLogoImg,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appStoreBackImg,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appStoreListimg,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appStoreListfocusimg,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appListonfocusimg,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appListFloat,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appInfoFloat,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appBoxBorderColor,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appBoxBgColor,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appBorFocusColor,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appBiFocuscolor,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appLeft,
                WelcomeActivity.getstyleResponse.items.appStoreTheme.appRight };

        @Override
        public void run() {
            for (int i = 0; i < str.length; i++) {

                try {
                    URL url = new URL(str[i]);
                    try {
                        bitmap = BitmapFactory.decodeStream(url.openStream());

                        bd = new BitmapDrawable(bitmap);
                      
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Message message = new Message();
                message.obj = bd;
                System.out.println("this image message is====>" + bd);
                message.what = i;
                System.out.println("this image message   what  is====>" + i);
                mhand.sendMessage(message);

            }
            msgHandler.sendEmptyMessageDelayed(0, 1 * 1000);
        }

    }

    class mHandler extends Handler {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 1:
                if (acache.getAsDrawable("1") == null) {
                    layoutDraw = ((BitmapDrawable) msg.obj);
                    // mlLayout.setBackground((BitmapDrawable) msg.obj);
                    System.out
                            .println("RRRRRRRRRRRthis layoutdraw is==========>"
                                    + layoutDraw);
                } else {
                    layoutDraw = (BitmapDrawable) acache.getAsDrawable("1");
                    // mlLayout.setBackground(acache.getAsDrawable("1"));
                    System.out
                            .println("RRRRRRRRRRRthis layoutdraw is==========>"
                                    + layoutDraw);
                }

                break;
            case 2:
                isNomal = (BitmapDrawable) msg.obj;
                System.out.println("11111111111111 nomale is ====>" + isNomal);
                // MyArrayAdapter.getImg(bd, 2);
                break;
            case 3:
                isfouce = (BitmapDrawable) msg.obj;
                System.out.println("222222222222 fouce is ====>" + isfouce);

                // MyArrayAdapter.getImg(bd, 3);
                break;
            case 4:
                isSelected = (BitmapDrawable) msg.obj;
                System.out.println("3333333333333333 selected is ====>"
                        + isSelected);
                // MyArrayAdapter.getImg(bd, 4);
                break;
            case 5:

                break;
            case 6:
                if (acache.getAsDrawable("6") == null) {

                    DetailDraw = ((BitmapDrawable) msg.obj);
                    // detail.setBackground((BitmapDrawable) msg.obj);
                    // detailInfo.setBackground((BitmapDrawable) msg.obj);
                    System.out
                            .println("66666666666666666 is the detail is is ====>"
                                    + DetailDraw);
                } else {

                    DetailDraw = (BitmapDrawable) acache.getAsDrawable("6");
                    // detail.setBackground((BitmapDrawable) acache
                    // .getAsDrawable("6"));
                    // detailInfo.setBackground((BitmapDrawable) acache
                    // .getAsDrawable("6"));
                    System.out.println("66666666666666666666 else is ====>"
                            + DetailDraw);
                }

                break;
            // color of the image
            case 8:
                break;
            // color of the image
            case 10:
                break;
            case 11:
                DetailInfoFragment.getImg(bd, 11);
                break;
            case 12:
                DetailInfoFragment.getImg(bd, 12);
                break;

            }
        }
    };

 

这里面的代码 我是完全没做修改了!嘿嘿,看懂了就懂了,其实我这个循环可能会很累赘,大家有什么好的,提提咯!期待。。。