如何通过URL显示图像范围而不是可绘制

时间:2017-05-02 10:35:37

标签: android imageurl

我正在尝试创建一个壁纸应用程序,以显示基于可绘制文件夹的一系列图像。如何从URL获取此图像,即通过url获取图像?

public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public Integer[] mThumbIds = {
        R.drawable.wallpaper_1,
        R.drawable.wallpaper_2,
        R.drawable.wallpaper_3,
        R.drawable.wallpaper_4,
        R.drawable.wallpaper_5,
        R.drawable.wallpaper_6,
        R.drawable.wallpaper_7,
        R.drawable.wallpaper_8,
        R.drawable.wallpaper_9,
        R.drawable.wallpaper_10
    };

    public ImageAdapter(Context c) {
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(imageView.getScaleType().CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 400));
        return imageView;
    }

}

2 个答案:

答案 0 :(得分:0)

让我们说你的图片网址类似于http://myweb.com/images/1.jpg,它的计数方式就像2.jpg3.jpg和...

现在,您必须在getView方法中使用此行:

Picasso.with(getContext()).load("http://myweb.com/images/"+(position+1)+".jpg").fit().into(imageView);

顺便说一下,你可以在你的gradle中添加picasso库: compile 'com.squareup.picasso:picasso:2.5.2'

更新:如果您在列表中包含您的网址,则必须更改Picasso行,如下所示:

Picasso.with(getContext()).load(listOfUrls.get(position)).fit().into(imageView);

答案 1 :(得分:0)

public String[] mThumbIds = {url1,url2,....,url10};

在getView方法中添加此代码

URL url = new URL(mThumbIds [position]);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);

<强>编辑:

Load image from url 在链接中,请参阅 Kyle Clegg 给出的答案 如何在单独的线程中将图像从url加载到imageview。