这是我学习Android时做的一个小程序,程序主要功能是实现一个迷你相册的功能,可以在虚拟机上看到很不错的效果。

我设置屏幕的大小为800*600

/*

*ImageSwitcherGallery.java

*/

package android.study_layout;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitcherGallery extends Activity implements
OnItemSelectedListener, ViewFactory {
private ImageSwitcher image_switcher;
private Gallery gallery;

private Integer[] mThumbIds = { R.drawable.a1, R.drawable.a2,
R.drawable.a3, R.drawable.a4, R.drawable.a5,
};

private Integer[] mImageIds = { R.drawable.a1, R.drawable.a2,
R.drawable.a3, R.drawable.a4, R.drawable.a5, };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.image_switcher_gallery);

image_switcher = (ImageSwitcher) findViewById(R.id.switcher);
image_switcher.setFactory(this);

image_switcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
image_switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));

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

gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(this);
}

@Override
public View makeView() {
ImageView image = new ImageView(this);
image.setBackgroundColor(0xFF000000);
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return image;
}

public class ImageAdapter extends BaseAdapter {
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;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mContext);

image.setImageResource(mThumbIds[position]);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return image;
}

private Context mContext;

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
ImageSwitcher image_switcher = (ImageSwitcher) findViewById(R.id.switcher);
image_switcher.setImageResource(mImageIds[position]);

}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}

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="wrap_content"
android:orientation="vertical">

<ImageSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="450dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>

<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="120dip"
android:background="#55000000"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:gravity="center_vertical"
android:spacing="30dip"
/>


</RelativeLayout>

更多相关文章

  1. Android中framework层下添加aidl编译说程序包不存在
  2. Android调节屏幕亮度分析源码
  3. Android系列之利用SharedPreferences是否是第一次进入安装程序
  4. 如何使Android应用程序获取系统权限【转】
  5. android 程序错误全局处理
  6. 关于安卓屏幕切换的过程

随机推荐

  1. 在php中get和post区别
  2. php header的作用
  3. 什么是php工厂模式
  4. php join的用法
  5. php中define的用法
  6. asp与php网站优缺点
  7. php保留两位小数的方法
  8. php构造函数的作用
  9. 排序算法—归并排序【附代码】
  10. php中include_once的意思