Android的四个动画特效

Android的四个动画分别是渐变透明,缩放尺寸,旋转角度,移动方向等动画组合,这些动画可以在res资源文件下中新建的anim文件夹下创建一些常用的动画属性文件供自己调用。下面笔者用写代码来说明四个动画的运行效果,并动态的设置动画各个参数属性值,方便一些学习者测试,并窥视它的动画细节,以便以掌握动画特效。写纯代码实现动画还能方便开发者打包jar.

特别声明,请读者务必具备一定的英语阅读能力。

一,渐变动画alpha

创建一个继承Activity的渐变动画页面类AlphaActivity.java

public class AlphaActivity extends Activity {

	private ImageView spaceshipImage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_alpha);
		
		spaceshipImage = (ImageView) this.findViewById(R.id.imageView_alpha);
		((Button) this.findViewById(R.id.button_start_alpha)).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startAlpha();
			}
		});
	}
	
	/**
	 * Alpha渐变动画,从透明到不透明,范围值为百分比
	 */
	private void startAlpha() {
//		设置动画开始时的透明度
		String text = ((EditText) this.findViewById(R.id.editText_from_alpha)).getText().toString();
		if(text.equals("")) text = "0.1";
		float fromAlpha = Float.valueOf(text);
//		设置动画结束时的透明度
		text = ((EditText) this.findViewById(R.id.editText_to_alpha)).getText().toString();
		if(text.equals("")) text = "1.0";
		float toAlpha = Float.valueOf(text);
//		设置动画时间
		text = ((EditText) this.findViewById(R.id.editText_duration_alpha)).getText().toString();
		if(text.equals("")) text = "1000";
		int duration = Integer.valueOf(text);
		
		Animation alphaAnimation = new AlphaAnimation(fromAlpha, toAlpha);
		alphaAnimation.setDuration(duration);
//		开始动画
		this.spaceshipImage.startAnimation(alphaAnimation);
	}
}

下面是渐变alpha的布局界面文件activity_alpha.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=".AlphaActivity" >

    <TextView
        android:id="@+id/textView_alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Alpha 渐变透明度动画" />

    <ImageView
        android:id="@+id/imageView_alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView_alpha"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:src="@drawable/test" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView_alpha"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:background="#667b7b7b"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="fromAlpha=" />

            <EditText
                android:id="@+id/editText_from_alpha"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0.1"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" >

            </EditText>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="toAlpha=" />

            <EditText
                android:id="@+id/editText_to_alpha"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="1.0"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="duration=" />

            <EditText
                android:id="@+id/editText_duration_alpha"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:hint="1000"
                android:inputType="number"
                android:maxLength="4"
                android:singleLine="true" >
            </EditText>

        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/button_start_alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="开始动画" />

</RelativeLayout>

渐变动画页面效果图:

二,缩放动画scale

创建一个继承Activity的缩放动画界面类ScaleActivity.java

public class ScaleActivity extends Activity {

	private ImageView spaceshipImage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_scale);
		
		spaceshipImage = (ImageView) this.findViewById(R.id.imageView_scale);
		((Button) this.findViewById(R.id.button_start_scale)).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startScale();
			}
		});
	}

	private void startScale() {
		String text = ((EditText) this.findViewById(R.id.editText_fromx_scale)).getText().toString();
		if(text.equals("")) text = "0.1";
		float from_x_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_tox_scale)).getText().toString();
		if(text.equals("")) text = "1.0";
		float to_x_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_fromy_scale)).getText().toString();
		if(text.equals("")) text = "0.1";
		float from_y_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_toy_scale)).getText().toString();
		if(text.equals("")) text = "1.0";
		float to_y_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_pivotX_scale)).getText().toString();
		if(text.equals("")) text = "0";
		float pivotX_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_pivotY_scale)).getText().toString();
		if(text.equals("")) text = "0";
		float pivotY_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_duration_scale)).getText().toString();
		if(text.equals("")) text = "1000";
		int duration = Integer.valueOf(text);
//		Animation scaleAnimation = new ScaleAnimation(from_x_scale, to_x_scale, from_y_scale, to_y_scale);
//		另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y 坐标的开始位置pivotXValue、pivotYValue等。
		Animation scaleAnimation = new ScaleAnimation(from_x_scale, to_x_scale, from_y_scale, to_y_scale,
				Animation.RELATIVE_TO_SELF, pivotX_scale, Animation.RELATIVE_TO_SELF, pivotY_scale);
