Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/myTextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Hello"
android:gravity="center_vertical|center_horizontal" />
<Gallery
android:id="@+id/myGallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom" />
</LinearLayout>
MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
//
using Android.Graphics;
namespace Ex03_15
{
[Activity(Label = "Ex03_15", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
//int count = 1;
private TextView mTextView01;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
mTextView01 = (TextView) FindViewById(Resource.Id.myTextView01);
mTextView01.SetText(Resource.String.str_txt1,TextView.BufferType.Normal );
mTextView01.SetTextColor(Color.Blue);
((Gallery)FindViewById(Resource.Id.myGallery1)).Adapter = new ImageAdapter(this);
}
public class ImageAdapter : BaseAdapter
{
/* 类成员 myContext为Context父类 */
private Context myContext;
/*使用android.R.drawable里的图片作为图库来源,类型为整数数组*/
private int[] myImageIds = {
Resource.Drawable.Icon ,
Resource.Drawable.Icon1,
Resource.Drawable.Icon2 ,
Resource.Drawable.Icon3 ,
Resource.Drawable.Icon4 ,
Resource.Drawable.Icon5 ,
Resource.Drawable.Icon6,
Resource.Drawable.Icon7
};
/* 建构子只有一个参数,即要储存的Context */
public ImageAdapter(Context c)
{
this.myContext = c;
}
/* 回传所有已定义的图片总数量 */
public override int Count
{
get
{
return this.myImageIds.Length;
}
}
/* 利用getItem方法,取得目前容器中影像的数组ID */
public override Java.Lang.Object GetItem(int position)
{
return position;
}
public override long GetItemId(int position)
{
return position;
}
/* 取得目前欲显示的影像View,传入数组ID值使之读取与成像 */
public override View GetView(int position, View convertView, ViewGroup parent)
{
/* 建立一个ImageView对象 */
ImageView i = new ImageView(this.myContext);
//i.setImageResource(this.myImageIds[position]);
//i.setScaleType(ImageView.ScaleType.FIT_XY);
///* 设定这个ImageView对象的宽高,单位为dip */
//i.setLayoutParams(new Gallery.LayoutParams(120, 120));
i.SetImageResource(this.myImageIds[position]);
i.SetScaleType(ImageView.ScaleType.FitXy);
i.LayoutParameters=new Gallery.LayoutParams(120,120);
return i;
}
/*依据距离中央的位移量 利用getScale回传views的大小(0.0f to 1.0f)*/
public float GetScale(bool focused, int offset)
{
/* Formula: 1 / (2 ^ offset) */
return Math.Max(0,1.0f/(float)Math.Pow(2,Math.Abs(offset)));
}
}
}
}