用手机浏览靓照也是件非常惬意的事了,然而如何实现这一功能呢,其实也并不难,下面是一个简单的例子,功能为:主屏幕上显示用户选择的靓照,屏幕下面滚动显示靓照集,点击即可浏览。如下图所示:

实现代码为:(这里图片没法提供了,可以设置自己喜欢的图片,R.drawable.photo1为图片标志,这里图片存放在res\drawable目录下,图片名为photo1,我所建工程的包结构为:cn.com.pan,有两个Activity和对应的两个布局文件。)
-----------------------MainActivity ----------------------------------------
import android.widget.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
OnClickListener listener0 = null;
private Button button0;
private Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//单击按钮跳转到photoActivity
listener0 = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
PhotoActivity.class);
startActivity(intent);
}
};
button0 = (Button) findViewById(R.id.showbutton);
button0.setOnClickListener(listener0);
button1 = (Button) findViewById(R.id.exitbutton);
//单击退出
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
----------------------------------PhotoActivity ---------------------------------
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
public class PhotoActivity extends Activity implements ViewFactory,
OnItemSelectedListener {
ImageSwitcher mSwitcher;
private Integer[] mThumbIds = { R.drawable.photo1, R.drawable.photo2,
R.drawable.photo6,R.drawable.photo4, R.drawable.photo5,R.drawable.photo3};
private Integer[] mImageIds = { R.drawable.photo1, R.drawable.photo2,
R.drawable.photo6 ,R.drawable.photo4, R.drawable.photo5,R.drawable.photo3};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.showphoto);
mSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01);
//系统的anim中的fade_in.xml
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
//为缩略图浏览器指定一个适配器
g.setAdapter(new ImageAdapter(this));
//响应在缩略图列表上选中某个缩略图后的事件
g.setOnItemSelectedListener(this);
}
@SuppressWarnings("unchecked")
public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
}
@SuppressWarnings("unchecked")
public void onNothingSelected(AdapterView parent) {
}
@Override
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//getView方法动态生成一个 ImageView,然后利用setLayoutParams,setImageResource,setBackgroundResource
//分别设定图片大小、图片源文件和图片背景。当图片被显示到当前
//屏幕的时候,这个函数就会被自动回调来提供要显示的 ImageView
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.photo3);
return i;
}
}
}
--------------------------------main.xml----------------------------------------
<?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"
>
<Button android:id="@+id/showbutton"
android:text="查看图片"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:id="@+id/exitbutton"
android:text="退出"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
-------------------------------showphoto.xml----------------------------------------
<?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">
<ImageSwitcher android:id="@+id/ImageSwitcher01"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true">
</ImageSwitcher>
<Gallery android:id="@+id/gallery" android:background="#abcdef"
android:layout_width="fill_parent" android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" android:gravity="center_vertical"
android:spacing="15dp" />
</RelativeLayout>
--------------------------------AndroidManifest.xml-------------------------------------
<?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">
<ImageSwitcher android:id="@+id/ImageSwitcher01"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true">
</ImageSwitcher>
<Gallery android:id="@+id/gallery" android:background="#abcdef"
android:layout_width="fill_parent" android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" android:gravity="center_vertical"
android:spacing="15dp" />
</RelativeLayout>