Android应用之更换头像
来源:互联网 发布:php curl 二进制数据 编辑:程序博客网 时间:2024/04/27 19:55
先来看一下运行图:
下面看一下具体的代码:
MainActivity.java
package cn.bzu.dialog_imageswitcher;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageButton;import android.widget.ImageView;public class MainActivity extends Activity{private ImageButton imageButton;private Gallery gallery;private ImageView imageView;private Integer[] mThumbIds = {R.drawable.aa, R.drawable.bb,R.drawable.cc, R.drawable.dd,R.drawable.ee, R.drawable.ff,R.drawable.gg};private Integer[] mImageIds = {R.drawable.cc, R.drawable.dd,R.drawable.ee, R.drawable.ff,R.drawable.gg };@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iniComponent();imageButton.setImageResource(R.drawable.cc);imageButton.setOnClickListener(new ImageButtonListener());}// 初始化组件private void iniComponent() {imageButton = (ImageButton) findViewById(R.id.imageButton);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;}// 创建适配器private class ImageAdapter extends BaseAdapter {private Context context;public ImageAdapter(Context c) {this.context = c;}public int getCount() {return mThumbIds.length;}public Object getItem(int position) {return position;}public long getItemId(int position) {return position;}// 获取视图public View getView(int position, View convertView, ViewGroup parent) {ImageView imageView = new ImageView(context);imageView.setImageResource(mThumbIds[position]);imageView.setAdjustViewBounds(true);imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));return imageView;}}private class ImageButtonListener implementsandroid.view.View.OnClickListener {public void onClick(View v) {AlertDialog.Builder imageSelect = new AlertDialog.Builder(MainActivity.this);LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);View galleryView = layoutInflater.inflate(R.layout.activity_gallery, null);gallery = (Gallery) galleryView.findViewById(R.id.gallery);gallery.setAdapter(new ImageAdapter(MainActivity.this));gallery.setOnItemClickListener(new ItemClickListener());imageSelect.setIcon(android.R.drawable.btn_star).setView(galleryView).setTitle("头像选择").setPositiveButton("确定", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {}}).create().show();}}private class ItemClickListener implements OnItemClickListener{public void onItemClick(AdapterView<?> parent, View view, final int position,long id) {AlertDialog.Builder imageSelect = new AlertDialog.Builder(MainActivity.this);LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);View layoutview = layoutInflater.inflate(R.layout.activity_image, null);imageView = (ImageView) layoutview.findViewById(R.id.imageView);imageView.setImageResource(mImageIds[position]);imageSelect.setView(layoutview).setPositiveButton("确定", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {imageButton.setImageResource(mThumbIds[position]);}}).create().show();}}}下面是布局文件:
activity_main.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="@drawable/gh" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_medium" android:text="@string/prompt" tools:context=".MainActivity" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ImageButton android:id="@+id/imageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/app_name" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="@string/info" > <requestFocus /> </EditText> </LinearLayout></LinearLayout>
activity_gallery.xml:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:spacing="2dp" /></LinearLayout>activity_image.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:contentDescription="@string/app_name" /></LinearLayout>
必要的需要在strings.xml中配置一下就OK了!
- Android应用之更换头像
- Android头像更换
- android更换头像问题
- Android更换头像上传
- Android更换头像功能实现
- Android实现点击更换头像
- 【Android】更换头像的实现
- Android更换头像功能实现
- Android实战一:更换头像
- Android中更换头像功能
- 更换头像
- Android开发实现用户头像更换
- Android开发实现用户头像更换
- Android中更换头像功能的实现
- 实现Android中更换头像功能
- PopupWindow全屏显示以及适配不同手机屏幕之 应用实例 更换头像,拍照,相册选取附带动画效果
- ANDROID应用性能优化之优化列表头像过度绘制
- Android之圆头像
- android有用的命令
- html5中manifest特性测试(一)
- WLAN AP模块WizFi630用户指南(一)
- 如何在 MAC 下面使用 Aircrack-ng?
- Appstore审核指南
- Android应用之更换头像
- 产品经理必读的九步法
- UVA 216
- YARN 框架源码分析
- POJ1009 Edge Detection 解题报告
- iptables从入门到精通
- linux命令学习(三)查找文件位置
- vim学习笔记
- Android ViewPager多页面滑动切换以及动画效果