动画特效的应用----ViewSwitcher控件

 

     为了让活动能不断更新视图控件的内容,为每一个视图控件绑定动画特效显得非常繁琐,但是Android SDK提供的ViewSwitcher控件,能够高效简便地更新视图。

       ViewSwitcher有两个子视图控件,并处理从当前子视图到下一个子视图的过渡。ViewSwitcher的子视图控件是使用ViewFactory以编程方式生成的。

       ViewSwitcher有两个子类:

       → TextSwitcher:让您能够在两个TextView控件之间切换。

       → ImageSwitcher:让您能够在两个ImageView控件之间切换。

       下面以ImageSwitcher为例来说一下使用的步骤:

 

1、  制作两个动画特效switcher_in.xmlswitcher_out.xml

switcher_in.xml

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

<set xmlns:android="http://schemas.android.com/apk/res/android">

         <scale

                   android:fromXScale="0.0"

                   android:toXScale="1.0"

                   android:fromYScale="1.0"

                   android:toYScale="1.0"

                   android:pivotX="99%"

                   android:pivotY="99%"

                   android:duration="500" />
         
         <alpha 

                   android:interpolator="@android:anim/linear_interpolator"

                   android:fromAlpha="0.0"

                   android:toAlpha="1.0"

                   android:duration="500"/>

</set>

 

switcher_out.xml

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

<set xmlns:android="http://schemas.android.com/apk/res/android">

         <scale 

                   android:fromXScale="1.0"

                   android:toXScale="0.0"

                   android:fromYScale="1.0"

                   android:toYScale="1.0"

                   android:pivotX="1%"

                   android:pivotY="1%"

                   android:duration="500"/>

         <alpha 

                   android:interpolator="@android:anim/linear_interpolator"

                   android:fromAlpha="1.0"

                   android:toAlpha="0.0"

                   android:duration="500"/>

</set>

 

2、  在布局文件中添加ImageSwitcher,并将刚创建的动画与其绑定:

        

	<ImageSwitcher 

                   android:id="@+id/imageSwitcher"

                   android:layout_width="wrap_content"

                   android:layout_height="wrap_content"

                   android:inAnimation="@anim/switcher_in"

                   android:outAnimation="@anim/switcher_out">

         </ImageSwitcher>


3、  为ImageSwitcher创建布局资源image_switcher_view.xml

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

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"

         android:layout_width="match_parent"

         android:layout_height="match_parent"

         android:scaleType="fitCenter" >

</ImageView>


4、初始化ImageSwitcher控件,实现自定义ViewFactory类:

 

private ImageSwitcher imageSwitcher;

private float touchDownX , touchUpX;

int index = 0;

int[] arrayImage = {R.drawable.a7 , R.drawable.a9 , R.drawable.a11 , R.drawable.a12};

@Override

protected void onCreate(Bundle savedInstanceState)

{

	super.onCreate(savedInstanceState);

	setContentView(R.layout.activity_main);

	imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);

	imageSwitcher.setFactory(new MyImageFacotry());

	imageSwitcher.setImageResource(arrayImage[index]);

}

private class MyImageFacotry implements ViewFactory

{

        public View makeView() 

        {

		ImageView imageView = (ImageView)LayoutInflater.from(getApplicationContext()).inflate(R.layout.image_switcher_view, imageSwitcher, false);

                   return imageView;

        }

}


5、  为ImageSwitcher添加手势滑动监听效果:

 

imageSwitcher.setOnTouchListener(new OnTouchListener()

{

	public boolean onTouch(View arg0, MotionEvent arg1) 

	{

	if (arg1.getAction() == MotionEvent.ACTION_DOWN)

	{

	touchDownX = arg1.getX();

	return true;

	}

	else if(arg1.getAction() == MotionEvent.ACTION_UP)

	{

		touchUpX = arg1.getX();

		if (touchDownX - touchUpX > 100)//左滑

		{

			if (index >0 ) 

			{

				imageSwitcher.setInAnimation(getApplicationContext(), R.anim.switcher_in_right);

				imageSwitcher.setOutAnimation(getApplicationContext(), R.anim.switcher_out_right);

				imageSwitcher.setImageResource(arrayImage[index]);

				index--;

			}

		}

		else

                   {

                     	if (index < 3)

                            {

                                      imageSwitcher.setInAnimation(getApplicationContext(), R.anim.switcher_in_left);

                                      imageSwitcher.setOutAnimation(getApplicationContext(), R.anim.switcher_out_left);

                                      imageSwitcher.setImageResource(arrayImage[index]);

                                      index++;

                             }

                     }

                     return true;

	}

           return false;

 }
});

 

源码下载地址:动画特效的应用----ViewSwitcher控件.docx 

 

 

 

你可能感兴趣的:(动画特效的应用----ViewSwitcher控件)