ImageSwitcher、Gallery、自定义对话框等UI组件的综合运用 头像选择

UI组件的综合运用

一、

1、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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <ImageButton  
        android:id="@+id/IB1"   
        android:layout_width="70dp"  
        android:layout_height="70dp"  
        android:onClick="click"/>  
      
    <EditText   
        android:layout_width="fill_parent"  
       android:layout_height="wrap_content"/> 

</LinearLayout>

2、xuanze.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/Ga1"  
        android:layout_width="fill_parent"  
        android:spacing="10dp"  
        android:layout_height="90dp" />  
  
    <ImageSwitcher  
        android:id="@+id/IS1"  
        android:layout_width="90dp"  
        android:layout_height="90dp"  
        android:layout_centerHorizontal="true">  
    </ImageSwitcher>  
  

    

</RelativeLayout>

3、MainActivity.java

package com.example.ui8;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
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 MainActivity extends Activity implements ViewFactory {

	ImageButton imageButton;
	Gallery gallery;
	ImageSwitcher imageSwitcher;

	int index;

	private final static Integer[] IMAGES = { R.drawable.p1, R.drawable.p2,
			R.drawable.p3, R.drawable.p4, R.drawable.p5,

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		imageButton = (ImageButton) findViewById(R.id.IB1);

		imageButton.setBackgroundResource(R.drawable.p1);

	}

	public void click(View v) {

		LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
		View view = layoutInflater.inflate(R.layout.xuanze, null);
		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

		imageSwitcher = (ImageSwitcher) view.findViewById(R.id.IS1);

		gallery = (Gallery) view.findViewById(R.id.Ga1);

		imageSwitcher.setFactory(MainActivity.this);

		gallery.setAdapter(new ImageAdapter(MainActivity.this));

		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				imageSwitcher.setImageResource(IMAGES[position]);
				index = position;

			}
		});

		builder.setTitle("图片选择").setView(view)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						imageButton.setBackgroundResource(IMAGES[index]);

					}

				});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});

		builder.show();

	}

	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		return new ImageView(this);
	}

	class ImageAdapter extends BaseAdapter {

		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 position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View v, ViewGroup vg) {
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(IMAGES[position]);
			imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));
			imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
			return imageView;
		}
	}

}


二、运行效果图

 
点击头像选择图片:
 
 




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值