使用ImageSwitcher要两个步骤:
1、为ImageSwitcher提供一个ViewFactory,改factory生成的View组件必须是ImageView
2、切换图片时,调用setImageDrawble或者setImageResourece即可
<span style="font-size:14px;">import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.ViewSwitcher.ViewFactory;
import com.example.android_lb_day3_ui.R;
public class ImageSwitcherTest extends Activity {
int imgIds[] = new int[] { R.drawable.animal_01, R.drawable.animal_02,
R.drawable.animal_03, R.drawable.animal_04, R.drawable.animal_05,
R.drawable.animal_01, R.drawable.animal_02, R.drawable.animal_03,
R.drawable.animal_04, R.drawable.animal_05, R.drawable.animal_01,
R.drawable.animal_02, };
ImageSwitcher switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_switcher);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < imgIds.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", imgIds[i]);
list.add(map);
}
switcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
// 为ImageSwitcher设置切换的动画效果
switcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView image = new ImageView(ImageSwitcherTest.this);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);// 设置图片缩放比例
image.setLayoutParams(new ImageSwitcher.LayoutParams(
160, 160));
return image;
}
});
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.swticher_img, new String[] { "image" },
new int[] { R.id.swticher_img });
GridView grid = (GridView) findViewById(R.id.gridView);
grid.setAdapter(adapter);
// 被选中的事件
grid.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
switcher.setImageResource(imgIds[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// 呗单机的事件
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switcher.setImageResource(imgIds[position]);
}
});
}
}</span>
<?xml version="1.0" encoding= "utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="pt"
android:numColumns="4"
android:verticalSpacing="2pt"
android:id="@+id/gridView" >
</GridView >
<ImageSwitcher
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"
android:inAnimation="@android:anim/fade_in"
android:outAnimation="@android:anim/fade_out"
android:id="@+id/imageSwitcher" >
</ImageSwitcher >
</LinearLayout>