android 入门 ImageSwitcher

本文展示了一个使用 Android 的 Gallery 和 ImageSwitcher 控件来实现图片轮播的应用示例。通过自定义适配器加载图片资源,并实现了 Gallery 的选择监听器以切换显示的图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.isoftstone.cry;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;

public class GallerySwitcherActivity extends Activity implements OnItemSelectedListener , ViewFactory
{
private ImageSwitcher mswitcher ;
private Integer[] mThumbIds ={
R.drawable.fc,
R.drawable.ic_launcher,
R.drawable.ic_menu_add,
R.drawable.ic_menu_edit,
R.drawable.ic_menu_help,
R.drawable.psu
};
private Integer[] mImageIds ={
R.drawable.fc,
R.drawable.ic_launcher,
R.drawable.ic_menu_add,
R.drawable.ic_menu_edit,
R.drawable.ic_menu_help,
R.drawable.psu
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_imageswitcher);
mswitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
mswitcher.setFactory(this);
mswitcher.setAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
mswitcher.setAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));

Gallery gallery = (Gallery)findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(this);
}
//gallery adapter
public class ImageAdapter extends BaseAdapter
{
Context context ;
public ImageAdapter(Context c) {
// TODO Auto-generated constructor stub
context = c ;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.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 convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv = new ImageView(context);
iv.setImageResource(mThumbIds[position]);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
iv.setBackgroundResource(R.drawable.fc);
return iv;
}
}
@Override
public void onItemSelected(AdapterView<?> adapter, View v, int position,
long id) {
// TODO Auto-generated method stub
mswitcher.setImageResource(mImageIds[position]);
}
@Override
public View makeView() {
// TODO Auto-generated method stub
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;
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}



<?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/gallery1"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:spacing="16dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />

<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageSwitcher>

</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值