图片库动态 [英] image gallery dynamically

查看:102
本文介绍了图片库动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想显示的图片库动态android系统中。(即)不应该从路解析度图像/绘..

我怎么能这样做呢?


解决方案

 这是code初始化图库视图..
      最后的画廊G =(图库论坛)findViewById(R.id.gallery);
      g.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图父母,视图V,INT位置,长的id){
          // Toast.makeText(CurrentActivity.this,+位置,Toast.LENGTH_SHORT).show();        }
      });
      g.setBackgroundColor(Color.LTGRAY);
      g.setAdapter(新ImageAdapter(getApplicationContext()));

现在的图像可以从URL中获取。你只需要告诉图像适配器,genarating画廊的每一个项目的观从网址提取图像。

下面的函数是从任何URL抓取图像并返回绘制
可以设置为图像视图

可绘制drawable_from_url(URL字符串,字符串src_name)
  抛出java.net.MalformedURLException,java.io.IOException异常

{

 返回Drawable.createFromStream(((java.io.InputStream中的)新的java.net.URL(URL).getContent()),src_name);}

请这个ImageAdapter类内,检查code ..

与图像的URL填充的矢量数据

类ImageAdapter延伸BaseAdapter {
        INT mGalleryItemBackground;
        私人语境mContext;

 公共ImageAdapter(上下文C){
        mContext = C;
        TypedArray A = obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground,0);
        a.recycle();
    }    公众诠释的getCount(){        返回data.size();
    }    公共对象的getItem(INT位置){
        返回的位置;
    }    众长getItemId(INT位置){
        返回的位置;
    }    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        ImageView的I =新ImageView的(mContext);        i.setImageDrawable(drawable_from_url(data.get(位置),SRC));        i.setLayoutParams(新Gallery.LayoutParams(300,180));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);        返回我;
    }
}

希望这将帮助:)

I just wanna display image gallery dynamically in android.(i.e) shouldn't get image from path res/drawable..

How could I do this?

解决方案

    This is the code to initialize Gallery view .. 


      final Gallery g = (Gallery) findViewById(R.id.gallery);
      g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
          // Toast.makeText(CurrentActivity.this, " " + position, Toast.LENGTH_SHORT).show();

        }
      });
      g.setBackgroundColor(Color.LTGRAY);
      g.setAdapter(new ImageAdapter(getApplicationContext()));

Now Images can be fetched from the URL . You just need to tell the image adapter to fetch image from URL while genarating the View of every item of gallery.

The following function is for fetching the image from any URL and returning the drawable that can be set as image view

Drawable drawable_from_url(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException

{

  return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name);

}    

Make this ImageAdapter class as inner , check the code..

Fill A Vector data with URL of images

class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        a.recycle();
    }

    public int getCount() {

        return   data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageDrawable(drawable_from_url(data.get(position), "src"));

        i.setLayoutParams(new Gallery.LayoutParams(300, 180));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);

        return i;
    }
}

Hope this will help :)

这篇关于图片库动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