布局:
-
<RelativeLayout xmlns:android=""
-
xmlns: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" >
-
-
<ImageSwitcher
-
android:id="@+id/switcher"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:layout_alignParentLeft="true"
-
android:layout_alignParentTop="true" >
-
</ImageSwitcher>
-
-
<Gallery
-
android:id="@+id/gallery"
-
android:layout_width="fill_parent"
-
android:layout_height="60dp"
-
android:layout_alignParentBottom="true"
-
android:layout_alignParentLeft="true"
-
android:background="#55000000"
-
android:gravity="center_vertical"
-
android:spacing="60dp" />
-
-
</RelativeLayout>
代码:
-
package com.example.gallary;
-
-
import android.os.Bundle;
-
import android.app.Activity;
-
import android.content.Context;
-
import android.view.Menu;
-
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.AdapterView;
-
import android.widget.AdapterView.OnItemSelectedListener;
-
import android.widget.BaseAdapter;
-
import android.widget.Gallery;
-
import android.widget.ImageSwitcher;
-
import android.widget.ImageView;
-
import android.widget.ViewSwitcher.ViewFactory;
-
-
public class MainActivity extends Activity
-
implements OnItemSelectedListener, ViewFactory
-
{
-
private ImageSwitcher mSwitcher;
-
private Gallery gll;
-
private Integer[] mThumbIds =
-
{
-
R.drawable.a, R.drawable.b,
-
R.drawable.c, R.drawable.d,
-
R.drawable.e, R.drawable.f,
-
R.drawable.g, R.drawable.h,
-
R.drawable.j
-
};
-
private Integer[] mImageIds =
-
{
-
R.drawable.a, R.drawable.b,
-
R.drawable.c, R.drawable.d,
-
R.drawable.e, R.drawable.f,
-
R.drawable.g, R.drawable.h,
-
R.drawable.j
-
};
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
requestWindowFeature(Window.FEATURE_NO_TITLE);
-
setContentView(R.layout.activity_main);
-
-
///< 图片切换器【在选中某个缩略图时会触发onItemSelected事件,进行资源切换】
-
///< 真正关键的是makeView,这里实现了View的生成
-
mSwitcher = (ImageSwitcher)findViewById(R.id.switcher);
-
mSwitcher.setFactory(this);
-
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
-
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
-
-
///< 画廊【ImageAdapter为图片适配器,能设置图片资源,然后返回该资源View】
-
///< ImageAdapter作为适配器,承载了显示缩略图和返回缩略图对应的图片资源ID【getItemId】的功能
-
gll = (Gallery)findViewById(R.id.gallery);
-
gll.setAdapter(new ImageAdapter(this));
-
gll.setOnItemSelectedListener(this);
-
}
-
-
@Override
-
public boolean onCreateOptionsMenu(Menu menu) {
-
// Inflate the menu; this adds items to the action bar if it is present.
-
getMenuInflater().inflate(R.menu.main, menu);
-
return true;
-
}
-
-
@Override
-
public View makeView() {
-
// TODO Auto-generated method stub
-
ImageView iv = new ImageView(this);
-
-
iv.setBackgroundColor(0xFF000000);
-
-
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
-
-
iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
-
LayoutParams.FILL_PARENT));
-
-
return iv;
-
}
-
-
@Override
-
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
-
long arg3) {
-
// TODO Auto-generated method stub
-
mSwitcher.setImageResource(mImageIds[arg2]);
-
}
-
-
@Override
-
public void onNothingSelected(AdapterView<?> arg0) {
-
// TODO Auto-generated method stub
-
-
}
-
-
public class ImageAdapter extends BaseAdapter
-
{
-
public ImageAdapter(Context c)
-
{
-
mContext = c;
-
}
-
-
@Override
-
public int getCount() {
-
// TODO Auto-generated method stub
-
return mThumbIds.length;
-
}
-
-
@Override
-
public Object getItem(int arg0) {
-
// TODO Auto-generated method stub
-
return arg0;
-
}
-
-
@Override
-
public long getItemId(int arg0) {
-
// TODO Auto-generated method stub
-
return arg0;
-
}
-
-
@Override
-
public View getView(int arg0, View arg1, ViewGroup arg2) {
-
// TODO Auto-generated method stub
-
ImageView iv = new ImageView(mContext);
-
-
iv.setImageResource(mThumbIds[arg0]);
-
-
iv.setAdjustViewBounds(true);
-
-
iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
-
LayoutParams.WRAP_CONTENT));
-
-
iv.setBackgroundResource(R.drawable.j);
-
-
return iv;
-
}
-
private Context mContext;
-
}
-
}
阅读(5165) | 评论(0) | 转发(0) |