是用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;
}

}

更多相关文章

  1. android 录音和播放
  2. Android实现动态显示或隐藏密码输入框的内容
  3. android 设置Button或者ImageButton的背景透明 半透明 透明
  4. Android(安卓)FactoryTest 流程
  5. android listview的一些设置
  6. tabcontent中scaleType的使用 图片Drawable和bitmap显示问题
  7. Android(安卓)Material Design 控件常用的属性
  8. Android(安卓)EditText属性大全
  9. android 为摄像头增加闪光灯(s5pv210)

随机推荐

  1. Android笔记 - Android启动之Linux内核启
  2. android线程池原理和实现
  3. android自定义ViewGroup基础
  4. Android之网络请求9————Retrofit的简
  5. android 2天
  6. Android项目优化宝典
  7. Android(安卓)studio 下的aidl编程实现An
  8. Android图灵聊天机器人-薇尔莉特
  9. Android 开发中Parcel存储类型和数据容器
  10. MUI在ios中的一些问题