前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >安卓开发实现画廊效果

安卓开发实现画廊效果

作者头像
听着music睡
发布2018-05-18 12:35:09
1.4K0
发布2018-05-18 12:35:09
举报
文章被收录于专栏:Android干货Android干货

画廊 使用Gallery表示,按水平方向显示内容,并且可以用手指直接拖动图片移动,一般用来浏览图片,被选中的选项位于中间,可以响应事件显示信息。

xml布局文件基本语法

<Gallery

属性列表

/>

Gallery支持4中xml属性

 效果的具体实现过程

layout

代码语言:javascript
复制
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7 
 8 
 9     <Gallery  
10         android:id="@+id/gallery"  
11         android:spacing="5px"          //设置列表项之间的间距为5像素
12         android:unselectedAlpha="0.5"  //设置未被选中的列表项的透明度
13         android:layout_width="match_parent"  
14         android:layout_height="wrap_content" />
15 
16    
17 </LinearLayout>

Activity

代码语言:javascript
复制
 1 package xqx;
 2 
 3 import com.example.xqx_lianxi.R;
 4 
 5 import android.app.Activity;
 6 import android.content.res.TypedArray;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.view.ViewGroup;
10 import android.widget.AdapterView;
11 import android.widget.AdapterView.OnItemClickListener;
12 import android.widget.BaseAdapter;
13 import android.widget.Gallery;
14 import android.widget.ImageView;
15 import android.widget.Toast;
16 
17 public class MainGallery extends Activity{
18      //设置画廊图片
19     private int[] imageId = new int[] { R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         // TODO Auto-generated method stub
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.main_gallery);
25         //获取Gallery组件
26         Gallery gallery = (Gallery) findViewById(R.id.gallery);
27         BaseAdapter adapter = new BaseAdapter() {       
28                //获取当前选项ID
29                @Override  
30                public long getItemId(int position) {  
31                    return position;  
32                }  
33                //获取当前选项值
34                @Override  
35                public Object getItem(int position) {  
36                    return position;  
37                }  
38                //获取数量
39                @Override  
40                public int getCount() {  
41                    return imageId.length;  
42                }
43                
44             @Override
45             public View getView(int position, View convertView, ViewGroup parent) {  
46                 ImageView imageview;   //声明ImageView的对象  
47                 if (convertView == null) {  
48                     imageview = new ImageView(MainGallery.this); //创建ImageView的对象  
49                     imageview.setScaleType(ImageView.ScaleType.FIT_XY);  //设置缩放方式
50                     imageview.setLayoutParams(new Gallery.LayoutParams(500, 400));  
51                     TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);  
52                     imageview.setBackgroundResource(typedArray.getResourceId(  
53                             R.styleable.Gallery_android_galleryItemBackground,  
54                             0));  
55                     imageview.setPadding(5, 0, 5, 0);  //设置imageview的内边距
56                 } 
57                 else 
58                 {  
59                     imageview = (ImageView) convertView;  
60                 }  
61                 imageview.setImageResource(imageId[position]);  
62                 return imageview;  
63             }  
64               
65         };
66            //将适配器与Gallery关联
67            gallery.setAdapter(adapter);  
68            gallery.setSelection(imageId.length / 2); //默认显示的图片的id  
69            //画廊图片的点击事件
70            gallery.setOnItemClickListener(new OnItemClickListener() {  
71                @Override  
72                public void onItemClick(AdapterView<?> parent, View view,  
73                        int position, long id) {
74                
75                    Toast.makeText(MainGallery.this,  
76                            "第" + String.valueOf(position+1) + "张图片被选中",  
77                            Toast.LENGTH_SHORT).show();  
78                }  
79            });  
80     
81 }
82 }

最后在res/values/string.xml中添加一段代码  ,这里对应activity中的51行

代码语言:javascript
复制
1 <declare-styleable name="Gallery">  
2         <attr name="android:galleryItemBackground" />  
3     </declare-styleable>

这样便完成了一个画廊的效果

效果图:

可以看到 一共有6张图片 默认显示第4张 

代码语言:javascript
复制
 gallery.setSelection(imageId.length / 2); //默认显示的图片的id
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-06-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档