註冊對話框內更換新頭像

在登陸各種程序時,往往有很多帶頭像的。我們在選擇頭像時,看看在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]);

	}

    
}


這樣一個簡單的更換頭像就完成了。

提升空間是很大的,歡迎批評指正。奮鬥

已上傳資源,大家可以下載看看

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章