如何将多个图像加载到Android图库?

时间:2013-12-26 06:08:58

标签: android image url gallery

我想将一大堆图片从网址加载到我的图库中。 目前它们存储在drawable目录中,但我想从像http://i200.photobucket.com/albums/aa216/example/color.jpg这样的URL链接加载它们而不是@ drawable / image1

这是我的代码。

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ViewFlipper
        android:id="@+id/view_flipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >


        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"

                />

            <TextView
                style="@style/ImageTitle"
                android:text="@string/image1" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/image2" />

            <TextView
                style="@style/ImageTitle"
                android:text="@string/image2" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/image3" />

            <TextView
                style="@style/ImageTitle"
                android:text="@string/image3" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/image4" />

            <TextView
                style="@style/ImageTitle"
                android:text="@string/image4" />
        </RelativeLayout>
 </ViewFlipper>

    <LinearLayout
        style="@style/ButtonContainer"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:background="@drawable/media_play" />

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/media_pause" />
    </LinearLayout>

    <ImageView
        android:id="@+id/swipe_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/arrowleft" />

    <ImageView
        android:id="@+id/swipe_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/arrowright" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

来自Github的Uae UniversalImageLoader图书馆。

以zip&amp; amp;导入项目中的库。然后从你的java类

ImageView imageView = (ImageView) findViewById(R.id.your_view) 
String imageUrl = "your_url";  
ImageLoader imageLoader = ImageLoader.getInstance(); 
imageLoader.init(ImageLoaderConfiguration.createDefault(context));              
imageLoader.displayImage(imageUrl, imageView);

答案 1 :(得分:0)

您可以使用Universal image loader

http://github.com/nostra13/Android-Universal-Image-Loader

ImageView imageView = ... // view, where the image will be displayed
String imageUrl = ... // image URL 
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
imageLoader.displayImage(imageUrl, imageView);

它非常易于使用,并且可以处理大多数事情,例如缓存等。

如果您不想使用任何外部库,您也可以编写自己的代码来下载和显示图像。

遵循本教程

http://www.androidhive.info/2012/07/android-loading-image-from-url-http/