本文主要是介绍运用ImageButton编写选择头像小程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现下列图的效果:
首先,进行布局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"><ImageButtonandroid: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"><Galleryandroid:id="@+id/gallery"android:layout_width="fill_parent"android:layout_height="80dp"android:layout_centerHorizontal="true"android:layout_centerVertical="true"></Gallery><ImageSwitcherandroid: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;@Overridepublic 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() {@Overridepublic void onItemClick(AdapterView<?> adapter, View view,int position, long id) {num = position;imageSwitcher.setImageResource(pics[num]);}});imageSwitcher.setFactory(new ViewFactory() {@Overridepublic 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;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_select_image, menu);return true;}}
这篇关于运用ImageButton编写选择头像小程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!