运用ImageButton编写选择头像小程序
来源:互联网 发布:linux telnet root 编辑:程序博客网 时间:2024/05/19 17:58
实现下列图的效果:
首先,进行布局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;@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编写选择头像小程序
- 运用CreateProcess()编写小程序
- 头像选择效果(ImageButton,Gallery,ImageSwitcher)
- 实现简单的选择头像的小程序
- 学习Android之第七个小程序头像选择(自定义对话框、Gallery、ImageSwitcher)
- 头像选择
- 头像选择
- 头像选择
- CrackMe小程序编写
- 运用友元类编写程序,输出时间日期
- ImageButton组件的应用(更改头像)
- ImageSwitcher、Gallery、自定义对话框等UI组件的综合运用 头像选择
- Dictionary运用之通讯录小程序
- 小程序编写的美女
- 初学C++编写小程序
- 编写我的小程序
- shell脚本编写小程序
- 小程序开发中获取用户头像和昵称
- 杂1
- 视频笔记1
- hdu 4289 Control
- 关于set_new_handler
- 坚持
- 运用ImageButton编写选择头像小程序
- Oracle的逻辑结构(表空间、段、区间、块)——Oracle数据块
- hdu 4288 Coder
- PS CS4抽出滤镜抠图小技巧
- BigBlueButton mysql修改为postgresql or oracle
- 真彩色制式下IplImage转成CBitmap格式
- 作业、进程、线程、管程、管道概念梳理
- ATL Internals 2ed复习.chapter 4.Thread
- extern "C"