使用Gallery控件实现个人相簿功能

首先将图片导入“Drawable”目录,在响应onCreate的同时将素材图片载入到Gallery Widget中;然后添加一个OnItemClick事件以取得图片的ID编号,这样就可以响应用户单击图片的状态,完成Gallery的高级使用。

在main.xml文件中插入一个Gallery控件:

<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/mygallery"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
/>

编写attrs.xml文件,设置定义Layout外部资源的样式,并且设置随着滑动而改变Layout背景图片效果。

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="Gallery">
    <attr name="android:galleryItemBackground" />
  </declare-styleable> 
</resources>


ImageAdapter继承于BaseAdapter类的未实现方法的重写构造,通过Gallery中的OnItemClick()方法来响应图片滑动及Layout的宽和高的设置。
package com.EX027;

import com.EX027.R;

import android.app.Activity;
import android.os.Bundle;

/* 本范例需使用到的class */
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class EX027 extends Activity 
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    /*通过findViewById取得*/
    Gallery g = (Gallery) findViewById(R.id.mygallery);
    /* 添加一ImageAdapter并设置给Gallery对象 */
    g.setAdapter(new ImageAdapter(this));
    
    /* 设置一个itemclickListener并Toast被点击图片的位置 */
    g.setOnItemClickListener(new OnItemClickListener() 
    {
      public void onItemClick
      (AdapterView<?> parent, View v, int position, long id)
      {
        Toast.makeText
        (EX027.this, getString(R.string.my_gallery_text_pre)
        + position+ getString(R.string.my_gallery_text_post), 
        Toast.LENGTH_SHORT).show();
      }
    });
  }
  
  /* 改写BaseAdapter自定义一ImageAdapter class */
  public class ImageAdapter extends BaseAdapter 
  {
    /*声明变量*/
    int mGalleryItemBackground;
    private Context mContext;
    
    /*ImageAdapter的构造器*/
    public ImageAdapter(Context c) 
    {
      mContext = c;
      
      /* 使用在res/values/attrs.xml中的<declare-styleable>定义
      * 的Gallery属性.*/
      TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
      
      /*取得Gallery属性的Index id*/
      mGalleryItemBackground = a.getResourceId
      (R.styleable.Gallery_android_galleryItemBackground, 0);
      
      /*让对象的styleable属性能够反复使用*/ 
      a.recycle();
    }
    
    /* 覆盖的方法getCount,返回图片数目 */
    public int getCount() 
    {
      return myImageIds.length;
    }
         
    /* 覆盖的方法getItemId,返回图像的数组id */

    public Object getItem(int position) 
    {
      return position;
    }
    public long getItemId(int position) 
    {
      return position;
    }
    
    /* 覆盖的方法getView,返回一View对象 */
    public View getView
    (int position, View convertView, ViewGroup parent)
    {
      /*产生ImageView对象*/
      ImageView i = new ImageView(mContext);
      /*设置图片给imageView对象*/
      i.setImageResource(myImageIds[position]);
      /*重新设置图片的宽高*/
      i.setScaleType(ImageView.ScaleType.FIT_XY);
      /*重新设置Layout的宽高*/
      i.setLayoutParams(new Gallery.LayoutParams(136, 88));
      /*设置Gallery背景图*/
      i.setBackgroundResource(mGalleryItemBackground);
      /*返回imageView对象*/
      return i;
    }
    
    /*建构一Integer array并取得预加载Drawable的图片id*/
    private Integer[] myImageIds = 
    {
      R.drawable.photo1,
      R.drawable.photo2,
      R.drawable.photo3,
      R.drawable.photo4,
      R.drawable.photo5,
      R.drawable.photo6,
    };   
  } 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值