Android中Gallery(画廊)的创建与使用
来源:互联网 发布:高斯滤波算法实现 编辑:程序博客网 时间:2024/06/01 20:40
Gallery(画廊)的创建步骤:
1.添加一个控件
2.添加数据(集合或数组中)
3.自定义适配器(接收数据集和上下文),把四个方法完善,在getView()方法中动态添加ImageView将其显示
4.setAdapter
Gallery的使用—循环播放
1:在getCount方法返回一个很大的数
Integer.MAX_VALUE。
2:将getView方法中的position值设置循环(取余)
resIds[position % resIds.length]
reslds为存放图片的数组
例子代码:
效果图:
1.添加一个控件
2.添加数据(集合或数组中)
3.自定义适配器(接收数据集和上下文),把四个方法完善,在getView()方法中动态添加ImageView将其显示
4.setAdapter
Gallery的使用—循环播放
1:在getCount方法返回一个很大的数
Integer.MAX_VALUE。
2:将getView方法中的position值设置循环(取余)
resIds[position % resIds.length]
reslds为存放图片的数组
例子代码:
MainActivity.javapackage com.zhiyuan3g.gallery;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.LinearLayout;import com.zhiyuan3g.gallery.adapter.MyAdapter;public class MainActivity extends AppCompatActivity { private int[] image = {R.drawable.aoly, R.drawable.jie, R.drawable.jks, R.drawable.yasu, R.drawable.zhaozilong}; private Gallery gallery; private LinearLayout mLinearLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { gallery = (Gallery) findViewById(R.id.gallery); mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout); //设置适配器(使用自定义适配器) gallery.setAdapter(new MyAdapter(image,this)); //给gallery控件添加条目(item)监听 gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //设置当点击某个item时,设置此item显示的图片为布局页面的背景 mLinearLayout.setBackgroundResource(image[position]); } }); }}
MyAdapter.javapackage com.zhiyuan3g.gallery.adapter;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;public class MyAdapter extends BaseAdapter { private int[] image; private Context mContext; /** * function:构造函数,用于传递参数 * @param image:图片数组的参数 * @param context:上下文的参数 */ public MyAdapter(int[] image, Context context) { this.image = image; mContext = context; } //获取长度,为图片数组的长度 @Override public int getCount() { return image.length; } //获取item显示的内容,为图片数组的内容 @Override public Object getItem(int position) { return image[position]; } //获取item的id,为position @Override public long getItemId(int position) { return position; } //获取视图,我们需要自里面创建视图以返回给item来显示 @Override public View getView(int position, View convertView, ViewGroup parent) { //创建imageview视图,给其上下文(context),锁定创建的位置 ImageView imageView = new ImageView(mContext); //给imageview添加所要显示的内容,为image图片数组内容 imageView.setImageResource(image[position]); //将imageview返回出去 return imageView; }}
Activity_main.xml<?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:layout_width="match_parent" android:orientation="horizontal" android:id="@+id/linearlayout" android:layout_height="match_parent" android:background="@drawable/aoly" 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="com.zhiyuan3g.gallery.MainActivity"> <Gallery android:spacing="5dp" android:layout_width="match_parent" android:layout_height="320dp" android:id="@+id/gallery" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/></LinearLayout>
效果图:
0 0
- Android中Gallery(画廊)的创建与使用
- android中画廊视图Gallery和ImageSwitcher组件的使用
- android 画廊控件Gallery的使用
- Android中画廊视图Gallery和ImageSwitcher组件的使用(十三)
- 画廊控件Gallery的使用
- android 中画廊Gallery的单页滑动
- 【Android 开发】:UI控件之 Gallery 画廊控件的使用
- Android开发学习之Gallery画廊的使用
- Android初级开发(四)——补充7、Gallery(画廊)的使用
- android中Gallery画廊效果(自动滚动)
- 89 Android Gallery 简单的画廊效果
- 画廊布局(ImageSwitcher与Gallery)
- android开发之gallery(画廊)
- android——画廊(Gallery)
- Android-画廊实现 (Gallery , ImageSwitch)
- Android新手入门2016(15)--Gallery画廊
- android 画廊 gallery
- Android--Gallery,画廊
- 合理使用Ehcache
- numpy使用小技巧
- 2月14号关于编辑地址后保存不更新的问题
- 服务器远程安装虚拟机
- Spark RDD算子介绍
- Android中Gallery(画廊)的创建与使用
- C++中的内联函数(inline)
- 学习JAVA的五个阶段
- H5+bootstrap开发半透明响应式网站后台模板
- 微信小程序的百度地图获取地理位置 —— 微信小程序教程系列(15)
- Leetcode——53. Maximum Subarray
- 493. Reverse Pairs
- windows安装pyqt4的过程
- POJ 1753 基础搜索 DFS 七