Gallery 解析

Gallery是布局小部件,这个部件用于水平方向显示,以水平中心为轴转动的一个图库相册。在本教程中,您将创建一个画廊的照片, 每次Gallery项被选中时显示一个Toast消息。

 

1、建立一个以HelloGallery命名的新项目。

2、找到一些你喜欢的或者提供的事例的图片放到res/drawable/目录下。

3、将下面的代码敲到res/layout/main.xml当中

<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
这个貌似没什么好解释的,就是声明了一个Gallery的控件,简单的添加了一个id和长宽。

正常来说的话,布局不应该是这样的,这样只能在界面里面出现这一个Gallery控件,远远达不到开发任何东西的目的。

4、打开HelloGallery.java,把下面的代码复制进去

super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Gallery g = (Gallery) findViewById(R.id.gallery);
		g.setAdapter(new ImageAdapter(this));

通过findViewbyID从xml文件中找到我们的Gallery

为这个Gallery添加一个适配器

这里只是简单的实现Gallery的功能,也可以添加一些ItemSelectedListener之类的监听器。

5、在res/values/目录下建立一个名为attrs.xml的xml文件,并输入一下内容:

	<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="HelloGallery">
        <attr name="android:galleryItemBackground" />
   </declare-styleable>
</resources>

这是一个定制的styleable资源,可以应用于一个布局。在本例中,它用来把我们图片放入Gallery。

<attr>是一个styleable的特定的属性。

在这种情况下,它指的是当前项目的一个属性——galleryItemBackground,它是用来定义Gallery的内容的边界样式。在接下来的步骤中,您将看到如何引用这个属性来申请Gallery的每一项内容。

6、在HelloGallery中加入下面代码

public class ImageAdapter extends BaseAdapter {
		private Integer[] mImageIds = { R.drawable.p1, R.drawable.p2,
			R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6,
			R.drawable.p7 };
		int mGalleryItemBackground;
		private Context mContext;
		public ImageAdapter(Context c) {
			mContext = c;
			TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
			mGalleryItemBackground = a.getResourceId(					R.styleable.HelloGallery_android_galleryItemBackground, 0);
			a.recycle();
		}
		public int getCount() {
			return mImageIds.length;
		}
		public Object getItem(int position) {
			return position;
		}
		public long getItemId(int position) {
			return position;
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(mContext);
			i.setImageResource(mImageIds[position]);
			i.setLayoutParams(new Gallery.LayoutParams(150, 100));
			i.setScaleType(ImageView.ScaleType.FIT_XY);
			i.setBackgroundResource(mGalleryItemBackground);
			return i;
		}
	}

首先,声明成员变量,包括一个引用保存在res下的图片的id所组成的数组。

下一步,是类的构造函数,是一个有参构造函数,里面使用了一个TypedArray数组,这个数组比较特殊,api文档中特别指出,在使用完这个类后,务必要对其进行回收(a.recycle();)。

obtainStyledAttributes方法返回的是一个TypedArray类型的值。

对于TypedArray类在api文档上是这样解释的:它是一个用来装载从obtainStyledAttributes或者obtainAttributes取回数据的数组容器

说白了,a就是用来接收并且传递我们自定义控件(styleable)属性的容器。

如果报错,可以将代码做如下修改

TypedArraya =c.obtainStyledAttributes(R.styleable.HelloGallery);

最后要说的是这个适配器里面getCount(),getItem(int),getItemId(int)三个方法是必须实现的。

我们用构造函数传递进来的Context创建一个新的ImageView,并且对这个ImageView进行了设置,最后将我们从xml文件传递过来的值设置到ImageView当中,就完成了我们的操作了。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package e gallery; import android app Activity; import android os Bundle; import android view LayoutInflater; import android view View; import android view ViewGroup; import android view ViewGroup MarginLayoutParams; import android widget AdapterView; import android widget AdapterView OnItemSelectedListener; import android widget BaseAdapter; import android widget Gallery; import android widget ImageView; import e gallery R; public class MainActivity extends Activity implements OnItemSelectedListener { Gallery gallery; ImageView main imageView; ImageView picture imageView; Gesture+ gesture; int[] ids1 { R drawable b R drawable c R drawable d R drawable f R drawable g }; @Override protected void onCreate Bundle savedInstanceState { super onCreate savedInstanceState ; setContentView R layout main ; main imageView ImageView findViewById R id main imageView ; gallery Gallery findViewById R id gallery ; gallery setOnItemSelectedListener this ; 怎么设置从中间开始 gallery setAdapter new MyAdapter ; } class MyAdapter extends BaseAdapter{ @Override public int getCount { return Integer MAX VALUE; } @Override public Object getItem int position { return null; } @Override public long getItemId int position { return 0; } @Override public View getView int position View convertView ViewGroup parent { LayoutInflater layoutInflater LayoutInflater from MainActivity this ; convertView layoutInflater inflate R layout picture null ; picture imageView ImageView convertView findViewById R id picture imageView ; picture imageView setImageResource ids1[position % 5] ; return convertView; } } @Override public void onItemSelected AdapterView< > parent View view int position long id { main imageView setImageResource ids1[position % 5] ; } @Override public void onNothingSelected AdapterView< > parent { } }">package e gallery; import android app Activity; import android os Bundle; import android view LayoutInflater; import android view View; import android view ViewGroup; import android view ViewGroup MarginLayoutParams; import android widget AdapterView; import android widget AdapterView OnItemSelecte [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值