android手机中图片的拖拉及浏览功能
2013-06-27 19:40
357 查看
配置文件
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Gallery
android:id="@+id/gallery"
android:gravity="center_horizontal"
android:spacing="3px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
img_data.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"/>
</LinearLayout>
程序文件
MainActivity.java
package com.example.simpleadaptergalleryproject;import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
private Gallery gallery=null;
private List<Map<String,Integer>> list=new ArrayList<Map<String,Integer>>();
private SimpleAdapter simpleAdapter=null;
private ImageSwitcher imageSwitcher=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.gallery=(Gallery)super.findViewById(R.id.gallery);//取得资源ID
this.imageSwitcher=(ImageSwitcher)super.findViewById(R.id.imageSwitcher);//取得资源ID
this.imageSwitcher.setFactory(new ViewFactorylmpl());//设置转换工厂
this.initAdapter();//初始化适配器
this.gallery.setAdapter(this.simpleAdapter); //设置图片集
this.gallery.setOnItemClickListener(new OnItemClickListenerlmpl());//设置事件
}
private class ViewFactorylmpl implements ViewFactory{
@Override
public View makeView() {
ImageView image=new ImageView(MainActivity.this);
image.setBackgroundColor(0xFFFFFFFF);
image.setScaleType(ImageView.ScaleType.CENTER);
image.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return image;
}
}
private class OnItemClickListenerlmpl implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//Toast.makeText(MainActivity.this,"一直都很帅,从未被超越",Toast.LENGTH_LONG).show();
Map<String,Integer> map=(Map<String,Integer>)MainActivity.this.simpleAdapter.getItem(arg2);
MainActivity.this.imageSwitcher.setImageResource(map.get("img"));
}
}
private void initAdapter(){//初始化适配器
Field[] field=R.drawable.class.getDeclaredFields();//取得全部属性
for (int i = 0; i < field.length; i++) {
if(field[i].getName().startsWith("ispic_")){ //找到所有以ispic_命名的图片
Map<String,Integer> map=new HashMap<String,Integer>();
try{
map.put("img", field[i].getInt(R.drawable.class)); //设置图片资源
}catch(Exception e){
}
this.list.add(map); //保存图片资源
}
}
this.simpleAdapter=new SimpleAdapter(MainActivity.this,
MainActivity.this.list,//要包装的数据集合
R.layout.img_data, //要使用的资源模板
new String[]{"img"},//要显示map中的Key
new int[]{R.id.img});//与模板中的组建向匹配
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
相关文章推荐
- android 图库浏览的功能有没有谁做过。主要是拿到图片和视频后排序的问题。
- Android实现图片预览浏览功能
- Android应用中图片浏览时实现自动切换功能的方法详解
- android 图库浏览的功能有没有谁做过。主要是拿到图片和视频后排序的问题。
- 【Android开发】图片拖拉功能的实现
- Android 使用ViewPager结合PhotoView开源组件实现网络图片在线浏览功能
- Android 使用ViewPager结合PhotoView开源组件实现网络图片在线浏览功能
- Android 图片浏览功能 图片放大缩小
- android手机cs端图片预览功能说明
- Android 图片浏览功能简单实现(画廊效果实现,支持放大缩小)
- 调用Android手机相册功能并获取图片路径
- 【android】给大家分享个小功能,关于低版本安卓手机拍照找不到图片的问题
- android 图库浏览的功能有没有谁做过。主要是拿到图片和视频后排序的问题。
- Android SmartImageView插件实现图片浏览功能
- Android手机图片居中滑动浏览HorizontalScrollView
- Android实现网页图片浏览功能
- Android实现对图片的浏览功能
- Android实现点击WebView界面中图片滑动浏览与保存图片功能
- android 图片浏览功能 图片放大缩小 使用 photoview 双击或双指缩放的ImageView
- Android实现网络图片浏览功能