您的位置:首页 > 移动开发 > Android开发

android-gallery-头像选择器效果

2011-05-09 12:54 555 查看
/**
 *
 * 测试gallery gallery头像选择器效果
 */
public class GalleryTestActivity extends Activity implements
  OnItemSelectedListener, ViewFactory {

 Gallery gallery;// xml中的组件Gallery

 ImageAdapater imageAdapater;// 自定义的ImageAdapater

 private ImageSwitcher imageSwitcher;// xml中的组件ImageSwitcher

 // 资源drawable
 private int[] resIds = new int[] { R.drawable.head0, R.drawable.head1,
   R.drawable.head2, R.drawable.head3 };

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // 加载Gallery组件
  gallery = (Gallery) findViewById(R.id.gallery);

  imageAdapater = new ImageAdapater(this);

  gallery.setAdapter(imageAdapater);

  gallery.setOnItemSelectedListener(this);

  imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);

  // 设置ImageSwitcher组件的工厂对象
  imageSwitcher.setFactory(this);

  // 设置ImageSwitcher组件显示图像的动画效果
  imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
    android.R.anim.fade_in));
  imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
    android.R.anim.fade_out));

 }

 int mGalleryItemBackground;

 public class ImageAdapater extends BaseAdapter {
  private Context mContext;

  public ImageAdapater(Context mContext) {
   this.mContext = mContext;
   // 获得Gallery组件的属性
   TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery1);
   mGalleryItemBackground = typedArray.getResourceId(
     R.styleable.Gallery1_android_galleryItemBackground, 0);
  }

  @Override
  public int getCount() {
   return resIds.length;// Integer.MAX_VALUE;//return 改成后面的可实现循环
  }

  @Override
  public Object getItem(int position) {
   return position;
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   final ImageView imageView = new ImageView(mContext);
   // 设置当前图像的图像(position为当前图像列表的位置)
   imageView.setImageResource(resIds[position % resIds.length]);
   imageView.setScaleType(ImageView.ScaleType.FIT_XY);
   imageView.setLayoutParams(new Gallery.LayoutParams(90, 90));
   // 设置Gallery组件的背景风格//系统默认的带边框的,入想去掉,可在onItemSelected()处理
   imageView.setBackgroundResource(mGalleryItemBackground);
   return imageView;

  }

 }

 int selectPos;

 @Override
 public void onItemSelected(AdapterView<?> parent, View view, int position,
   long id) {
  // 选中Gallery中某个图像时,在ImageSwitcher组件中放大显示该图像
  imageSwitcher.setImageResource(resIds[position % resIds.length]);
  selectPos = position;
  for (int i = 0; i < parent.getChildCount(); i++) {
   if (i == position) {
    view.setBackgroundResource(R.drawable.headseletor);// 选中效果
   } else {
    parent.getChildAt(i).setBackgroundResource(R.drawable.dialogbg);// 透明背景
    // 让默认的选框不可见
   }
   if (position == parent.getCount() - 1) {
    view.setBackgroundResource(R.drawable.headseletor);//
   }
  }
  // imageAdapater.notifyDataSetChanged();
 }

 @Override
 public void onNothingSelected(AdapterView<?> arg0) {

 };

 // ImageSwitcher组件需要这个方法来创建一个View对象(一般为ImageView对象)
 // 来显示图像
 @Override
 public View makeView() {
  ImageView imageView = new ImageView(this);
  imageView.setBackgroundColor(0xFF000000);
  imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
  imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  return imageView;

 }

}

 

//////////////////////////////////////////////////////////布局相关/////////////////////////////////////////////////////////////////

path /GalleryTest/res/layout/main.xml

 

<?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 组件-->
    <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:layout_marginTop="40px"
        android:spacing="5px"
        style="@drawable/selector_head"/>
    <!--  ImageSwitcher 组件 用于展示Gallery选中的资源显示--> 
    <ImageSwitcher android:id="@+id/imageswitcher" 
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:layout_marginTop="40px" />  
   
</LinearLayout>

 

 

path /GalleryTest/res/values/attrs.xml

 

<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- 定义 Gallery的属性-->
 <declare-styleable name="Gallery1">
  <attr name="android:galleryItemBackground" />
 </declare-styleable>
</resources>

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息