是用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. Androd之在图片右上角显示红色圆圈里面数字提醒
  2. tabcontent中scaleType的使用 图片Drawable和bitmap显示问题
  3. android图片浏览器 PhotoStore
  4. Android 图片裁剪之三剑式(二)
  5. Android利用setLayoutParams在代码中调整布局(Margin和居中)
  6. android 选择本地图片并截剪图片保存到,sdcard指定目录中
  7. Android非Kotlin支持项目添加Kotlin代码时的注意点
  8. Android获取View位置getHitRect、getDrawingRect、getLocalVisib

随机推荐

  1. Android无限循环ViewPager
  2. Android(安卓)用户界面---操作栏(Action B
  3. android装逼技术之暗码小DOME
  4. Android(安卓)自定义控件布局 NullPointe
  5. Android(安卓)Studio 提示与技巧(官方文档
  6. Android中px, dp, sp单位转换
  7. android 顶部Tab实现及原理
  8. 关于houdini技术和android x86平台兼容性
  9. 学习OpenGL ES for Android(十三)— 投光物
  10. 一场针对手机底层的新圈地运动(程苓峰)