您的位置:首页 > 移动开发 > Android开发

Android开发拖拉图片Gallery画廊组…

2013-12-30 20:42 302 查看




新建grid_layout.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="match_parent"
 
  android:orientation="vertical"
>
 
  
 
 
<ImageView 
 
     
android:id="@+id/img"
 
     
android:layout_width="wrap_content"
 
     
android:layout_height="wrap_content"
 
     
android:scaleType="center"/>

</LinearLayout>

main.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
 
 
xmlns:tools="http://schemas.android.com/tools"
 
  android:layout_width="match_parent"
 
 
android:layout_height="match_parent"
 
  android:orientation="vertical"
 
 
tools:context=".MainActivity" 
 
 
android:gravity="bottom">
 
 
<ImageSwitcher 
 
     
android:id="@+id/imageswitcher"
 
     
android:layout_width="fill_parent"
 
     
android:layout_height="wrap_content"/>
 
  
 
 
<Gallery 
 
     
android:id="@+id/myGallery"
 
     
android:layout_gravity="center_vertical"
 
     
android:layout_width="fill_parent"
 
     
android:layout_height="wrap_content"
 
     
android:spacing="3px"/>
 
  

</LinearLayout>

main.java

package
com.example.gallerydemo;

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.util.Log;
import
android.view.Menu;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.AdapterView.OnItemClickListener;
import
android.widget.ImageView;
import
android.widget.LinearLayout.LayoutParams;
import
android.widget.ViewSwitcher.ViewFactory;
import
android.widget.Gallery;
import
android.widget.ImageSwitcher;
import
android.widget.SimpleAdapter;
import
android.widget.TextView;

public class MainActivity
extends Activity {

private Gallery
myGallery=null;
private SimpleAdapter
simpleAdapter=null;
private ImageSwitcher
imageswitcher=null;
private
List<Map<String,Integer>>
list=new
ArrayList<Map<String,Integer>>();
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myGallery=(Gallery)
super.findViewById(R.id.myGallery);
this.initAdapter();
this.myGallery.setAdapter(this.simpleAdapter);
this.myGallery.setOnItemClickListener(new
OnItemClickListenerImp());
this.imageswitcher=(ImageSwitcher)
super.findViewById(R.id.imageswitcher);
this.imageswitcher.setFactory(new
ViewFactoryImp());
}
private void
initAdapter(){
//取得全部的属性
Field[]
fields=R.drawable.class.getDeclaredFields();
for(int
i=0;i<fields.length;i++){
//System.out.println(fields[i]);
if(fields[i].getName().startsWith("ispic_")){
Map<String,Integer>
map=new HashMap<String,
Integer>();
try {
//必须指定名称是img
map.put("img",
fields[i].getInt(R.drawable.class));
} catch (Exception e)
{
// TODO: handle
exception
}
this.list.add(map);
}
}
//如果使用SimpleAdapter需要自己定义内部的布局文件
this.simpleAdapter=new
SimpleAdapter(this,this.list , 
R.layout.grid_layout, 
new
String[]{"img"}, 
new
int[]{R.id.img});
}
private class
OnItemClickListenerImp implements OnItemClickListener{

public void
onItemClick(AdapterView<?> parent,
View view, int position,
long id) {
Map<String,Integer>
map=(Map<String, Integer>)
parent.getAdapter().getItem(position);
MainActivity.this.imageswitcher.setImageResource(map.get("img"));
}
}
private class ViewFactoryImp
implements ViewFactory{

public View makeView()
{
ImageView img=new
ImageView(MainActivity.this);
img.setBackgroundColor(0xFFFFFFFF);
img.setScaleType(ImageView.ScaleType.CENTER);
img.setLayoutParams(new
ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return img;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: