Mono for Andrid学习笔记(八)—— 如IPhone拖动相片特效,Gallery画廊_第1页
Mono for Andrid学习笔记(八)—— 如IPhone拖动相片特效,Gallery画廊_第2页
Mono for Andrid学习笔记(八)—— 如IPhone拖动相片特效,Gallery画廊_第3页
Mono for Andrid学习笔记(八)—— 如IPhone拖动相片特效,Gallery画廊_第4页
Mono for Andrid学习笔记(八)—— 如IPhone拖动相片特效,Gallery画廊_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、曾经被IPhone用手指在屏幕上滑来滑去拖动图片的操作方式吸引吗?在Android里,这并不是什么难事,只要用到Gallery即可。它通常用在设计相册、图片类型的选择器上。在开始之前,先了解一下什么是Context以及Android.Widget命名空间下的BaseAdapter,在Activity当中,Context就如同是Canvas画布,随时等着被处理或覆盖。还记得先前介绍Activity之间的传递时,做过Intent以及Context的应用吗?当中Intent是Android.Content命名空间下的一个类,而Context也是一样。本范例在Layout里布局一个Gallery对象,

2、再通过BaseAdapter容器存放Gallery所需要的图片。先将你要显示的图片放到Drawable文件夹下,然后编译一下工程,使其能在代码中使用。Main.axml的结构很简单,只要放上一个Gallery即可:+ expand source?12345678 本范例的另一个重点,就是如何设置Gallery图片的宽高以及放置图片Layout的大小,在此我们新建一个ImageAdapter类来继承BaseAdapter容器来存放图片,通过ImageView的SetScaleType方法来改变图片的显示,再通过LayoutParameters属性类改变Layout的宽高。我们先在Values目录

3、下新建一个attrs.xml文件,这是一个自制Layout元素的用法,在其中定义标签,目的是自定义layout的背景风格,并且通过Android.Content.Res.TypedArray类的特性,让相同的Layout元素可以重复用于每一张图片。+ expand source?123456 ImageAdapter类的结构如下:+ expand source?0102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859

4、606162public class ImageAdapter : BaseAdapter private Context ctx; int mGalleryItemBackground; private int images = Resource.Drawable.btn1, Resource.Drawable.btn1_b, Resource.Drawable.btn2, Resource.Drawable.btn2_b, Resource.Drawable.btn3, Resource.Drawable.btn3_b, Resource.Drawable.btn4, Resource.D

5、rawable.btn4_b ; public ImageAdapter(Context ctx) try this.ctx = ctx; Android.Content.Res.TypedArray a = ctx.ObtainStyledAttributes(Resource.Styleable.Gallery); mGalleryItemBackground = a.GetResourceId(Resource.Styleable.Gallery_android_galleryItemBackground, Android.Graphics.Color.Azure); a.Recycle

6、(); catch (System.Exception ex) MessageBox.ShowErrorMessage(ctx, ex); public override int Count get return images.Length; public override Java.Lang.Object GetItem(int position) return position; public override long GetItemId(int position) return position; public override View GetView(int position, V

7、iew convertView, ViewGroup parent) try ImageView v = new ImageView(this.ctx); v.SetImageResource(this.imagesposition); v.SetScaleType(ImageView.ScaleType.FitXy); v.LayoutParameters = new Gallery.LayoutParams(100, 50); v.SetBackgroundResource(mGalleryItemBackground); return v; catch (System.Exception

8、 ex) MessageBox.ShowErrorMessage(this.ctx, ex); return null; Activity1.cs的代码:+ expand source?010203040506070809101112131415161718Activity(Label = MonoDroidTest, MainLauncher = true) public class Activity1 : Activity protected override void OnCreate(Bundle bundle) base.OnCreate(bundle); SetContentVie

