Gallery、ImageSwitcher
来源:互联网 发布:vscode 设置语法高亮 编辑:程序博客网 时间:2024/05/21 10:29
转载请注明出处:http://blog.csdn.net/mr_leixiansheng/article/details/64123557
使用方式类似于ListView
步骤:
1、设置好数据源
2、设置适配器
3、添加适配器
4、实现监听
代码如下:
1、界面设置
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.leixiansheng.mygallery.MainActivity"> <Gallery android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content"> </Gallery> <ImageSwitcher android:id="@+id/image_switcher" android:layout_width="match_parent" android:layout_height="wrap_content"> </ImageSwitcher></LinearLayout>2、设置适配器
package com.example.leixiansheng.mygallery;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;/** * Created by Leixiansheng on 2017/3/20. */public class Myadpter extends BaseAdapter { private int[] pics; private Context context; public Myadpter(int[] pics, Context context) { this.pics = pics; this.context = context; } @Override public int getCount() {// return pics.length; return Integer.MAX_VALUE; } @Override public Object getItem(int i) { return pics[i]; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ImageView imageView = new ImageView(context); //不循环显示// imageView.setBackgroundResource(pics[i]); //循环显示 imageView.setBackgroundResource(pics[i% pics.length]); //设置图片大小 imageView.setLayoutParams(new Gallery.LayoutParams(400, 300)); //设置拉伸模式 imageView.setScaleType(ImageView.ScaleType.FIT_XY); return imageView; }}3、主程序实现功能
package com.example.leixiansheng.mygallery;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher;public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory{ private int[] pics = {R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic6, R.drawable.pic7}; private Gallery gallery; private Myadpter myadpter; private ImageSwitcher imageSwitcher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gallery = (Gallery) findViewById(R.id.gallery); imageSwitcher = (ImageSwitcher) findViewById(R.id.image_switcher); myadpter = new Myadpter(pics, this); gallery.setAdapter(myadpter); gallery.setOnItemSelectedListener(this); imageSwitcher.setFactory(this); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); } //监听 @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { imageSwitcher.setBackgroundResource(pics[i % pics.length]); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } @Override public View makeView() { ImageView imageView = new ImageView(this); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); return imageView; }}
0 0
- Gallery、ImageSwitcher
- gallery ,imageSwitcher 结合使用
- Gallery和ImageSwitcher实例
- Android Gallery 和 ImageSwitcher
- 图片切换ImageSwitcher&Gallery
- Gallery和ImageSwitcher
- Gallery和ImageSwitcher
- Gallery与ImageSwitcher总结
- ImageSwitcher&Gallery练习
- Android之ImageSwitcher,Gallery用法
- ImageSwitcher 和 Gallery 的使用
- ImageSwitcher和Gallery的运用
- ImageSwitcher和Gallery的使用
- Android:ImageSwitcher 和 Gallery用法
- ImageSwitcher gallery 实现图片展示
- Gallery+ImageSwitcher用法实例解析
- ImageSwitcher与gallery图片浏览
- Android入门第十三篇之Gallery + ImageSwitcher
- [Linux/Ubuntu] vi/vim 使用方法讲解
- SQL 函数
- 水果分类
- Java并发编程
- 中心极限定理的一个例子:大小医院的新生儿
- Gallery、ImageSwitcher
- Java基础总结(一)对象,类,属性,方法
- 简单的mybatis例子,用idea写的
- pip安装软件时出现Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build*的解决方案
- springMVC+maven+nginx ueditor上传配置详解
- android屏幕密度和res文件目录对应关系
- 网络中的三种通讯模式:单播、广播、组播(多播)
- 第四周作业:Integer Replacement
- 全排列算法的递归实现