//		scaleAnimation.setRepeatCount(repeatCount)
		scaleAnimation.setDuration(duration);
		this.spaceshipImage.startAnimation(scaleAnimation);
	}
}

下面是缩放动画scale的布局文件activity_acale.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=".ScaleActivity" >

    <TextView
        android:id="@+id/textView_scale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Scale 缩放动画" />

    <Button
        android:id="@+id/button_start_scale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="开始动画" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView_scale"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="#667b7b7b"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="fromXScale=" />

            <EditText
                android:id="@+id/editText_fromx_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0.1"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" >

                <requestFocus />
            </EditText>

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="toXScale=" />

            <EditText
                android:id="@+id/editText_tox_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="1.0"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="fromYScale=" />

            <EditText
                android:id="@+id/editText_fromy_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0.1"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="toYScale=" />

            <EditText
                android:id="@+id/editText_toy_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="1.0"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pivotX=" />

            <EditText
                android:id="@+id/editText_pivotX_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0%"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="pivotY=" />

            <EditText
                android:id="@+id/editText_pivotY_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0%"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="duration=" />

            <EditText
                android:id="@+id/editText_duration_scale"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:hint="1000"
                android:inputType="number"
                android:maxLength="4"
                android:singleLine="true" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView_scale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView_scale"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:src="@drawable/test" />

</RelativeLayout>

缩放动画页面效果图

三,旋转动画rotate

创建一个继承Activity的旋转动画界面类RotateActivity.java

public class RotateActivity extends Activity {

	private ImageView spaceshipImage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_rotate);
		
		spaceshipImage = (ImageView) this.findViewById(R.id.imageView_rotate);
		((Button) this.findViewById(R.id.button_start_rotate)).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startScale();
			}
		});
	}
	
	private void startScale() {
		String text = ((EditText) this.findViewById(R.id.editText_start_rotate)).getText().toString();
		if(text.equals("")) text = "0";
		float start_degree_rotate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_end_rotate)).getText().toString();
		if(text.equals("")) text = "360";
		float end_degree_rotate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_pivotX_rotate)).getText().toString();
		if(text.equals("")) text = "0";
		float pivotX_rotate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_pivotY_rotate)).getText().toString();
		if(text.equals("")) text = "0";
		float pivotY_rotate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_duration_rotate)).getText().toString();
		if(text.equals("")) text = "1000";
		int duration = Integer.valueOf(text);
//		Animation rotateAnimation = new RotateAnimation(start_degree_rotate, end_degree_rotate);
//		另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y 坐标的开始位置pivotXValue、pivotYValue等。
		Animation rotateAnimation = new RotateAnimation(start_degree_rotate, end_degree_rotate,
				Animation.RELATIVE_TO_SELF, pivotX_rotate, Animation.RELATIVE_TO_SELF, pivotY_rotate);
		rotateAnimation.setDuration(duration);
		this.spaceshipImage.startAnimation(rotateAnimation);
	}
}

下面是旋转动画rotate的布局文件activity_rotate.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=".RotateActivity" >

    <TextView
        android:id="@+id/textView_rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Rotate 旋转动画" />

    <Button
        android:id="@+id/button_start_rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="开始动画" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView_rotate"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="#667b7b7b"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="fromDegrees=" />

            <EditText
                android:id="@+id/editText_start_rotate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0'"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" >

                <requestFocus />
            </EditText>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:text="toDegrees=" />

            <EditText
                android:id="@+id/editText_end_rotate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="360'"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pivotX=" />

            <EditText
                android:id="@+id/editText_pivotX_rotate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0%"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="pivotY=" />

            <EditText
                android:id="@+id/editText_pivotY_rotate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0%"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="duration=" />

            <EditText
                android:id="@+id/editText_duration_rotate"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:hint="1000"
                android:inputType="number"
                android:maxLength="4"
                android:singleLine="true" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView_rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView_rotate"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:src="@drawable/test" />

</RelativeLayout>

旋转动画页面效果图

四,位移动画translate

