android Gallery(画廊)以及BaseAdapter

本文详细介绍了如何使用XML布局文件和Gallery控件在Android应用中实现图片轮播功能,包括自定义适配器和监听器,以及设置图片透明度和间距等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout
03. xmlns:android="http://schemas.android.com/apk/res/android"
04. android:orientation="vertical"
05. android:layout_width="wrap_content"
06. android:layout_height="wrap_content"
07. android:gravity="center_vertical"
08. android:layout_gravity="center">
09. <!--
10. android:unselectedAlpha 设置选中的图片的透明度
11. android:spacing 图片之间的空白大小
12. -->
13. <Gallery
14. android:id="@+id/gallery"
15. android:layout_width="fill_parent"
16. android:layout_height="fill_parent"
17. android:spacing="10dip"
18. android:unselectedAlpha="1" />
19.</LinearLayout>


import android.app.Activity;
04.import android.os.Bundle;
05.import android.view.View;
06.import android.view.ViewGroup;
07.import android.widget.AdapterView;
08.import android.widget.BaseAdapter;
09.import android.widget.Gallery;
10.import android.widget.ImageView;
11.import android.widget.AdapterView.OnItemClickListener;
12.
13.public class GalleryActivity extends Activity {
14.
15. int[] imgIds = {R.drawable.a, R.drawable.b,
16. R.drawable.c, R.drawable.d, R.drawable.e};
17. @Override
18. public void onCreate(Bundle savedInstanceState) {
19. super.onCreate(savedInstanceState);
20. setContentView(R.layout.gallery_layout);
21.
22. Gallery gallery = (Gallery) findViewById(R.id.gallery);
23. BaseAdapter adapter = new GalleryAdpter();
24. gallery.setAdapter(adapter);
25. OnItemClickListener listener = new OnItemClickListener() {
26.
27. /*
28. * parent 发生点击动作的AdapterView
29. * view 在AdapterView中被点击的视图(它是由adapter提供的一个视图)
30. * position 视图在adapter中的位置
31. * id 被点击元素的行id
32. */
33. @Override
34. public void onItemClick(AdapterView<?> parent, View view, int position,
35. long id) {
36. Gallery gallery = (Gallery) findViewById(R.id.gallery);
37. gallery.setSelection(position);
38. }
39. };
40.
41. gallery.setOnItemClickListener(listener);
42. }
43.
44. class GalleryAdpter extends BaseAdapter {
45.
46. @Override
47. public int getCount() {
48. return imgIds.length;
49. }
50.
51. @Override
52. public Object getItem(int position) {
53. return null;
54. }
55.
56. @Override
57. public long getItemId(int position) {
58. return 0;
59. }
60.
61. @Override
62. public View getView(int position, View convertView, ViewGroup parent) {
63. ImageView imageView = new ImageView(GalleryActivity.this);
64. imageView.setImageResource(imgIds[position]);
65. imageView.setScaleType(ImageView.ScaleType.FIT_XY);
66. return imageView;
67. }
68. }
69.}

01.<?xml version="1.0" encoding="utf-8"?>
02.<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03. package="com.mygallery.activity"
04. android:versionCode="1"
05. android:versionName="1.0">
06. <application android:icon="@drawable/icon" android:label="@string/app_name">
07. <activity android:name=".GalleryActivity"
08. android:label="@string/app_name">
09. <intent-filter>
10. <action android:name="android.intent.action.MAIN" />
11. <category android:name="android.intent.category.LAUNCHER" />
12. </intent-filter>
13. </activity>
14.
15. </application>
16. <uses-sdk android:minSdkVersion="8" />
17.</manifest>

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/jamesliulyc/archive/2011/04/18/6331873.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值