Android拖动图片:
package com.ko8e;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter{
private Context context = null;
private int[] nImages = {
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5,
R.drawable.img6,
R.drawable.img7,
R.drawable.img8,
};
public ImageAdapter(Context c) {
context = c;
}
//获取图片的个数
public int getCount() {
int i = nImages.length;
return i;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
//给imageView设置资源
imageView.setImageResource(nImages[position]);
//设置布局图片的显示比例
imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));
//设置显示比例的类型
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}
}
Activity类:
package com.ko8e;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.Gallery);
g.setAdapter(new ImageAdapter(this));
g.setBackgroundResource(R.drawable.bg0);
//設置Gallery時間的監聽
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(MyActivity.this, "你选择了"+(position + 1)+ " 号图片", Toast.LENGTH_SHORT).show();
}
});
}
}
具体图片就不弄上来了。