当前位置:网站首页>imageswitcher的功能和用法

imageswitcher的功能和用法

2022-07-07 15:40:00 XLMN

imageswitcher的功能和用法

public class MainActivity extends Activity {
int[] imageIds = new int[] { R.drawable.love, R.drawable.mia,
R.drawable.love, R.drawable.mia, R.drawable.love, R.drawable.mia,
R.drawable.love, R.drawable.mia, R.drawable.love, R.drawable.mia,
R.drawable.love, R.drawable.mia, R.drawable.love, R.drawable.mia,
R.drawable.love,R.drawable.love};
ImageSwitcher is;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.imageswitcher);
    // 创建list对象,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
    is = (ImageSwitcher) findViewById(R.id.imageswitcher);
    // 获取显示图片imageswitcher设置图片切换的动画效果。
    is.setFactory(new ViewSwitcher.ViewFactory() {

        @Override
        public View makeView() {
            // TODO Auto-generated method stub
            // 创建imageview对象
            ImageView iv = new ImageView(MainActivity.this);
            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
            iv.setLayoutParams(new ImageSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            // 返回imageview对象
            return iv;
        }
    });
    // 创建一个simpleAdapter
    SimpleAdapter sa = new SimpleAdapter(this, listitems, R.layout.cell,
            new String[] { "image" }, new int[] { R.id.imge1 });
    GridView grid = (GridView) findViewById(R.id.gridview);
    // 为gridview设置adapter
    grid.setAdapter(sa);
    // 添加列表项被选中的监听器
    grid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                  int arg2, long arg3) {
            // TODO Auto-generated method stub
            is.setImageResource(imageIds[arg2]);
        }

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

        }
    });
    // 添加列表项被选中的监听器
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            // TODO Auto-generated method stub
            // 显示被单击的图片
            is.setImageResource(imageIds[arg2]);
        }
    });
}

}

<?xml version="1.0" encoding="utf-8"?>

<!-- 定义girdview组件 -->

<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:horizontalSpacing="2dp"
    android:numColumns="4"
    android:verticalSpacing="2dp" >
</GridView>
<!-- 定义一个imageswitcher组件 -->

<ImageSwitcher
    android:id="@+id/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" >
</ImageSwitcher>
<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/imge1"
    android:layout_width="match_parent"
    android:layout_height="100dp"/>

在这里插入图片描述

在这里插入图片描述

原网站

版权声明
本文为[XLMN]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44701192/article/details/125012255