ImageSwitcher继承了ViewSwitcher,主要在切换图片时加入动画效果
使用方法:
1.为ImageSwitcher提供一个ViewFactory,该ViewFactory生成的View组件必须是ImageView
2.切换图片时,用到的3个方法:imageSwitcher.setImageDrawable(Drawable drawable);
imageSwitcher.setImageResource(int resid);
imageSwitcher.setImageURI(URI uri);
1.java代码:
package gdp.switcherview2; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
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.Toast;
import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity {
//声明一个数组,用来装载图片资源
int[] imageIds = new int[]{R.drawable.baiyang, R.drawable.chunv, R.drawable.jinniu, R.drawable.juxie,
R.drawable.mojie, R.drawable.sheshou, R.drawable.shizi, R.drawable.shuangyu,
R.drawable.shuangzi, R.drawable.shuiping, R.drawable.tiancheng, R.drawable.tianxie};
//声明ImageSwitcher对象
private ImageSwitcher switcher ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //创建一个List对象,其元素Map
List<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>();
for(int i = 0; i<imageIds.length; i++){
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("image", imageIds[i]);
listItems.add(listItem);
} //获取显示图片的ImageSwitcher
switcher = (ImageSwitcher)findViewById(R.id.switcher);
//为imageSwicher设置图片切换的动画效果
switcher.setFactory(new ViewFactory() { @Override
public View makeView() {
// 创建imageView对象
ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
//返回ImageView对象
return imageView;
}
});
//创建Adapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.cell, new String[]{"image"}, new int[]{R.id.image1});
GridView grid = (GridView)findViewById(R.id.grid01);
grid.setAdapter(simpleAdapter);
//添加列表项被点击的监听器
grid.setOnItemClickListener(new ItemClick());
//添加列表项被选中的监听器
grid.setOnItemSelectedListener(new ItemSelect());
} class ItemClick implements OnItemClickListener{ @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switcher.setImageResource(imageIds[arg2]);
} } class ItemSelect implements OnItemSelectedListener{ @Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switcher.setImageResource(imageIds[arg2]);
} @Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Nothing Selected" , Toast.LENGTH_LONG).show();
} } @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;
} }
2.xml文件
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal" >
<!-- 定义一个GridView组件 -->
<GridView
android:id="@+id/grid01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp"
android:numColumns="4"
android:gravity="center"
/>
<!-- 定义一个ImageSwitcher组件 -->
<ImageSwitcher
android:id="@+id/switcher"
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"
/>
</LinearLayout>
cell.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
/>
</LinearLayout>