Android的动画例子:模拟应用首次启动界面导向,模拟杀毒软件扫描病毒的动画效果

模拟应用首次启动界面导向

API

overridePendingTransition(int enterAnim, int exitAnim)
              enterAnim  :  将要显示的新界面进入动画
              exitAnim  :  当前界面退出的动画
用于在Activity界面切换时显示指定的切换动画
一般在startActivity()或者finish()之后调用
在Activity里面重写就可以了

代码

Activity类
package com.jane.animation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;

/**
 * 欢迎界面
 */
public class WelcomeActivity extends Activity
{

	private RelativeLayout rl_welcome_root;
	private Handler handler = new Handler()
	{
		public void handleMessage(android.os.Message msg)
		{
			if (msg.what == 1)
			{
				startActivity(new Intent(WelcomeActivity.this,
						SetupGuide1Activity.class));
				// 关闭自己
				finish();
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);

		rl_welcome_root = (RelativeLayout) findViewById(R.id.rl_welcome_root);

		// 显示动画
		showAnimation();

		// 发送延迟3s的消息
		handler.sendEmptyMessageDelayed(1, 3000);
	}

	/**
	 * 显示动画
	 * 
	 * 旋转动画 RotateAnimation: 0-->360 视图的中心点 2s 透明度动画 AlphaAnimation: 0-->1 2s
	 * 缩放动画 ScaleAnimation: 0-->1 视图的中心点 2s
	 */
	private void showAnimation()
	{
		// 旋转动画 RotateAnimation: 0-->360 视图的中心点 2s
		RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotateAnimation.setDuration(2000);
		// 透明度动画 AlphaAnimation: 0-->1 2s
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		alphaAnimation.setDuration(2000);
		// 缩放动画 ScaleAnimation: 0-->1 视图的中心点 2s
		ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		scaleAnimation.setDuration(2000);
		// 创建复合动画,并添加
		AnimationSet animationSet = new AnimationSet(true);
		animationSet.addAnimation(rotateAnimation);
		animationSet.addAnimation(alphaAnimation);
		animationSet.addAnimation(scaleAnimation);
		// 启动
		rl_welcome_root.startAnimation(animationSet);
	}
}
package com.jane.animation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

/**
 * 设置向导1界面
 */
public class SetupGuide1Activity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup_guide1);
	}

	public void next(View v)
	{
		startActivity(new Intent(this, SetupGuide2Activity.class));
		overridePendingTransition(R.anim.right_in, R.anim.left_out);
	}
}
package com.jane.animation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

/**
 * 设置向导2界面
 */
public class SetupGuide2Activity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup_guide2);
	}

	public void next(View v)
	{
		startActivity(new Intent(this, SetupGuide3Activity.class));
		overridePendingTransition(R.anim.right_in, R.anim.left_out);
	}

	public void pre(View v)
	{
		finish();
		overridePendingTransition(R.anim.left_in, R.anim.right_out);
	}
}

package com.jane.animation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

/**
 * 设置向导3界面
 */
public class SetupGuide3Activity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup_guide3);
	}

	public void confirm(View v)
	{
		startActivity(new Intent(this, MainActivity.class));
		overridePendingTransition(R.anim.bottom_in, R.anim.disappear_out);
	}

	public void pre(View v)
	{
		finish();
		overridePendingTransition(R.anim.left_in, R.anim.right_out);
	}
}
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_welcome_root"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context="${relativePackage}.${activityClass}" >

    <!-- 这是我们自己定义的正在加载的进度显示
    	首先我们得定义一个旋转的动画,里面包含一个图片,其他的默认就行
    	然后在这里使用android:indeterminateDrawable="@anim/image_progress"
    	进行引用就行了
     -->
    <ProgressBar
        android:id="@+id/pb_welcome_loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp"
        android:indeterminateDrawable="@anim/image_progress"
        android:indeterminateDuration="700"/>
    
    <TextView
        android:id="@+id/tv_welcome_version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/pb_welcome_loading"
        android:layout_marginBottom="10dp"
        android:text="当前版本: 1.0"
        android:textSize="20sp" />
</RelativeLayout>
<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="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="向导1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:onClick="next"
        android:text="下一步" />

</RelativeLayout>
<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="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="向导2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:onClick="next"
        android:text="下一步" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:onClick="pre"
        android:text="上一步" />

</RelativeLayout>
<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="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="向导3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:onClick="confirm"
        android:text="完成" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:onClick="pre"
        android:text="上一步" />

</RelativeLayout>
<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="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/et_main_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="用户名" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_main_name"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="41dp"
        android:onClick="login"
        android:text="登陆" />

