要点:
(查看Api总结)
1:ImageSwitcher 继承 ViewSwitcher, (ViewSwitcher 有继承FrameLayout )
2: 要实现切图必须实现 ViewSwitcher.ViewFactory接口中的 makeView()
易错点:
1:
mImageSwitcher.setFactory(this);
mImageSwitcher.setImageResource(mImages[0]);
setImageResource必须调用在setFactory之后。 原因:根据源码分析调用setFactory获得接口,才能
获得makeView()返回的view 否则抛空指针异常
2: 在makeView中设置布局参数 要用FrameLayout布局参数
package com.imageswitcher; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.FrameLayout; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; import android.widget.ViewSwitcher; public class MainActivity extends Activity implements ViewSwitcher.ViewFactory { private LinearLayout mLinearLayout; private ImageSwitcher mImageSwitcher; private Integer[] mImages = {R.drawable.android01, R.drawable.android02, R.drawable.android03, R.drawable.android04, R.drawable.android05, R.drawable.android06, R.drawable.android07, R.drawable.android08, R.drawable.android09}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); mImageSwitcher.setFactory(this); mImageSwitcher.setImageResource(mImages[0]); mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); for (int i = 0; i < mImages.length; i++) { mLinearLayout.addView(getImageView(i)); } } private ImageView getImageView(final int i) { ImageView imageView = new ImageView(this); imageView.setImageResource(mImages[i]); imageView.setId(i); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mImageSwitcher.setImageResource(mImages[i]); Toast.makeText(v.getContext(), "选择了" + (v.getId() + 1) + "张图", Toast.LENGTH_SHORT).show(); } }); return imageView; } private void initView() { mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout); mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); } @Override public View makeView() { ImageView imageView = new ImageView(this); Log.i(MainActivity.class.getSimpleName(), "第几次调用"); imageView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return imageView; } }
xml
<RelativeLayout 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" tools:context=".MainActivity"> <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="match_parent" android:layout_height="200dp"> </ImageSwitcher> <HorizontalScrollView android:layout_below="@id/imageSwitcher" android:layout_width="match_parent" android:layout_height="100dp" > <LinearLayout android:id="@+id/linearLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout> </HorizontalScrollView> </RelativeLayout>