如何在ViewPager中显示来自网络的图像?

时间:2015-06-29 09:52:30

标签: android android-viewpager

我需要创建像图库一样的应用:我应该使用视图寻呼机,当用户向左或向右滑动时,应该从网址加载图片。目前我有一个数组,其中包含drawable文件夹中的图像。我想知道一种从Web加载图像并在ViewPager中显示它的方法。

这是我的Activity类。

private void RecognizedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //gte the selected item from listbox
        string inputTextWritePad = RecognizedListBox.SelectedItem.ToString();
        //add the item to RichEditBox
        MyTextNote.Document.Selection.Text += inputTextWritePad + " ";
        //clear the canvas 
        recognizerShared.ClearInk(InkCanvas);
        //return the listbox to vide
        RecognizedListBox.ItemsSource = null;
    }

这是我的BaseAdapter类

public class FullImageActivity2 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_image_activity2);

        //Selected image id
        Integer[] newArray={R.drawable.digitallovesaktid,R.drawable.abcd,R.drawable.blue};


        ImageAdapter imageAdapter = new ImageAdapter(this, newArray);
        List<ImageView> images = new ArrayList<ImageView>();

        for (int i1 = 0; i1 < imageAdapter.getCount(); i1++) {
            ImageView imageView = new ImageView(this);

            imageView.setImageResource(imageAdapter.mThumbIds[i1]);

            images.add(imageView);
        }

        // Finally create the adapter
        ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(images);
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(imagePagerAdapter);


    }
}

这是我的寻呼机适配器类

        public class ImageAdapter extends BaseAdapter {
    private Context mContext;


    // Keep all Images in array
    public Integer[] mThumbIds = {};


    public ImageAdapter(Context c, Integer[] mThumbIds2){
        mContext = c;
        this.mThumbIds=mThumbIds2;
    }



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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
        //imageView.setBackgroundResource(R.layout.edit_border);
        imageView.setPadding(3, 3, 3, 3);

        return imageView;
    }
}

1 个答案:

答案 0 :(得分:0)

使用Picasso库从网址加载图片。访问链接http://square.github.io/picasso/