Android中Gallery和ImageSwitcher的使用实例

所属分类: 软件编程 / Android 阅读数: 95
收藏 0 赞 0 分享

效果如下:

布局文件activity_main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >
  <ImageSwitcher
    android:id="@+id/imageSwitcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:paddingTop="30px" >
  </ImageSwitcher>
  <Gallery
    android:id="@+id/gallery1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:spacing="5px"
    android:unselectedAlpha="0.6" />
</RelativeLayout>

MainActivity.java代码如下:

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
  private int imageId[] = new int[] { R.drawable.a, R.drawable.b,
      R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f,
      R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.j,
      R.drawable.k };
  private ImageSwitcher imageSwitcher;
  private Gallery gallery;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher);
    gallery = (Gallery) this.findViewById(R.id.gallery1);
    // 设置动画效果
    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_in));
    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_out));
    imageSwitcher.setFactory(new ViewFactory() {
      @Override
      public View makeView() {
        ImageView imageView = new ImageView(MainActivity.this);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // 设置保持纵横比居中
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        return imageView;
      }
    });
    GalleryAdapter adapter = new GalleryAdapter(MainActivity.this,imageId);
    gallery.setAdapter(adapter);
    gallery.setSelection(imageId.length / 2);
    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
        imageSwitcher.setImageResource(imageId[position]);
      }
      @Override
      public void onNothingSelected(AdapterView<?> arg0) {
      }
    });
  }
}

其中需要的一个适配器:

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryAdapter extends BaseAdapter {
  private int[] imageId;
  private Context mContext;
  /**
   * 穿入上下文和图片资源数组
   * @param mContext
   * @param imageId
   */
  public GalleryAdapter(Context mContext, int[] imageId) {
    this.mContext = mContext;
    this.imageId = imageId;
  }
  @Override
  public int getCount() {
    return imageId.length;
  }
  @Override
  public Object getItem(int position) {
    return imageId[position];
  }
  @Override
  public long getItemId(int position) {
    return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView1;
    if (convertView == null) {
      imageView1 = new ImageView(mContext);
      imageView1.setScaleType(ImageView.ScaleType.FIT_XY);
      imageView1.setLayoutParams(new Gallery.LayoutParams(180, 135));
      TypedArray typedArray = mContext
          .obtainStyledAttributes(R.styleable.Gallery);
      imageView1.setBackgroundResource(typedArray.getResourceId(
          R.styleable.Gallery_android_galleryItemBackground, 0));
      imageView1.setPadding(5, 0, 5, 0); // 设置ImageView的内边距
    } else {
      imageView1 = (ImageView) convertView;
    }
    imageView1.setImageResource(imageId[position]); // 为ImageView设置要显示的图片
    return imageView1; // 返回ImageView
  }
}

到此 OK!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多