Android应用开发控件——Gallery和ImageSwitcher
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="4">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<ImageButton
android:id="@+id/btn_img"
android:layout_width="60px"
android:layout_height="60px"
android:scaleType="centerCrop"
android:src="@drawable/icon"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et_nama"
android:gravity="top"
android:layout_gravity="center_vertical"
android:hint="姓名"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="手机"
android:textSize="20dp"></TextView>
<EditText
android:id="@+id/et_mobilephone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:phoneNumber="true"
android:hint="手机"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="办公室电话"
android:textSize="20dp"></TextView>
<EditText
android:id="@+id/et_officephone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:phoneNumber="true"
android:hint="办公室电话"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="家庭电话"
android:textSize="20dp"></TextView>
<EditText
android:id="@+id/et_homephone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:phoneNumber="true"
android:hint="家庭电话"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="职务职称"
android:textSize="20dp"></TextView>
<EditText
android:id="@+id/et_position"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="职务职称"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="单位名称"
android:textSize="20dp"></TextView>
<EditText
android:id="@+id/et_company"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="单位名称"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="地址"
android:textSize="20dp"></TextView>
<EditText
android:id="@+id/et_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="地址"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="邮政编码"
android:textSize="20dp"></TextView>
<EditText
android:id="@+id/et_ripcode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:phoneNumber="true"
android:hint="邮政编码"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="E—Mail"
android:textSize="20dp"></TextView>
<EditText
android:id="@+id/et_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="E—Mail"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="其他联系方式"
android:textSize="20dp"></TextView>
<EditText
android:id="@+id/et_other"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="其他联系方式"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="备注"
android:textSize="20dp"></TextView>
<EditText
android:id="@+id/et_postcode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="备注"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="保存"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_return"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="返回"
android:layout_height="wrap_content"/>
</LinearLayout>
</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">
<Gallery
android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="110px"
android:layout_alignParentLeft="true"
android:layout_marginTop="10px"></Gallery>
<ImageSwitcher
android:id="@+id/img_switcher"
android:layout_width="90px"
android:layout_height="90px"
android:layout_alignBottom="@+id/img_gallery"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
</ImageSwitcher>
</RelativeLayout>
package org.ContactProject;
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.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;
public class AddNewActivity extends Activity{
private int [] images={ R.drawable.image1,R.drawable.image2,
R.drawable.image3,R.drawable.image4,
R.drawable.image5,R.drawable.image6,
R.drawable.image7,R.drawable.image8,
R.drawable.image9,R.drawable.image10,
R.drawable.image11,R.drawable.image12,
R.drawable.image13,R.drawable.image14,
R.drawable.image15,R.drawable.image16,
R.drawable.image17,R.drawable.image18,
R.drawable.image19,R.drawable.image20,
R.drawable.image21,R.drawable.image22,
R.drawable.image23,R.drawable.image24,
R.drawable.image25,R.drawable.image26,
R.drawable.image27,R.drawable.image28,
R.drawable.image29,R.drawable.image30};
private ImageButton btn_img;
private AlertDialog imageChooseDialog;
private Gallery gallery;
private ImageSwitcher imageSwitcher;
private int imageposition;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
btn_img=(ImageButton)findViewById(R.id.btn_img);
btn_img.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
initImageChooseDialog();
imageChooseDialog.show();
}});
}
private void initImageChooseDialog()
{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("请选择头像");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
btn_img.setImageResource(images[imageposition]);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
LayoutInflater flater=LayoutInflater.from(this);
View view=flater.inflate(R.layout.imageswich, null);
gallery=(Gallery) view.findViewById(R.id.img_gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setSelection(images.length/2);
imageSwitcher=(ImageSwitcher) view.findViewById(R.id.img_switcher);
gallery.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
imageposition=arg2;
imageSwitcher.setImageResource(images[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
imageSwitcher.setFactory(new MyViewFactory(this));
builder.setView(view);
imageChooseDialog=builder.create();
}
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 null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv=new ImageView(context);
iv.setImageResource(images[position]);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new Gallery.LayoutParams(80,80));
iv.setPadding(15, 10, 15, 10);
return iv;
}}
class MyViewFactory implements ViewFactory{
private Context context;
public MyViewFactory(Context context)
{
this.context=context;
}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView iv=new ImageView(context);
iv.setLayoutParams(new ImageSwitcher.LayoutParams(90,90));
return iv;
}}
}