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;
}
}
}
相关文章推荐
- android:scaleType的使用
- android中的 gravity 和…
- Android开发拖拉图片Gallery画廊组…
- Android获取当前时间
- Android开发之文本切换组件TextSwi…
- Android开发之ImageSwitcher组件的…
- Android开发之Toast组件
- Android开发之自定义评分组件
- @+id以及@id和android:id的区别
- Android当中layer-list使用
- Android开发拖动条实现照片的切换
- Android设置TextView能够滚动
- Android开发拖动条SeekBar的使用
- Android开发之环形进度条(安卓默认…
- android中LayoutInflater的使用
- Android输入密码密文显示
- Android开发对话框之多选对话框的…
- Android开发对话框之单选对话框的…
- Android开发之列表对话框的使用
- Android开发之对话框事件之点击手…