在登陆各种程序时,往往有很多带头像的。我们在选择头像时,看看在android中是怎样实现的吧!
效果图:
选择成功则显示:
是不是很好看?那就来试试吧
一 首先先把布局文件设计好:
(1)主页面布局:
<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="#86f4ee"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/head"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/image0" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>
(2)对话框文件布局:
<?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" >
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#55000000"
android:spacing="10dp" />
</LinearLayout>
划线部分就是画廊功能的组件了,可以边浏览边选择哦!!
二 布局结束后就是实现方法文件了
MainActivity
public class MainActivity extends Activity implements OnItemClickListener {
private ImageButton avatar;
private Gallery gallery;
private int[] images = { R.drawable.a, R.drawable.b,
R.drawable.c, R.drawable.d,R.drawable.e ,R.drawable.d,R.drawable.f };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
avatar = (ImageButton) this.findViewById(R.id.head);
avatar.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LayoutInflater inflater = LayoutInflater
.from(MainActivity.this);
final View dialogView = inflater.inflate(R.layout.dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
gallery = (Gallery) dialogView.findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter());
gallery.setSelection(images.length / 2);
gallery.setOnItemClickListener(MainActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_dialer)
.setTitle("更换头像")
.setView(dialogView)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).create().show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 负责产生gallery中的图片
*/
private class ImageAdapter extends BaseAdapter {
private int mGalleryItemBackground;
public ImageAdapter(){
TypedArray typedArray=obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground=typedArray.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
typedArray.recycle();
}
// 返回图片的个数,比如你想得到图片的个数
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int position) {
return images[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(images[position]);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
public void onItemClick(AdapterView<?> adapterview, View view, int postion,long id) {
avatar.setImageResource(images[postion]);
}
}
这样一个简单的更换头像就完成了。
提升空间是很大的,欢迎批评指正。
已上传资源,大家可以下载看看