新建项目


② 定义layout外部resourcexml文件,用来改变layout的背景


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="Gallery">
  4. <attr name="android:galleryItemBackground" />
  5. </declare-styleable>
  6. <!-- 定义layout 外部resource 的xml 文件,用来改变layout 的背景图。 -->
  7. </resources>
复制代码


③ 修改main.xml布局,添加一个Gallery和一个ImageView

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout
  3. android:id="@+id/widget_absolutelayout"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. xmlns:android="http://schemas.android.com/apk/res/android"
  7. >
  8. <Gallery
  9. android:layout_width="fill_parent"
  10. android:layout_height="143px"
  11. android:layout_x="0px"
  12. android:layout_y="51px"
  13. android:id="@+id/Gallery_preView">
  14. </Gallery>
  15. <ImageView
  16. android:layout_width="239px"
  17. android:layout_height="218px"
  18. android:layout_x="38px"
  19. android:layout_y="184px"
  20. android:id="@+id/ImageView_photo">
  21. </ImageView>
  22. </AbsoluteLayout>
复制代码


④ 新建一个myImageAdapter--Gallery的适配器,它继承于BaseAdapter.

  1. package zyf.Ex_Ctrl_10ME;
  2. import android.view.View;
  3. import android.view.ViewGroup;
  4. import android.widget.BaseAdapter;
  5. public class myImageAdapter extends BaseAdapter {
  6. @Override
  7. public int getCount() {
  8. // TODO Auto-generated method stub
  9. return 0;
  10. }
  11. @Override
  12. public Object getItem(int position) {
  13. // TODO Auto-generated method stub
  14. return null;
  15. }
  16. @Override
  17. public long getItemId(int position) {
  18. // TODO Auto-generated method stub
  19. return 0;
  20. }
  21. @Override
  22. public View getView(int position, View convertView, ViewGroup parent) {
  23. // TODO Auto-generated method stub
  24. return null;
  25. }
  26. }
复制代码


⑤ 修改mainActivity.java,添加Gallery相关操作

  1. package zyf.Ex_Ctrl_10ME;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.AdapterView;
  6. import android.widget.Gallery;
  7. import android.widget.ImageView;
  8. import android.widget.Toast;

  9. public class Ex_Ctrl_10ME extends Activity {
  10. /** Called when the activity is first created. */
  11. /*定义要使用的对象*/
  12. private Gallery gallery;
  13. private ImageView imageview;
  14. private myImageAdapter imageadapter;
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. imageadapter=new myImageAdapter(this);
  20. /* 通过findViewById 取得 资源对象*/
  21. gallery=(Gallery)findViewById(R.id.Gallery_preView);
  22. imageview=(ImageView)findViewById(R.id.ImageView_photo);
  23. /*给Gallery设置适配器 把Ex_Ctrl_10ME类传入参数*/
  24. gallery.setAdapter(imageadapter);
  25. /*设置Gallery的点击事件监听器*/
  26. gallery.setOnItemClickListener(newGallery.OnItemClickListener(){
  27. @Override
  28. public void onItemClick(AdapterView<?> parent, View v, int position,
  29. long id) {
  30. // TODO Auto-generated method stub
  31. /*显示该图片是几号*/
  32. Toast.makeText(Ex_Ctrl_10ME.this,
  33. "这是图片:"+position+"号", Toast.LENGTH_SHORT).show();

  34. /*设置大图片*/
  35. imageview.setBackgroundResource(imageadapter.myImageIds[position]);
  36. }
  37. });
  38. }
  39. }
复制代码


⑥ 修改myImageAdapter.java文件,实现相簿浏览效果

  1. package zyf.Ex_Ctrl_10ME;

  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseAdapter;
  7. import android.widget.Gallery;
  8. import android.widget.ImageView;

  9. public class myImageAdapter extends BaseAdapter{//自定义的类变量
  10. /*变量声明*/
  11. int mGalleryItemBackground;
  12. private Context context;//上下文
  13. /* 构建一Integer array 并取得预加载Drawable 的图片id */
  14. public Integer[] myImageIds = { R.drawable.photo1, R.drawable.photo2,
  15. R.drawable.photo3, R.drawable.photo4, R.drawable.photo5,
  16. R.drawable.photo6, };
  17. /*自定义的构造方法*/
  18. public myImageAdapter(Context context) {
  19. // TODO Auto-generated constructor stub
  20. this.context=context;
  21. /*
  22. * 使用在res/values/attrs.xml 中的<declare-styleable>定义 的Gallery 属性.
  23. */
  24. TypedArray typed_array=context.obtainStyledAttributes(R.styleable.Gallery);
  25. /* 取得Gallery 属性的Index id */
  26. mGalleryItemBackground=typed_array.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
  27. /* 让对象的styleable 属性能够反复使用 */
  28. typed_array.recycle();
  29. }
  30. /* 重写的方法getCount,返回图片数目 */
  31. @Override
  32. public int getCount() {
  33. // TODO Auto-generated method stub
  34. return myImageIds.length;
  35. }
  36. /* 重写的方法getItemId,返回图像的数组id */
  37. @Override
  38. public Object getItem(int position) {
  39. // TODO Auto-generated method stub
  40. return position;
  41. }
  42. @Override
  43. public long getItemId(int position) {
  44. // TODO Auto-generated method stub
  45. return position;
  46. }
  47. /* 重写的方法getView,返回一View 对象 */
  48. @Override
  49. public View getView(int position, View convertView, ViewGroup parent) {
  50. // TODO Auto-generated method stub
  51. /* 产生ImageView 对象 */
  52. ImageView imageview = new ImageView(context);
  53. /* 设置图片给imageView 对象 */
  54. imageview.setImageResource(myImageIds[position]);
  55. /* 重新设置图片的宽高 */
  56. imageview.setScaleType(ImageView.ScaleType.FIT_XY);
  57. /* 重新设置Layout 的宽高 */
  58. imageview.setLayoutParams(new Gallery.LayoutParams(128, 128));
  59. /* 设置Gallery 背景图 */
  60. imageview.setBackgroundResource(mGalleryItemBackground);
  61. /* 返回imageView 对象 */
  62. return imageview;
  63. }
  64. }

复制代码

Android UI开发第七篇之Android Gallery_第1张图片

http://www.devdiv.com/home.php?mod=space&uid=14682&do=blog&id=3860

/**
* @author 张兴业
* 邮箱:xy-zhang@163.com
* qq:363302850
*
*/

更多相关文章

  1. android前台渲染图片
  2. android网络图片的下载
  3. Android Webview调用系统相册实现多选图片上传
  4. Android声音播放实例代码
  5. 2013.12.23 (2)——— android 代码调用shell
  6. Android判断横屏竖屏代码
  7. 代码中如何设置TextView为不可见

随机推荐

  1. Android应用开发相关下载资源(2016/07/24
  2. Android(安卓)5.0以上Button,ImageView自
  3. 演化理解 Android(安卓)异步加载图片
  4. activity的android:name 设置问题
  5. cmd>>android
  6. Android(安卓)ProgressDialog 最佳处理方
  7. android shape的使用
  8. Android:layout_weight详解
  9. Android(安卓)中Service生命周期
  10. 2011.09.22——— android ViewStub的简