android 拖动效果 Gallery 实例

大多数手机上都会有类似的动态效果,因为这样的动画会让人耳目一新。苹果曾经因此吸引了不少手机粉丝。那么在android上同样也可以实现此效果。
Gallery的功能就是用来显示这样的效果。简单说明一下Gallery是什么样的:
假设你放入了10个图片,那么你用Gallery的时候,用此容器来存放你的图片,在手机界面上会把图片显示出来(android的系统中带有自己的Gallery的风格)。那么你在点击后一张图片的时候前一张图片就会往前移动,而你点击的图片就会突出显示。你也可以触摸拖动图片,任意选择你想要的那张图片突出显示。
不过,通常的Gallery在你指定了10张或者更多的图片时,到了最后一张就不在循环显示了。也就是说,它只能限定显示图片。
通过一个实例来完成Gallery的用法。
首先:我们要知道Gallery是显示图片用的,那么就需要指定他的显示布局。
我们需要一个ImageView来完成此布局,而此布局需要继承BaseAdapter,来实现其中的方法来为Gallery实现效果。
我们所有要用到的图片要放在一个int型数组中,然后通过ImageView的setImageResource方法来设置需要显示的图片,然后在把此图片通过ImageView对象显示在手机屏幕上。
ImageAdapter类:


public class ImageAdapter extends BaseAdapter {
// 用来设置ImageView的风格
int mGalleryItemBackground;
private Context context;
//图片的资源ID
private Integer[] mImageIds = {
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5,
R.drawable.img6,
R.drawable.img7,
R.drawable.img8
};
//构造函数
public ImageAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
//返回所有图片的个数
@Override
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.length;
}
//返回图片在资源的位置
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
//返回图片在资源的位置
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//此方法是最主要的,他设置好的ImageView对象返回给Gallery
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(context);
//通过索引获得图片并设置给ImageView
imageView.setImageResource(mImageIds[position]);
//设置ImageView的伸缩规格,用了自带的属性值
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//设置布局参数
imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));
//设置风格,此风格的配置是在xml中
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}

public int getmGalleryItemBackground() {
return mGalleryItemBackground;
}

public void setmGalleryItemBackground(int mGalleryItemBackground) {
this.mGalleryItemBackground = mGalleryItemBackground;
}

}

上面的代码实现了要用来放置在Gallery中的ImageView对象。其中上面有几个部分要注意:
1.mGalleryItemBackground 此变量是用来设置ImageView的显示风格的,那么它的配置我写在了一个xml中(/valus/attrs.xml):

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

同时,我在网上看到有人把读取这个的代码
[quote]TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);[/quote]
同时也放在了构造函数中,但是我尝试了去这么做失败了,所以我把此代码提取了出来放在了Activity中设置,那么需要把mGalleryItemBackground 变量在ImageAdapter中放入set()方法。
2.getCount和getView。其中getCount是返回图片总数,但这个总数不能大于图片的实际数(可以小于),否则会抛出越界异常。
而getView是核心,Gallery组件要显示某一个图片时,就会调用getView方法,并将当前的图片索引(position参数)传给该方法,然后返回一个ImageView对象。它不是一下把所有的图片都显示出来,而是即时的显示。
那么在看activity中,在这里就简单了。
Activity01类:

public class Activity01 extends Activity {
/** Called when the activity is first created. */
private Gallery myGallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myGallery = (Gallery)findViewById(R.id.myGallery);
//这段代码是杨丰盛老师的《android开发揭秘》中这样写的
//myGallery.setBackgroundResource(R.drawable.bg0);
ImageAdapter adapter = new ImageAdapter(this);
//设置背景风格。Gallery背景风格定义在attrs.xml中
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
adapter.setmGalleryItemBackground(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0));
myGallery.setAdapter(adapter);
}
}

main.xml(这里只放入了一个Gallery):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery
android:id="@+id/myGallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

ok,到这里就结束了。我把我做的例子上传供大家下载学习。
ps:用到的图片都是杨老师的例子中的,我手中也有一本他的书,但是书中解说的很少,所以自己总结了一下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值