9、w(Resource.Layout.Main); try Gallery mGallery = FindViewById(Resource.Id.gallery); mGallery.Adapter = new ImageAdapter(this); catch (System.Exception ex) MessageBox.ShowErrorMessage(this, ex); 运行程序,效果如下:接下来的问题就是,我们能不能读取我们SD卡上的图片,而不是直接把图片作为资源打到包里啊?这当然是可以的,下面我们把ImageAdapter改写一下,从下面的例子,我们还可以学到如何读取SD卡的文

10、件(记得using Java.IO;):+ expand source?0102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071public class ImageAdapter : BaseAdapter private Context ctx; int mGalleryItemBackground; File files; int width, height; pu

11、blic ImageAdapter(Context ctx) try this.ctx = ctx; DisplayMetrics dm = new DisplayMetrics(); (Activity)this.ctx).WindowManager.DefaultDisplay.GetMetrics(dm); width = dm.WidthPixels; height = dm.HeightPixels; if (Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaMounted)

12、File path = Android.OS.Environment.ExternalStorageDirectory; File file = new File(path.Path + /Pictures/Camera); if (file.Exists() files = file.ListFiles(); else throw new System.Exception(SD卡中不存在此路径); else if (Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaRemoved) t

13、hrow new System.Exception(没有SD卡); Android.Content.Res.TypedArray a = ctx.ObtainStyledAttributes(Resource.Styleable.Gallery); mGalleryItemBackground = a.GetResourceId(Resource.Styleable.Gallery_android_galleryItemBackground, Android.Graphics.Color.Azure); a.Recycle(); catch (System.Exception ex) Mess

14、ageBox.ShowErrorMessage(ctx, ex); public override int Count get if (files = null) return 0; return this.files.Length; public override Java.Lang.Object GetItem(int position) return position; public override long GetItemId(int position) return position; public override View GetView(int position, View

15、convertView, ViewGroup parent) try ImageView v = new ImageView(this.ctx); Bitmap bmp = BitmapFactory.DecodeFile(filesposition.Path); v.SetImageBitmap(bmp); v.SetScaleType(ImageView.ScaleType.FitXy); v.LayoutParameters = new Gallery.LayoutParams(width, height); v.SetBackgroundResource(mGalleryItemBac

16、kground); return v; catch (System.Exception ex) MessageBox.ShowErrorMessage(this.ctx, ex); return null; 然后我们在增加一点小功能,就是在点击图片的时候,弹出个小提示,提示你点击的是哪张图片。这里用到了Toast对象,其实Toast对象我们在第四篇文章介绍布局的时候就已经用过了,但没有具体介绍过它,Toast是Android专属的提示小对象,它的使用方法相当简单,但用途却很广泛。基本上,Toast就是一个简短的小信息,将要告诉用户的信息以一个浮动在最上层的View显示,显示Toast之后,静待

17、几秒就会自动消失,最常见的应用就是音量大小的调整。当单击音量调整按钮之后,会看见跳出的音量指示Toast对象,等待调整完之后便会消失。+ expand source?123456Gallery mGallery = FindViewById(Resource.Id.gallery); mGallery.Adapter = new ImageAdapter(this); mGallery.ItemClick += (sender, e) = Toast.MakeText(this, string.Format(你点击的是0号图片, e.Position), ToastLength.Short).

18、Show(); ;运行后的效果:Toast显示后会在一定时间内消失,在Toast.MakeText方法的第三个参数是一个ToastLength枚举,颗选择Long或Short,前者时间较长,后者较短。当然,你也可以使用重写Toast对象的方法,自定义Toast显示的Layout,以不同于系统内置的方式显示Toast对象,如要在Toast里显示图片,方式如下:+ expand source?0102030405060708091011Gallery mGallery = FindViewById(Resource.Id.gallery); mGallery.Adapter = new ImageAdapter(this); mGallery.ItemClick += (sender, e) = Toast t = new Toast(this); ImageView img = new ImageView(this); img.SetImageResource(Android.Resource.Drawable.IcMenuCamera); t.View = img; t.Show(); /Toast.MakeText(this, stri

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论