Android中Gallery和ImageSwitcher的使用实例
作者:徐刘根 发布时间:2021-05-28 15:52:12
标签:android,gallery,imageswitcher
效果如下:
布局文件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!
来源:https://blog.csdn.net/xlgen157387/article/details/45969277


猜你喜欢
- 今天因为工作需要,把以前编写的一个GPS测试程序拿出来重新修改了一下。这个程序说起来有些历史了,是我11年编写的,那时候学了Android开
- 本文为大家分享了Java实现班级管理系统的具体代码,供大家参考,具体内容如下需求:班级管理系统功能:对学生的信息进行管理1 登录系统 &nb
- 目录 任务和线程的区别: 一、认识Task和Task的基本使用1、认识Task2、创建Task 二、Task的
- 本文为大家分享了Android Studio使用USB真机调试的具体方法,供大家参考,具体内容如下以小米4为例,先将手机通过USB连接电脑,
- Hibernate中有HQL查询语法。但我们用得比较熟的还是数SQL语句,那么应该怎么来让Hibernate支持SQL呢?这个不用我们去考虑
- 本文为大家分享了java实现水果超市管理系统的具体代码,供大家参考,具体内容如下首先建立水果类的界面public class Fruit {
- 在C#语言中,DateTime是用来表示时间的类,在C#的DateTime时间类中,提供了好像时间对象加减法操作,可用于某一个时间对象加减
- 为什么要自定义缓存注解?Spring Cache本身提供@Cacheable、@CacheEvict、@CachePut等缓存注解,为什么还
- java @Value("${}")获取不到配置文件中值1、property.yml配置spring: ma
- 最近在学习springboot,session这个点一直困扰了我好久,今天把这些天踩的坑分享出来吧,希望能帮助更多的人。一、pom.xml配
- 曾经做过一个项目,其中登录界面的交互令人印象深刻。交互设计师给出了一个非常作的设计,要求做出包含根据情况可变色的下划线,左侧有可变图标,右侧
- Autowired有两种注入方式by typeby name默认使用的是byType的方式向Bean里面注入相应的Bean。例如:@Auto
- 前后端分离的项目,前端有菜单(menu),后端有API(backendApi),一个menu对应的页面有N个API接口来支持,本文介绍如何基
- 本文实例讲述了Android编程设计模式之抽象工厂模式。分享给大家供大家参考,具体如下:一、介绍抽象工厂模式(Abstract Factor
- 一.前言在日常的开发中,经常需要对方法参数进行校验(非空、长度等)。如果采用hardcode去校验(if..else..),会造成代码冗余,
- 1、HttpClient:代码复杂,还得操心资源回收等。代码很复杂,冗余代码多,不建议直接使用。2、RestTemplate: 是 Spri
- 当jvm虚拟机被关闭的时候,可能我们需要做一些处理,比如对连接的关闭,或者对一些必要信息的存储等等操作,这里就可以借助于虚拟机提供的钩子函数
- 前置工作:项目配置升到对应的29版本compileSdkVersion: 29,buildToolsVersion: ‘29.0.0'
- 什么是WebSocket?WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信—
- 网上有不少教程,那个提示框字符集都是事先写好的,例如用一个String[] 数组去包含了这些数据,但是,我们也可以吧用户输入的作为历史记录保