关于android 的相册 的简单的实现方法

从网络获取图片的相册。我查过了ImageSwitcher

但是请宽恕我没有找到适用于网络图片地址的方法。所以改用 ImageView了。

首先我们要有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" >
        <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/relativeLayout1" >
    <ImageView
        android:id="@+id/imageSwitcher1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">
    </ImageView>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/picture_left"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="54dp"
            android:background="@drawable/left" />

        <Button
            android:id="@+id/picture_right"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="54dp"
            android:background="@drawable/right" />

    </RelativeLayout>

</RelativeLayout>  


图片 right 和 left 都是图片。图标。

我们将图片地址存储在一个list集合里面。

还有一个用于标识的 int 类型 索引 index

	private ArrayList<String> mList;
	private int index =0;


然后我们需要将网络图片的url给浓缩成一个 图片格式 名字叫bitmap。

这时我们需要用到这个方法

public static Bitmap getHttpBitmap(String url) {
		
		Bitmap bitmap = null;
		try {
			URL myFileUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
			conn.setConnectTimeout(0);
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		}
			return bitmap;
		}


有了这个方法。我们需要将图片给填充。这个时候需要用到

Image.setImageBitmap(Bitmap bitmap)

这个方法来进行填充。

这里再附加上相册的处理办法。图片转换的处理办法

public void onClick(View view) {
		// TODO Auto-generated method stub
		if(view == picUp){
			if(index == 0){
				index = index + 1;
				Bitmap bitmap =getHttpBitmap(mList.get(index));
				picture.setImageBitmap(bitmap);
			}else{
				index = index - 1;
				Bitmap bitmap =getHttpBitmap(mList.get(index));
				picture.setImageBitmap(bitmap);
			}
		}
		if(view == picDown){
			if(index +1 == mList.size()){
				index = index - 1;
				Bitmap bitmap =getHttpBitmap(mList.get(index));
				picture.setImageBitmap(bitmap);
			}else{
				index = index + 1;
				Bitmap bitmap =getHttpBitmap(mList.get(index));
				picture.setImageBitmap(bitmap);
			}
		}
	}


picUp是 ←这个箭头

picDown是 → 这个箭头。

转载请注明地址噢。亲。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值