第一章:初入Android大門(Gallery拖動相片特效)

效果:


[img]http://dl.iteye.com/upload/attachment/382581/5982447c-9126-3690-8b19-3a0d21a45e5b.jpg[/img]

main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<Gallery
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="43px"
android:layout_y="142px"
>
</Gallery>
</AbsoluteLayout>





package gallery.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** 獲得 Gallery對象組件*/
((Gallery)findViewById(R.id.image)).setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter{
private Context myContext;
/** 定義圖片*/
private int[] myImages={
android.R.drawable.btn_minus,
android.R.drawable.btn_radio,
android.R.drawable.ic_lock_idle_low_battery,
android.R.drawable.ic_menu_camera,
};
/** 存儲Context*/
public ImageAdapter(Context c){
this.myContext=c;

}
/** 獲得圖片數量*/
public int getCount() {
// TODO Auto-generated method stub
return this.myImages.length;
}
/** 獲得目前圖像數組ID*/
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/**取得顯示圖像View,傳入數組ID值讀取數組圖像*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/**實例化ImageView對象*/
ImageView i=new ImageView(this.myContext);
/**設置圖片*/
i.setImageResource(this.myImages[position]);
/**設置View的大小*/
i.setScaleType(ImageView.ScaleType.FIT_XY);
/**設置ImageView對象寬度和高度*/
i.setLayoutParams(new Gallery.LayoutParams(120,120));
return i;
}
/**距離中央距離位移梁 利用getScale返回View大小*/
public float getScale(boolean flg,int offset){
return Math.max(0,1.0f/(float)Math.pow(2, Math.abs(offset)));
}

}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章