<?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="fitXY"></ImageButton> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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/img_gallery" android:layout_width="fill_parent" android:layout_height="110px" android:layout_marginTop="2px" android:layout_alignParentLeft="true"></Gallery> <ImageSwitcher android:id="@+id/img_switcher" android:layout_width="90px" android:layout_height="90px" android:layout_centerHorizontal="true" android:layout_below="@+id/img_gallery" ></ImageSwitcher> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="addContact_PleaseChooseImg">请选择图像</string> </resources>
package cn.moon.contact; 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.view.ViewGroup.LayoutParams; 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; publicclass AddContactActivity extends Activity { ImageButton ibtnImgChoose; AlertDialog imgChooseDialog; Gallery gallery; ImageSwitcher imageSwitcher; int selectedImage; privateint[] images = { R.drawable.png0001, R.drawable.png0002, R.drawable.png0003, R.drawable.png0004, R.drawable.png0005, R.drawable.png0006, R.drawable.png0007, R.drawable.png0008, R.drawable.png0009, R.drawable.png0010, }; publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addcontact); System.out.println("AddContact"); ibtnImgChoose = (ImageButton) findViewById(R.id.ibtnHead); ibtnImgChoose.setOnClickListener(new View.OnClickListener() { publicvoid onClick(View v) { initImageChoose(); imgChooseDialog.show(); } }); } privatevoid 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() { publicvoid onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) { imageSwitcher.setImageResource(images[arg2]); selectedImage = arg2; } publicvoid onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); AlertDialog.Builder builder =new AlertDialog.Builder(this); builder.setTitle("请选择图像"); builder.setPositiveButton("确认", new OnClickListener() { publicvoid onClick(DialogInterface dialog, int which) { ibtnImgChoose.setImageResource(images[selectedImage]); } }); builder.setNegativeButton("取消", new OnClickListener() { publicvoid onClick(DialogInterface dialog, int which) { } }); builder.setView(view); imgChooseDialog = builder.create(); } class imageAdapter extends BaseAdapter { private Context context; public imageAdapter(Context context) { super(); this.context = context; } publicint getCount() { return images.length; } public Object getItem(int arg0) { // TODO Auto-generated method stub returnnull; } publiclong getItemId(int position) { // TODO Auto-generated method stub return0; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView =new ImageView(context);// 构造一个ImageView imageView.setImageResource(images[position]);// 设置ImageView图片源 imageView.setAdjustViewBounds(true); imageView.setScaleType(ScaleType.FIT_XY);// 自适应高和宽 imageView.setLayoutParams(new Gallery.LayoutParams(80, 80));// 设置显示图处的大小 imageView.setPadding(10, 5, 10, 5);// 设置四边的距离 return imageView; } } 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; } } }