运用ImageButton编写选择头像小程序

这篇博客展示了如何在Android应用中运用ImageButton组件来创建一个选择头像的对话框。通过布局activity_select_image.xml、dialog.xml以及在SelectImageActivity.java中的主要代码实现,实现了从图库或相机选取头像的功能。

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

实现下列图的效果:

 

首先,进行布局activity_select_image.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:background="#000000"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:contentDescription="@drawable/ic_launcher"
        android:src="@drawable/ic_launcher" />
    
    <EditText 
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"/>

</LinearLayout>

在添加一个xml文件(dialog. xml),代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">
    </Gallery>

    <ImageSwitcher
        android:id="@+id/imageswitch"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">
    </ImageSwitcher>

</RelativeLayout>


主要的Activity(SelectImageActivity.java)中代码如下:

package com.bzu.selectimage.activity;

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.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
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;

public class SelectImageActivity extends Activity {
	private ImageButton imageButton;
	private Gallery gallery;
	private ImageSwitcher imageSwitcher;
	// 定义图片数组
	private int[] pics;
	private int num;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_image);
		imageButton = (ImageButton) this.findViewById(R.id.image);
		pics = new int[] { R.drawable.star0, R.drawable.star1,
				R.drawable.star2, R.drawable.star3, R.drawable.star4,
				R.drawable.star5 };
		imageButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// Inflator在android中建立了资源文件到对象的桥梁
				LayoutInflater layoutInflater = LayoutInflater
						.from(SelectImageActivity.this);
				// 得到自定义的对话框
				View dialogView = layoutInflater.inflate(R.layout.dialog, null);
				AlertDialog.Builder dialog = new AlertDialog.Builder(
						SelectImageActivity.this);
				dialog.setTitle("请选择头像:");
				dialog.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								imageButton.setImageResource(pics[num]);
							}
						});
				dialog.setNegativeButton("取消",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {

							}
						});
				gallery = (Gallery) dialogView.findViewById(R.id.gallery);
				imageSwitcher = (ImageSwitcher) dialogView.findViewById(R.id.imageswitch);
				gallery.setAdapter(new Adapter(SelectImageActivity.this));
				gallery.setSelection(pics.length/2);
				gallery.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> adapter, View view,
							int position, long id) {
						num = position;
						imageSwitcher.setImageResource(pics[num]);
					}
				});
				imageSwitcher.setFactory(new ViewFactory() {

					@Override
					public View makeView() {
						ImageView imageView = new ImageView(
								SelectImageActivity.this);
						imageView.setLayoutParams(new ImageSwitcher.LayoutParams(80,80));// 设置显示图片的大小
						return imageView;
					}
				});
				dialog.setView(dialogView);
				dialog.create().show();
			}
		});
	}

	private class Adapter extends BaseAdapter {
		private Context context;

		public Adapter(Context context) {
			this.context = context;
		}

		public int getCount() {
			return pics.length;
		}

		public Object getItem(int position) {
			return pics[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(SelectImageActivity.this);
			imageView.setImageResource(pics[position]);
			// 设置图片大小自适应
			imageView.setAdjustViewBounds(true);
			imageView.setLayoutParams(new Gallery.LayoutParams(60, 60));// 设置显示图片的大小
			imageView.setPadding(15, 10, 15, 10);// 设置四边的距离
			return imageView;
		}

	}

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

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值