80,350
社区成员
发帖
与我相关
我的任务
分享
package com.android.jiashie;
import android.app.Activity;
import android.os.Bundle;
//使用Gallery widget所用到的包
import android.content.Context;
import android.widget.Gallery;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
import android.view.ViewGroup;
public class GalleryDemo extends Activity {
private TextView myText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView)findViewById(R.id.myText);
//Gallery ga = (Gallery)findViewById(R.id.myGallery);
((Gallery) findViewById(R.id.myGallery))
.setAdapter(new ImageAdaper(this));
}
public class ImageAdapter extends BaseAdapter{
private Context myContext;
//使用android.R.drawable中的图片作为图库源
private int[] myImageIDs = new int[] {
android.R.drawable.btn_minus,
android.R.drawable.btn_radio,
android.R.drawable.ic_lock_idle_alarm,
android.R.drawable.ic_menu_camera
};
//构造函数
public ImageAdapter(Context c)
{
this.myContext=c;
}
public int getCount()
{
// TODO Auto-generated method stub
return this.myImageIDs.length;
}
public Object getItem(int position)
{
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
//创建一个ImageView对象
ImageView iv = new ImageView(this.myContext);
iv.setImageResource(this.myImageIDs[position]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
//设置这个ImageView的宽高,单位为dip
iv.setLayoutParams(new Gallery.LayoutParams(120,120));
return iv;
}
//依据距离中央位置的偏移量,利用getScale返回views的大小(0.0f - 1.0f)
public float getScale(boolean focused,int offset) {
// formula: 1/(2^offset)
return Math.max(0, 1.0f/(float)Math.pow(2, Math.abs(offset)));
}
}
}