</RelativeLayout>
动画文件
用来显示自己定义的加载中的动画显示
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/progess" >

</rotate>
下面都是一些界面切换的效果动画

<?xml version="1.0" encoding="utf-8"?>
<translate
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:fromYDelta="100%"
     android:toYDelta="0"
     android:duration="500">
</translate>

<?xml version="1.0" encoding="utf-8"?>
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1" 
    android:toAlpha="0"
    android:duration="500">
</alpha>


<?xml version="1.0" encoding="utf-8"?>
<translate
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:fromXDelta="-100%"
     android:toXDelta="0"
     android:duration="500">
</translate>

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="0"
    android:toXDelta="-100%" >

</translate>

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="100%"
    android:toXDelta="0" >

</translate>

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="0"
    android:toXDelta="100%" >

</translate>
抖动的动画(系统的源代码)

<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="0"
    android:interpolator="@anim/cycle_8"
    android:toXDelta="10" />


<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:cycles="8" />

模拟杀毒软件扫描病毒的动画效果

如何自定义水平进度条?
得在drawable里面定义自己的进度条图片的xml文件
然后再在进度条视图里面进行引用
android:progressDrawable="@drawable/my_progress"

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 背景图片 -->
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/security_progress_bg">
    </item>
    <!-- 进度图片 -->
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/security_progress">
    </item>

</layer-list>

代码

布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:gravity="center"
        android:padding="5dp"
        android:text="手机杀毒"
        android:textSize="25sp" />

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

        <!-- 在帧布局里面先指定整个图片作为背景
        	然后在里面再添加扫描动作中的一小部分作为旋转动画
        	将这小部分的图片作为动画进行旋转就能达到扫描的效果
         -->
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/ic_scanner_malware" >

            <ImageView
                android:id="@+id/iv_main_scan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/act_scanning_03" />
        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:gravity="center_vertical"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_main_scan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <ProgressBar
                android:id="@+id/pb_main_scan"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:progressDrawable="@drawable/my_progress" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
Activity文件
package com.jane.animation2;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity
{
	private ImageView iv_main_scan;
	private TextView tv_main_scan;
	private ProgressBar pb_main_scan;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		iv_main_scan = (ImageView) findViewById(R.id.iv_main_scan);
		tv_main_scan = (TextView) findViewById(R.id.tv_main_scan);
		pb_main_scan = (ProgressBar) findViewById(R.id.pb_main_scan);

		// 1. 显示扫描动画
		showScanAnimation();

		// 2. 扫描,并显示扫描进度
		scan();
	}

	/**
	 * 扫描,并显示扫描进度
	 */
	private void scan()
	{
		// 启动异步任务做
		new AsyncTask<Void, Void, Void>()
		{
			// 1. 主线程, 显示提示视图
			protected void onPreExecute()
			{
				tv_main_scan.setText("手机杀毒引擎正在扫描中...");
			}

			// 2. 分线程, 做长时间的工作(扫描应用)
			@Override
			protected Void doInBackground(Void... params)
			{
				int appCount = 60;
				// 设置进度条的最大值
				pb_main_scan.setMax(appCount);
				for (int i = 0; i < appCount; i++)
				{
					SystemClock.sleep(40);
					// 扫描完成一个
					// 发布进度
					publishProgress();
				}
				return null;
			}

			// 在主线程执行, 更新进度
			protected void onProgressUpdate(Void[] values)
			{
				pb_main_scan.incrementProgressBy(1);// 增加1
				// pb_main_scan.setProgress(pb_main_scan.getProgress()+1);
			}

			// 3. 主线程, 更新界面
			protected void onPostExecute(Void result)
			{
				// 隐藏进度条
				pb_main_scan.setVisibility(View.GONE);
				// 更新文本
				tv_main_scan.setText("扫描完成, 未发现病毒应用, 请放心使用!");
				// 停止扫描动画
				iv_main_scan.clearAnimation();
			}
		}.execute();
	}

	/**
	 * 显示扫描动画 iv_main_scan的旋转动画
	 */
	private void showScanAnimation()
	{
		// 创建动画对象
		RotateAnimation animation = new RotateAnimation(0, 360,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		// 设置
		animation.setDuration(1000);
		animation.setRepeatCount(Animation.INFINITE);
		animation.setInterpolator(new LinearInterpolator());
		// 启动
		iv_main_scan.startAnimation(animation);
	}
}
drawable文件
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 背景图片 -->
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/security_progress_bg">
    </item>
    <!-- 进度图片 -->
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/security_progress">
    </item>
</layer-list>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ReflectMirroring

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值