是用Android Gallery组建 ,对图片实现拖拽效果 首先在layout文件中拖入Grallery图片显示的组建
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery android:id="@+id/Gallery01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Gallery>
</LinearLayout>
Activity视图代码如下
GalleryActivity.java
package com.study.galleryTest;
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.OnItemLongClickListener;
public class GalleryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Gallery <--> ImageAdapter(BaseAdapter) <--> ImageView
Gallery g = (Gallery) this.findViewById(R.id.Gallery01);
g.setAdapter(new ImageAdapter(this));
g.setBackgroundResource(R.drawable.background);
// 设置事件监听
g.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> viewGroup, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(GalleryActivity.this, "图片编号为" + (position + 1),
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
代码主要是取得Gallery组建,设置了Gallery组建的Adapter和背景图片显示,
g.setAdapter(new ImageAdapter(this));用了自定义适配器,继承自BaseAdapter
ImageAdapter.java
package com.study.galleryTest;
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;
public ImageAdapter(Context c){
this.context = c;
}
//设置图片源-->整型数组
private Integer[] images =
{
R.drawable.android_001,
R.drawable.android_002,
R.drawable.android_003,
R.drawable.android_004,
R.drawable.android_005,
R.drawable.android_006,
R.drawable.android_007,
R.drawable.android_008,
R.drawable.android_009,
};
//获得图片个数
@Override
public int getCount() {
return images.length;
}
//获取图片在库中的位置
@Override
public Object getItem(int position) {
return position;
}
//获取图片在库中的位置
@Override
public long getItemId(int position) {
return position;
}
//获得单个的ImageView
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(images[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(85,85));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}
}