AlertDialog Gallery ImageSwitcher LayoutInflater 的结合使用,弹出窗选择图片
userheadchoose.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery
android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="110px"
android:layout_alignParentLeft="true"
android:layout_marginTop="10px"></Gallery>
<ImageSwitcher
android:id="@+id/img_switcher"
android:layout_width="90px"
android:layout_height="90px"
android:layout_alignBottom="@+id/img_gallery"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
</ImageSwitcher>
</RelativeLayout>
addcontact.xml
<?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">
<ImageButton android:id="@+id/ibtnHead"
android:layout_height="60px"
android:layout_width="60px"
android:src="@drawable/icon"
android:scaleType="centerCrop">
</ImageButton>
</LinearLayout>
AddContactActivity.java
package cn.AddContactActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
public class AddContactActivity extends Activity {
ImageButton ibtnImgChoose;
AlertDialog imgChooseDialog;
Gallery gallery;
ImageSwitcher imageSwitcher;
int selectedImage;
private int[] images = { R.drawable.image0001, R.drawable.image0002,
R.drawable.image0003, R.drawable.image0004, R.drawable.image0005,
R.drawable.image0006, R.drawable.image0007, R.drawable.image0008,
R.drawable.image0009, R.drawable.image0010, };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcontact);
ibtnImgChoose = (ImageButton) findViewById(R.id.ibtnHead);
ibtnImgChoose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
initImageChoose();
imgChooseDialog.show();
}
});
}
/**
*
*/
private void initImageChoose() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.userheadchoose, null);
//查找控件
gallery = (Gallery) view.findViewById(R.id.img_gallery);
gallery.setAdapter(new imageAdapter(this));
gallery.setSelection(images.length / 2);
//查找控件
imageSwitcher = (ImageSwitcher) view.findViewById(R.id.img_switcher);
imageSwitcher.setFactory(new switcherFactory(this));
//图片的单击事件
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selectedImage = arg2;
imageSwitcher.setImageResource(images[arg2%images.length]);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择图像");
builder.setPositiveButton("确认", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ibtnImgChoose.setImageResource(images[selectedImage%images.length]);
}
});
builder.setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setView(view);
imgChooseDialog = builder.create();
}
/**
*
* @author Administrator
*
*/
class imageAdapter extends BaseAdapter {
private Context context;
public imageAdapter(Context context) {
super();
this.context = context;
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);// 构造一个ImageView
imageView.setImageResource(images[position%images.length]);// 设置ImageView图片源
imageView.setAdjustViewBounds(true);
imageView.setLayoutParams(new Gallery.LayoutParams(80, 80));// 设置显示图处的大小
imageView.setPadding(15, 10, 15, 10);// 设置四边的距离
return imageView;
}
}
/**
*
* @author Administrator
*
*/
class switcherFactory implements ViewFactory {
private Context context;
public switcherFactory(Context context) {
super();
this.context = context;
}
public View makeView() {
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(90, 90));
return imageView;
}
}
}