android初体验-HelloGallery实例详解
2010-03-28 by zhiwei
自己学习android开发已经有三天了,收获还不少。现在在网上很难找到关于android应用开发的汉语书籍,于是不得不去阅读英文版的android developer docs,再一次体会到英语的重要性。由于自己学过java与java web,所以对于android的开发环境比较熟悉,很快就上手了。今天上午在练习android docs里面提供的HelloGallery例子时遇到了点问题,在网上查了很久,最终还是把问题解决了,我自己又在android所给的源码基础上加上了一个简单的功能。
Android docs提供的HelloGallery例子的程序不能通过编译,提示“android.R.styleable cannot be resolved”,我在网上查了一下,是因为sdk版本的问题,新版的android sdk已经不支持styleable了,按照这个地址上给出的方法改了一下还是不能通过,提示错误是“The method obtainStyledAttributes(int[]) is undefined for the type ImageAdapter”,最后在“xda-developers”论坛上找一了解决方法“you probably have ImageAdapter in a separate file and not as an inner class of HelloGallery,obtainStyledAttributes() is a method of the Context class”意思是obtainStyledAttributes()是Context类中的方法,如果将ImageAdapter单独写在一个.java文件中的话,必须在obtainStyledAttributes()方法前加上方法的引用才能解决问题。
最终问题是这样解决的:
1.在“res/values/”目录下新建“attars.xml”文件,写入以下代码并保存:
1 2 3 4 5 6 7 | <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Gallery"> <attr name="android:galleryItemBackground"> </attr> </declare-styleable> </resources> |
2.修改ImageAdapter.java里面的代码:
1 2 3 4 5 6 7 | public ImageAdapter(Context c) { mContext = c; TypedArray a = obtainStyledAttributes(android.R.styleable.Theme); mGalleryItemBackground = a.getResourceId( android.R.styleable.Theme_galleryItemBackground, 0); a.recycle(); } |
改为以下代码:
1 2 3 4 5 6 7 | public ImageAdapter(Context c) { mContext = c; TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery); mGalleryItemBackground = a.getResourceId( R.styleable.Gallery_galleryItemBackground, 0); a.recycle(); } |
现在再编译一下就通过了。
又看了这个地址上的例子后,我也改了一下HelloGallery程序源码,实现了相似功能,虽然实现了这些功能,但我对部分代码还是不很了解,现在贴出来和大家分享一下。
res/layout/main.xml :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget_absolutelayout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <!-- Gallery 屏幕上方显示一排图片,滚动显示 --> <ImageView android:id="@+id/view_photo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_y="100px" /> <!-- ImageView 屏幕下方显示选中的那张图片的大图 --> </AbsoluteLayout> |
res/values/attars.xml:
1 2 3 4 5 6 7 | <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Gallery"> <attr name="android:galleryItemBackground"> </attr> </declare-styleable> </resources> |
src包里的HelloGallery.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class HelloGallery extends Activity { private ImageView imageView; private ImageAdapter imageAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageAdapter = new ImageAdapter(this); imageView = (ImageView) findViewById(R.id.view_photo); Gallery g = (Gallery) findViewById(R.id.gallery); g.setAdapter(new ImageAdapter(this)); g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText( HelloGallery.this, "" + imageAdapter.mImageIds[position] + "this is " + position + " photo", Toast.LENGTH_SHORT) .show(); imageView.setBackgroundResource (imageAdapter.mImageIds[position]); } }); } } |
src包里的ImageAdapter.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; public Integer[] mImageIds = { R.drawable.budala, R.drawable.cloud, R.drawable.cloud_2, R.drawable.green, R.drawable.greenwater, R.drawable.hill, R.drawable.hillsunning, R.drawable.mountain, R.drawable.tibet, R.drawable.tree_water, R.drawable.water }; public ImageAdapter(Context c) { mContext = c; TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery); // obtainStyledAtrributes is a method of Context class,so add the c. // before this method mGalleryItemBackground = a.getResourceId( R.styleable.Gallery_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; } } |
效果见下图:对不起,由于flickr上的相册显示不出来,不能看图了。
最后我把自己已经改好的源代码导出来了,可以从这里下载。
另外,贪吃蛇的源码我也修改好了,可以从这里下载,下载完后直接导入你的eclipse中就可以运行了。
注:我用的android sdk 2.1版。可以在这里下载:http://dl.google.com/android/android-sdk_r05-windows.zip
PS:在Google China BLog上看到了google举办的暑期大学生博客大赛,于是就参加了。参赛标志写在这里:“首届 Google 暑期大学生博客分享大赛——2010 Andriod 篇”。
© 2010, chenzhiwei.net. 版权所有.
本文永久链接:http://chenzhiwei.net/2010/03/android-hellogallery-app/

Gallery图片浏览,怎么实现定时啊,或自动浏览图片。?
java coder 路过。。
又是个强人。学习。
潮人呀 我看好android
现在搞android人很多,也不须要什么成本。
nice
加油,android很可能做成像APP STORE的模式,到时候就赚钱了。
学着学着不知道再怎么下手了。