当你跌入谷底的时候,不要绝望,抬起头,你会看见一片灿烂的星空。
package com.crazyit.ui.viewanimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* ImageSwitcher 的简单功能与用法
* ImageSwitcher 继承了ViewSwitcher
* 使用 ImageSwitcher 只需要实现2步
* (1) 为 ImageSwitcher提供一个 ViewFactory 该ViewFactory组件生成的View组件必须是 imageview
* (2) 需要切换图片的时候,只要调用 ImageSwitcher的 setImageDrawable(Drawable drawable), setImageResource(int resId)
* 和setImageURi(Uri uri)方法更换图片即可.
*/
public class ImageSwitcherActivity extends AppCompatActivity {
int[] imageIds = new int[]{R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.e, R.drawable.f, R.drawable.fuqi};
ImageSwitcher switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_switcher);
List<Map<String, Object>> listItems = new ArrayList<>();
for (int i = 0; i < imageIds.length; i++) {
Map<String, Object> listItem = new HashMap<>();
listItem.put("image", imageIds[i]);
listItems.add(listItem);
}
switcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
switcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(ImageSwitcherActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ViewSwitcher.LayoutParams(ViewSwitcher.LayoutParams.WRAP_CONTENT,
ViewSwitcher.LayoutParams.WRAP_CONTENT));
return imageView;
}
});
SimpleAdapter simpleAdapter = new SimpleAdapter(this,
listItems,
R.layout.cell,
new String[]{"image"},
new int[]{R.id.image01}
);
GridView gridView = (GridView) findViewById(R.id.grid01);
gridView.setAdapter(simpleAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switcher.setImageResource(imageIds[position]);
Toast.makeText(ImageSwitcherActivity.this, "22", Toast.LENGTH_SHORT).show();
}
});
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.crazyit.ui.viewanimator.ImageSwitcherActivity">
<GridView
android:id="@+id/grid01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="2dp"
android:numColumns="4"
android:verticalSpacing="2dp"></GridView>
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="320dp"
android:layout_height="320dp"
android:layout_gravity="center"
android:inAnimation="@android:anim/fade_in"
android:outAnimation="@android:anim/fade_out"></ImageSwitcher>
</LinearLayout>
列表项的布局文件
<?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:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dp">
<ImageView
android:id="@+id/image01"
android:layout_width="50dp"
android:layout_height="50dp" />
</LinearLayout>