Android控件开发之Gallery
来源:互联网 发布:淘宝网上开店的步骤 编辑:程序博客网 时间:2024/05/16 10:43
Gallery是Android中的图片库控件。可以浏览自己的图片相册。
效果图
本程序main.xml源码
本程序ImageAdapter类源码
package com.sx.Gallery;
- <?xml version="1.0" encoding="utf-8"?>
- lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- lt;TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- lt;Gallery
- android:id="@+id/gallery"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:spacing="2px" />
- <!-- android:spacing="2px"用来设置图片之间的间距 -->
- lt;/LinearLayout>
JAVA源码
- import android.content.Context;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.ImageView;
- public class ImageAdapter extends BaseAdapter
- {
- private Context context;
- private int[] MyImageIDs =
- {
- R.drawable.img1,
- R.drawable.img2,
- R.drawable.img3,
- R.drawable.img4,
- R.drawable.img5,
- R.drawable.img6,
- R.drawable.img7,
- R.drawable.img8,
- R.drawable.img9,
- R.drawable.img10,
- R.drawable.img11,
- R.drawable.img12,
- R.drawable.img13,
- R.drawable.img14,
- R.drawable.img15,
- R.drawable.img16,
- R.drawable.img17,
- R.drawable.img18,
- R.drawable.img19,
- R.drawable.img20
- };
- public ImageAdapter(Context context)
- {
- this.context = context;
- }
- // 获取图片个数
- public int getCount()
- {
- return MyImageIDs.length;
- //return Integer.MAX_VALUE;
- }
- // 获取图片在库中的位置
- public Object getItem(int position)
- {
- return position;
- }
- // 获取图片在库中的ID
- public long getItemId(int id)
- {
- return id;
- }
- public View getView(int position, View convertView, ViewGroup parent)
- {
- ImageView imageview = new ImageView(this.context);
- // 给ImageView设置资源
- imageview.setImageResource(this.MyImageIDs[position]);
- // 设置布局 图片120*120
- imageview.setLayoutParams(new Gallery.LayoutParams(120, 120));
- // 设置显示比例类型
- imageview.setScaleType(ImageView.ScaleType.FIT_XY);
- //imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
- return imageview;
- }
- }
- //要想实现图片无限循环可拖动修改getCount() 函数改为返回值为return Integer.MAX_VALUE;
- //imageview.setImageResource(this.MyImageIDs[position] % MyImageIDs.length.); 即可
- Activity源码
- package com.sx.Gallery;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.Gallery;
- import android.widget.Toast;
- public class GalleryActivity extends Activity
- {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- CreateGallery();
- }
- private void CreateGallery()
- {
- //获得Gallery对象
- Gallery gellery=(Gallery)this.findViewById(R.id.gallery);
- //添加ImageAdapter给Gallery对象 注意哦Gallery类并没有setAdapter这个方法 这个方法是从AbsSpinner类继承的
- gellery.setAdapter(new ImageAdapter(this));
- //设置Gallery的背景图片
- //gellery.setBackgroundResource(R.drawable.bg0);
- //设置Gallery的事件监听
- gellery.setOnItemClickListener(new GalleryItemListener());
- }
- class GalleryItemListener implements OnItemClickListener
- {
- public void onItemClick(AdapterView<?> parent, View view, int position, long id)
- {
- Toast.makeText(GalleryActivity.this, "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT).show();
- }
- }
- }
0 0
- Android控件开发之Gallery
- Android控件开发之Gallery
- Android控件开发之Gallery
- android控件之gallery
- Android控件之Gallery
- Android控件之Gallery
- Android控件之Gallery
- 【Android 开发】:UI控件之 Gallery 画廊控件的使用
- Android之Gallery控件使用
- Android 控件使用之 Gallery
- Android控件之Gallery探究
- Android控件之Gallery探究
- Android 控件之Gallery图片集
- Android控件之Gallery(画廊)
- Android 控件之Gallery图片集
- Android 控件之Gallery图片集
- Android 控件之Gallery图片集
- Android体验系列之--Gallery控件
- 在Ubuntu平台上创建Cordova Camera HTML5应用
- ABAP table control
- 均方根值(RMS)+ 均方根误差(RMSE)+标准差(Standard Deviation)
- SmartImageView 智能异步的ImageView
- 软件开发工具 (四)——技术
- Android控件开发之Gallery
- 图片预览
- java多线程-专题-聊聊并发(二)Java SE1.6中的Synchronized
- 《转》Ceilometer Alarm API 参数详解 及 举例说明
- zabbix搭建总结
- ubuntu 14.04.1下安装android 4.2编译环境出错总结
- 哈尔小波变换实例讲解
- 成为明星软件工程师的十种特质
- 函数绑定在Java和C++中的区别