`
bcyy
  • 浏览: 1824062 次
文章分类
社区版块
存档分类
最新评论

Android应用开发之电子相册

 
阅读更多

在iphone手机上用手指拖动图片移动,这功能很Cool,咱Android也不能含糊,用Gallery类就可以实现这个功能。

今天我就做了个小小的电子相册:

假设你已经新建好了项目。

首先我们事先准备好的图片存放在drawable文件夹下,然后新建一个接口:

  1. publicinterfaceImageResource{
  2. //用一个Integer数组保存图像资源
  3. Integer[]dImageID={
  4. R.drawable.sample_0,
  5. R.drawable.sample_1,
  6. R.drawable.sample_2,
  7. R.drawable.sample_3,
  8. R.drawable.sample_4,
  9. R.drawable.sample_5,
  10. R.drawable.sample_6,
  11. R.drawable.sample_7,
  12. };
  13. }

这个接口里有一个int型的数组保存了咱drawable下的图片,这样方便以后修改。

要用Gallery实现这个功能,先要有一个容器来存放Gallery要显示的图片,我使用的是一个继承自BaseAdapter的ImageAdapter。这个电子相册分两部分,上部分是用户可以拖动图像列表,下部分是用户选中上面图像后更大的显示出来。


负责上部分显示的ImageView是imageAll,负责下部分显示的是imageOne,代码如下:

  1. importandroid.app.Activity;
  2. importandroid.content.Context;
  3. importandroid.content.res.TypedArray;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.view.ViewGroup;
  7. importandroid.widget.BaseAdapter;
  8. importandroid.widget.ImageView;
  9. importandroid.widget.Gallery;
  10. publicclassHelloGalleryextendsActivity{
  11. //这个ImageView是用来显示单个图像
  12. privateImageViewimageOne;
  13. //这个ImageView是用来显示所有图像
  14. privateImageViewimageAll;
  15. @Override
  16. publicvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. imageOne=(ImageView)findViewById(R.id.imageView);
  20. //新建一个Gallery类,这是是实现拖动效果的关键
  21. //Galleryis一个用来水平卷动显示对象的视图
  22. Gallerygallery=(Gallery)findViewById(R.id.gallery);
  23. //ImageAdapter就是gallery要显示的对象
  24. gallery.setAdapter(newImageAdapter(this));
  25. }
  26. //实现ImageResource接口获得我自己创建的图像资源
  27. classImageAdapterextendsBaseAdapterimplementsImageResource{
  28. //每一个gallery中图像的背景资源
  29. privateintgalleryItemBackground;
  30. privateContextcontext;
  31. publicImageAdapter(Contextcontext){
  32. this.context=context;
  33. //这里实现的功能就是上半部分每个图像的那个背景框
  34. //对应的xml文件就是attr.xml
  35. TypedArraya=obtainStyledAttributes(R.styleable.Gallery1);
  36. galleryItemBackground=a.getResourceId(
  37. R.styleable.Gallery1_android_galleryItemBackground,0);
  38. a.recycle();
  39. }
  40. publicintgetCount(){
  41. returndImageID.length;
  42. }
  43. publicObjectgetItem(intposition){
  44. returnposition;
  45. }
  46. //这个方法获得是呈现在用户面前的图像下标
  47. publiclonggetItemId(intposition){
  48. //将此索引的图像设为imageOne显示imageOne.setImageResource(ImageAdapter.dImageID[position]);
  49. imageOne.setScaleType(ImageView.ScaleType.FIT_CENTER);
  50. returnposition;
  51. }
  52. //这个方法返回的ImageView就是实现拖动效果的图像
  53. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  54. imageAll=newImageView(context);
  55. //设置图像资源
  56. imageAll.setImageResource(dImageID[position]);
  57. //设置imageAll视图为120×120
  58. imageAll.setLayoutParams(newGallery.LayoutParams(120,120));
  59. //设置图像相对于视图的比例,FIT_XY表示充满X和Y轴
  60. imageAll.setScaleType(ImageView.ScaleType.FIT_XY);
  61. //设置imageAll中每一个Item的背景资源
  62. imageAll.setBackgroundResource(galleryItemBackground);
  63. returnimageAll;
  64. }
  65. }
  66. }

布局文件如下:

Mail.xml:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. />
  12. <Galleryandroid:id="@+id/gallery"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content">
  15. </Gallery>
  16. <ImageViewandroid:id="@+id/imageView"
  17. android:layout_height="fill_parent"
  18. android:layout_width="fill_parent">
  19. </ImageView>
  20. </LinearLayout>

下面就是上文中提到的attr.xml:

Attr.xml:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <declare-styleablename="Gallery1">
  4. <attrname="android:galleryItemBackground"/>
  5. </declare-styleable>
  6. <declare-styleablename="LabelView">
  7. <attrname="text"format="string"/>
  8. <attrname="textColor"format="color"/>
  9. <attrname="textSize"format="dimension"/>
  10. </declare-styleable>
  11. </resources>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics