头像选择效果(ImageButton,Gallery,ImageSwitcher)
2012-10-06 17:26
387 查看
效果图
![](http://img.my.csdn.net/uploads/201210/06/1349515499_8950.png)
![](http://img.my.csdn.net/uploads/201210/06/1349515515_2169.jpg)
![](http://img.my.csdn.net/uploads/201210/06/1349515529_6937.png)
总体布局文件:
按下图像按钮后,弹出框的布局文件:
activity代码:
![](http://img.my.csdn.net/uploads/201210/06/1349515499_8950.png)
![](http://img.my.csdn.net/uploads/201210/06/1349515515_2169.jpg)
![](http://img.my.csdn.net/uploads/201210/06/1349515529_6937.png)
总体布局文件:
<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; } }
相关文章推荐
- android-gallery-头像选择器效果
- 学习Android之第七个小程序头像选择(自定义对话框、Gallery、ImageSwitcher)
- ImageSwitcher、Gallery、自定义对话框等UI组件的综合运用 头像选择
- Asp.net FileUpload+Image制作头像效果
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- ImageButton设置背景透明后,点击显示button按下效果
- android 用ImageSwitcher+Gallery实现图片浏览效果
- Asp.net FileUpload+Image制作头像效果示例代码
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法
- 【原创】Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- Android ImageButton自定义按钮的按下效果的代码实现方法分享
- [Android实例] Android ImageButton动画效果
- Android 笔记:ImageButton单击切换按钮图片效果的实现(转)
- ImageButton 实现 FloatingActionButton 波纹/表面高度效果
- 自定义组件---图片和文字实现ImageButton效果
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片
- Android——图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟、图片透明度、滚动和时间选择器
- iOS点击button放大后缩小效果,类似QQ、微信选择图片时的特效。
- (转)Android 模仿淘宝滑动查看图片的效果 Gallery + ImageSwitcher