本文介绍了误差在图像URL的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林新到Android ..

IM具有图像URL面临网格布局的一个问题。

我的code ..

 公共类的行为扩展活动{
    静态字符串uri1 =htt​​ps://i3.ytimg.com/vi/bQaWsVQSLdY/default.jpg;
    静态字符串uri2 =htt​​ps://i4.ytimg.com/vi/cJQCniWQdno/mqdefault.jpg;
    静态字符串uri3 =htt​​ps://i1.ytimg.com/vi/D8dA4pE5hEY/mqdefault.jpg;
    公共静态的String [] =网址{uri1,uri2,uri3};
    公共位图位图;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        GridView控件GRD =(GridView控件)findViewById(R.id.gridView1);
        grd.setAdapter(新ImageAdapter(本));
        grd.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>母公司,视图V,INT POS机,
                    长ID){
                Toast.makeText(getBaseContext(),PIC+(POS + 1)+选择,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }    公共类ImageAdapter延伸BaseAdapter {
        私人上下文的背景下;
        私人诠释itemBackground;        ImageAdapter(上下文C){
            上下文= C;
            TypedArray A = obtainStyledAttributes(R.styleable.Gallery1);
            itemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground,0);
            a.recycle();
        }        公众诠释的getCount(){
            返回urls.length;
        }        公共对象的getItem(INT POS){
            返回POS;
        }        众长getItemId(INT POS){
            返回POS;
        }        公共查看getView(INT POS,查看简历的ViewGroup以及VG){
            位图位图= DownloadImage(网址[POS]);
            ImageView的ImageView的=新ImageView的(上下文);
            imageview.setImageBitmap(位图);
            返回简历;
        }        私人位图DownloadImage(字符串URL){
            最后弦乐URL1 =网址;
            新的Thread(){
                公共无效的run(){
                    在的InputStream = NULL;
                    消息味精= Message.obtain();
                    msg.what = 1;
                    尝试{
                        在= OpenHttpConnection(URL1);
                        位图位图= BitmapFactory.de codeStream(中);
                        束B =新包();
                        b.putParcelable(位图,位图);
                        msg.setData(二);
                        附寄();
                    }赶上(IOException异常E1){
                        // TODO自动生成catch块
                        e1.printStackTrace();
                    }
                }
            }。开始();
            返回位图;
        }        私人的InputStream OpenHttpConnection(字符串urlString)
                抛出IOException
            在的InputStream = NULL;
            INT响应= -1;
            网址URL =新的URL(urlString);
            康涅狄格州的URLConnection = url.openConnection();
            如果(!(康涅狄格州的instanceof HttpURLConnection类))
                抛出新IOException异常(不是一个HTTP连接);
            尝试{
                HttpURLConnection的httpConn =(HttpURLConnection类)美国康涅狄格州;
                httpConn.setAllowUserInteraction(假);
                httpConn.setInstanceFollowRedirects(真);
                httpConn.setRequestMethod(GET);
                httpConn.connect();
                响应= httpConn.getResponse code();
                如果(响应== HttpURLConnection.HTTP_OK){
                    在= httpConn.getInputStream();
                }
            }赶上(例外前){
                抛出新IOException异常(错误连接);
            }
            返回;
        }
    }
}

logcat的

解决方案

I recommend to you to read this : http://xjaphx.wordpress.com/2011/06/12/custom-grid-view-of-applications/

这篇关于误差在图像URL的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:27