Android ViewPager 和从网络加载图像

标签 android image android-viewpager

我需要制作一个像图片库一样工作的小应用程序:我应该使用 View 寻呼机,当用户向左或向右滑动时,应用程序应该从网址加载图像。我有一个带有 url 的数组,当用户滑动到新页面时,它应该开始加载特定图像(它不应该在开始时加载所有图像)。例如,如果我在第一页,我应该看到 url 中的第一张图像(数组中的索引 0)。当我滑动到第二页时,应用程序应该开始从索引 1 加载图像,如果完成加载,它应该出现在屏幕上。我使 View 寻呼机可以使用资源中的图像,但无法使用远程图像。

我不应该使用任何现有的库、代码或类似的东西。到目前为止,这是我的代码: 主要 Activity :

    urls = new int[] {
            R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i4, R.drawable.i5

    };
    viewPager = (ViewPager) findViewById(R.id.pager);
    adapter = new ViewPagerAdapter(MainActivity.this, urls);        
    viewPager.setAdapter(adapter);

这是我的 View 适配器:

public ViewPagerAdapter(Context context, int[] urls) {
    this.context = context;
    this.urls = urls;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.viewpager_item, container,
            false);
    // Locate the TextViews in viewpager_item.xml
    imgflag = (ImageView) itemView.findViewById(R.id.image);
    imgflag.setImageResource(urls[position]);
    ((ViewPager) container).addView(itemView);
    return itemView;
}

layout.viewpager_item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp" >
<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="#000000"
    android:padding="1dp" />

有什么建议吗?

最佳答案

您可以使用类似 Picasso 的 ImageLoader

Picasso.with(context).load(urls[position]).into(imgflag);

或者:

public class AsynImageLoader extends AsyncTask<Void,Void,Void>{

    public String url;
    public ImageView img ;

    public AsynImageLoader(ImageView img,String url){
        this.url = url;
        this.img = img;
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Bitmap bitmap;
            URL imageUrl = new URL(url);

            HttpURLConnection connection;
            if (url.startsWith("https://")) {
                connection = (HttpsURLConnection) imageUrl.openConnection();
            } else {
                connection = (HttpURLConnection) imageUrl.openConnection();
            }
            connection.setConnectTimeout(30000);
            connection.setReadTimeout(30000);
            connection.setInstanceFollowRedirects(true);

            InputStream is = connection.getInputStream();

            bitmap = BitmapFactory.decodeStream(is);
            if(img!=null){
                img.setImageBitmap(bitmap);
            }

        }catch(Exception e){

        }
        return null;
    }
}

在你的适配器中你可以写:

AsynImageLoader task = new AsynImageLoader(imgflag,urls[position]);
task.execute();

关于Android ViewPager 和从网络加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25448671/

相关文章:

java - 在android中使用系统时间函数获取时间戳?

android - 从返回堆栈返回后 fragment 不刷新内容

android - 如何在主屏幕小部件中包含 CardView

android - Phonegap Android 应用程序图标拒绝更新

android - 无法显示YouTube的缩略图?

c - 在 C/Linux 中显示不断更新的图像的简便方法

android - fragment 递归进入 executePendingTransactions 错误

android - 每次我在 View 寻呼机中更改或滑动页面时都会调用 onDraw() 方法

android - 如何强制 PreferenceActivity 处理我的 SharedPreferences?

javascript - Jquery 在行路径中水平水平制作六个图像的动画