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

本文介绍了一种在Android应用中实现图像选择器的方法。当点击图像按钮时,会弹出包含多个头像选项的对话框供用户选择。该实现利用了AlertDialog、Gallery和ImageSwitcher等组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

总体布局文件:

<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="horizontal" >

    <ImageButton
        android:id="@+id/image"
        android:layout_width="wrap_content"       
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"     
        android:ems="10"
        android:inputType="text" >
        <requestFocus />
    </EditText>

</LinearLayout>


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

<?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"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="110dp"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="2dp" >
    </Gallery>

    <ImageSwitcher
        android:id="@+id/switcher"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_below="@+id/gallery"
        android:layout_centerHorizontal="true" >
    </ImageSwitcher>

</RelativeLayout>

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;
	}

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值