头像选择效果(ImageButton,Gallery,ImageSwitcher)

效果图

总体布局文件:



    

    
        
    


按下图像按钮后,弹出框的布局文件:




    
    

    
    

activity代码:

package cn.bzu.display;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
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.ViewSwitcher.ViewFactory;
import android.support.v4.app.NavUtils;

public class DisplayActivity extends Activity {
	private ImageButton imageButton;//图像按钮
	private int[] images = { R.drawable.a, R.drawable.b, R.drawable.c,
			R.drawable.d, R.drawable.e };//头像图片数组
	private AlertDialog imageChooseDialog;//自定义对话框
	private Gallery gallery;
	private ImageSwitcher imageSwitcher;
	private int imagePosition;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display);
		imageButton = (ImageButton) this.findViewById(R.id.image);
		imageButton.setBackgroundResource(R.drawable.ic_launcher);
		imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				initImageChooseDialog();
				imageChooseDialog.show();
			}
		});
	}

	private void initImageChooseDialog() {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
		alertDialog.setTitle("请选择你的头像");
		alertDialog.setPositiveButton("确定",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						imageButton.setImageResource(images[imagePosition
								% images.length]);
					}
				});
		alertDialog.setNegativeButton("取消",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
		LayoutInflater inflater = LayoutInflater.from(this);
		// 通过布局文件生成view,并在后面通过.setView()方法赋给alertDialog
		View view = inflater.inflate(R.layout.imageswitch, null);
		// 设置gallery
		gallery = (Gallery) view.findViewById(R.id.gallery);
		gallery.setAdapter(new ImageAdapter(this));// 设置适配器
		gallery.setSelection(images.length / 2);// 设置默认图片
		imageSwitcher = (ImageSwitcher) view.findViewById(R.id.switcher);
		// 设置gallery选择事件,通过imageSwitcher来完成选择
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView parent, View view,
					int position, long id) {
				imagePosition = position;// 将选定的位置保存起来,用于设定头像
				imageSwitcher.setImageResource(images[position]);
			}

			@Override
			public void onNothingSelected(AdapterView arg0) {

			}
		});
		imageSwitcher.setFactory(new MyViewFactory());// 为imageSwitcher设置自定义Factory
		alertDialog.setView(view);
		imageChooseDialog = alertDialog.create();

	}

	// 自定义MyViewFactory
	private class MyViewFactory implements ViewFactory {
		@Override
		public View makeView() {
			ImageView iv = new ImageView(DisplayActivity.this);
			iv.setLayoutParams(new ImageSwitcher.LayoutParams(60, 60));// 设置图片选择的大小
			return iv;
		}
	}

	// 自定义ImageAdapter
	private class ImageAdapter extends BaseAdapter {
		private Context context;
		public ImageAdapter(Context context) {
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return images.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return images[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView iv = new ImageView(DisplayActivity.this);
			iv.setImageResource(images[position]);
			iv.setAdjustViewBounds(true);
			iv.setLayoutParams(new Gallery.LayoutParams(80, 80));// 设置gallery显示的图片的大小
			iv.setPadding(15, 10, 15, 10);
			return iv;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_display, menu);
		return true;
	}

}


 

你可能感兴趣的:(头像选择效果(ImageButton,Gallery,ImageSwitcher))