`

android 加载图片到gallery

阅读更多
/** == XML Layout ==
*
* <?xml version=\"1.0\" encoding=\"utf-8\"?>
* <LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
* android:orientation=\"vertical\"
* android:layout_width=\"fill_parent\"
* android:layout_height=\"fill_parent\"
* >
* <Gallery id=\"@+id/gallery\"
* android:layout_width=\"fill_parent\"
* android:layout_height=\"wrap_content\"
* android:gravity=\"bottom\"
* />
* </LinearLayout>
*
*/

package org.anddev.android.galleryexample;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

/* Find the gallery defined in the main.xml
* Apply a new (custom) ImageAdapter to it. */
((Gallery) findViewById(R.id.gallery))
.setAdapter(new ImageAdapter(this));
}

public class ImageAdapter extends BaseAdapter {
/** The parent context */
private Context myContext;

/** URL-Strings to some remote images. */
private String[] myRemoteImages = {
\"http://www.anddev.org/images/tiny_tutheaders/weather_forecast.png\",
\"http://www.anddev.org/images/tiny_tutheaders/cellidtogeo.png\",
\"http://www.anddev.org/images/tiny_tutheaders/droiddraw.png\"
};

/** Simple Constructor saving the \'parent\' context. */
public ImageAdapter(Context c) { this.myContext = c; }

/** Returns the amount of images we have defined. */
public int getCount() { return this.myRemoteImages.length; }

/* Use the array-Positions as unique IDs */
public Object getItem(int position) { return position; }
public long getItemId(int position) { return position; }

/** Returns a new ImageView to
* be displayed, depending on
* the position passed. */
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(this.myContext);

try {
/* Open a new URL and get the InputStream to load data from it. */
URL aURL = new URL(myRemoteImages[position]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
/* Apply the Bitmap to the ImageView that will be returned. */
i.setImageBitmap(bm);
} catch (IOException e) {
i.setImageResource(R.drawable.error);
Log.e(\"DEBUGTAG\", \"Remtoe Image Exception\", e);
}

/* Image should be scaled as width/height are set. */
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
/* Set the Width/Height of the ImageView. */
i.setLayoutParams(new Gallery.LayoutParams(150, 150));
return i;
}

/** Returns the size (0.0f to 1.0f) of the views
* depending on the \'offset\' to the center. */
public float getScale(boolean focused, int offset) {
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
}
}
}
分享到:
评论

相关推荐

    android gallery 只加载当前图片 图片缓存 异步 下载

    在Android开发中,"android gallery 只加载当前图片 图片缓存 异步 下载"这一主题涉及到几个关键概念和技术,这些技术对于构建一个高效、流畅的图片浏览应用至关重要。以下是对这些知识点的详细说明: 1. **只加载...

    android Gallery实现异步加载网络图片

    android Gallery实现异步加载网络图片 并只加载当前停止页面图

    Android基于Gallery实现网络图片和本地图片循环播放

    综上所述,实现Android基于Gallery的网络图片和本地图片循环播放功能,需要结合Adapter、图片加载库、网络请求和数据管理等多个技术点,同时考虑用户体验和性能优化。通过熟练掌握这些知识点,可以打造出流畅且功能...

    android Gallery实现异步加载网络图片 并只加载当前停止页面图.zip

    总的来说,这个`android Gallery实现异步加载网络图片 并只加载当前停止页面图`的示例展示了如何在Android应用中高效地处理大量网络图片的加载,通过异步加载和滚动监听,实现了只加载当前展示图片的策略,提升了...

    Android利用Gallery和ImageSwitcher实现在线相册图片预览功能(异步加载图片)

    在我们的在线相册预览场景中,当用户点击`Gallery`中的图片时,我们可以设置`ImageSwitcher`显示该图片的全尺寸版本,并添加一个平滑的过渡动画。 异步加载图片是为了避免UI线程阻塞,提高应用的响应速度。我们可以...

    Android从系统Gallery获取图片具体实现

    在Android开发中,从系统Gallery获取图片是一项常见的需求,这通常涉及到用户交互和图片选择的过程。本篇将详细阐述如何实现这一功能。 首先,了解Gallery应用。Gallery是Android系统内置的一个图库应用,用于浏览...

    android开发,gallery图片切换

    在`getView()`方法中,我们可以加载图片到`ImageView`中,如使用`Picasso`或`Glide`库: ```java public class GalleryAdapter extends BaseAdapter { private Context context; private List&lt;String&gt; imageUrls;...

    android 动态向Gallery中添加图片及倒影&&3D;效果

    在`Adapter`的`getView()`方法中,你会加载图片并设置到`ImageView`上。使用`Bitmap`对象可以加载本地资源或者网络资源,`BitmapFactory`类提供了加载`Bitmap`的方法。 为了实现倒影效果,我们需要创建图片的倒影。...

    一个关于android Gallery图片点击后放大缩小的例子

    在Android开发中,`Gallery`组件是一个非常有用的控件,它允许用户在水平方向上滚动浏览一系列的项目,常用于展示图片或者选择日期等场景。本示例将深入讲解如何实现`Gallery`中的图片点击后渐进式放大缩小的效果。 ...

    Android中gallery图片自动切换Demo

    3. **适配器**:创建一个继承自`BaseAdapter`的自定义适配器,重写`getCount()`、`getItem()`、`getItemId()`以及`getView()`方法,以将图片数据绑定到`Gallery`上。 ```java public class ImageAdapter extends ...

    Android应用源码之Gallery2_Android.zip

    Gallery2是一个针对Android平台的开源图片浏览应用,其源码为我们提供了一个深入理解Android系统中图片展示、手势操作以及图片库集成的实例。通过分析这个项目,我们可以学习到许多关于Android开发的重要知识点。 ...

    Android读取sdcard上的图片并用Gallery显示

    首先,在布局文件中添加Gallery: ```xml &lt;Gallery android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; ``` 接着,为Gallery设置适配器,将图片加载到...

    安卓Gallery实现异步加载网络图片源代码

    在加载网络图片的例子中,可以在doInBackground()方法中进行网络请求和图片解码,然后在onPostExecute()方法中更新UI,将图片显示在Gallery上。 6. **ImageView优化**: 使用高效的ImageView子类,如Picasso、Glide...

    Android基于Gallery的图片播放器

    7. **处理异常**:在实际开发中,要考虑到图片加载失败的情况,为此,我们可以在`ImageSwitcher`中设置默认图片,或者提供错误提示。 总结起来,Android基于Gallery的图片播放器项目可以帮助开发者掌握Android UI...

    android gallery图片轮播

    综上所述,"android gallery图片轮播"的实现涉及到多个技术点,包括ViewPager的使用、图片加载库的集成、自定义ViewGroup以及触摸事件处理等。开发者需要结合具体需求,灵活运用这些技术来构建功能丰富且高效的图片...

    Gallery循环删除图片

    在Android开发中,"Gallery循环删除图片"是一个常见的需求,主要涉及到 Gallery 组件的使用、Java反射机制的应用以及图片管理。下面将详细讲解这些知识点。 首先,Gallery 是 Android 提供的一种特殊的视图组件,...

    安卓Gallery照片墙画廊图库相关-Android利用Gallery和ImageSwitcher实现在线相册图片预览功能异步加载图片.rar

    本项目“安卓Gallery照片墙画廊图库相关-Android利用Gallery和ImageSwitcher实现在线相册图片预览功能异步加载图片”就是针对这种场景的一个实例。在这个项目中,开发者使用了`Gallery`组件和`ImageSwitcher`来实现...

    android Gallery 3d 图片浏览 oom

    总结来说,解决"android Gallery 3d 图片浏览 oom"问题的关键在于优化图片加载流程,合理使用内存和缓存策略,同时注意处理好读取SD卡图片的权限。通过自定义`CustomGallery`,我们可以打造出性能优异、用户体验良好...

    android Gallery 拖动式 图片浏览

    通常,我们会使用`ArrayAdapter`或者自定义的适配器,将图片资源绑定到`Gallery`上。例如: ```java ArrayList&lt;Integer&gt; imageIds = new ArrayList(); imageIds.add(R.drawable.image1); imageIds.add(R.drawable....

    Gallery浏览图片

    5. **监听事件**:`Gallery`提供了`OnItemSelectedListener`接口,通过实现这个接口,开发者可以监听到用户在`Gallery`上的选择变化,从而执行相应的操作。 6. **性能优化**:`Gallery`会智能地只加载可见项的视图...

Global site tag (gtag.js) - Google Analytics