创建一个继承Activity的位移动画界面类TranslateActivity.java

public class TranslateActivity extends Activity {

	private ImageView spaceshipImage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_translate);
		
		spaceshipImage = (ImageView) this.findViewById(R.id.imageView_translate);
		((Button) this.findViewById(R.id.button_start_translate)).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startScale();
			}
		});
	}

	private void startScale() {
		String text = ((EditText) this.findViewById(R.id.editText_fromx_translate)).getText().toString();
		if(text.equals("")) text = "0.1";
		float from_x_translate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_tox_translate)).getText().toString();
		if(text.equals("")) text = "100";
		float to_x_translate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_fromy_translate)).getText().toString();
		if(text.equals("")) text = "0.1";
		float from_y_translate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_toy_translate)).getText().toString();
		if(text.equals("")) text = "100";
		float to_y_translate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_duration_translate)).getText().toString();
		if(text.equals("")) text = "1000";
		int duration = Integer.valueOf(text);
		Animation translateAnimation = new TranslateAnimation(from_x_translate, to_x_translate, from_y_translate, to_y_translate);
		translateAnimation.setDuration(duration);
		this.spaceshipImage.startAnimation(translateAnimation);
	}
}

下面是位移动画translate的布局文件activity_translate.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=".TranslateActivity" >

    <TextView
        android:id="@+id/textView_translate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Translate 位移动画" />

    <Button
        android:id="@+id/button_start_translate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="开始动画" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView_translate"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="#667b7b7b"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="fromXDelta=" />

            <EditText
                android:id="@+id/editText_fromx_translate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0.1"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" >

                <requestFocus />
            </EditText>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:text="toXDelta=" />

            <EditText
                android:id="@+id/editText_tox_translate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="100"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="fromYDelta=" />

            <EditText
                android:id="@+id/editText_fromy_translate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0.1"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="toYDelta=" />

            <EditText
                android:id="@+id/editText_toy_translate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="100"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="duration=" />

            <EditText
                android:id="@+id/editText_duration_translate"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:hint="1000"
                android:inputType="number"
                android:maxLength="4"
                android:singleLine="true" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView_translate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView_translate"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:src="@drawable/test" />

</RelativeLayout>

位移动画页面效果图

五,多页卡的主页面

创建一个有四个选项卡的页卡主页面MainActivity.java,继承与TabActivity

public class MainActivity extends TabActivity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
//		渐变动画一
		TabHost tabHost = this.getTabHost();
		TabSpec tab1 = tabHost.newTabSpec("tab1");
		tab1.setIndicator("渐变");
		tab1.setContent(new Intent(this, AlphaActivity.class));
		tabHost.addTab(tab1);
//		缩放动画二
		TabSpec tab2 = tabHost.newTabSpec("tab2");
		tab2.setIndicator("缩放");
		tab2.setContent(new Intent(this, ScaleActivity.class));
		tabHost.addTab(tab2);
//		旋转动画三
		TabSpec tab3 = tabHost.newTabSpec("tab3");
		tab3.setIndicator("旋转");
		tab3.setContent(new Intent(this, RotateActivity.class));
		tabHost.addTab(tab3);
//		位移动画四
		TabSpec tab4 = tabHost.newTabSpec("tab4");
		tab4.setIndicator("位移");
		tab4.setContent(new Intent(this, TranslateActivity.class));
		tabHost.addTab(tab4);
	}

}

下面是主页面的main的布局文件activity_main.xml,其中tabHost,tabWidget组件id为自动设置不能修改

<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" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >
            </FrameLayout>

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

            </TabWidget>

        </LinearLayout>
    </TabHost>

</RelativeLayout>

六,在AndrroidManifest.xml属性文件中添加上面的四个继承与Activity类的属性

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.animationtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.animationtest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="AlphaActivity"></activity>
        <activity android:name="ScaleActivity"></activity>
        <activity android:name="RotateActivity"></activity>
        <activity android:name="TranslateActivity"></activity>
    </application>

</manifest>

七,项目总算完成了,项目没有报错的话那安装在模拟器中,运行界面按下开始动画按钮测试效果看看吧。

如果读者还有什么问题,欢迎留言。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TA远方

谢谢!收到你的爱╮(╯▽╰)╭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值