本文主要是介绍动画综合练习(延迟进入+页面向导+文本框抖动),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、activity_welcome.xml(界面刚加载进入欢迎界面的布局)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/ll_welcome_root"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" android:background="@drawable/welcome_back"android:gravity="center_horizontal|bottom"><ProgressBarandroid:id="@+id/progressBar1"android:layout_width="wrap_content"android:layout_height="wrap_content" android:indeterminateDrawable="@anim/image_progress"android:indeterminateDuration="500"/>
</LinearLayout>
2、image_progress.xml(欢迎界面启动后,屏幕底部会有一个旋转的加载进度条)
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"android:fromDegrees="0"android:toDegrees="360"android:pivotX="50%"android:pivotY="50%"android:drawable="@drawable/progess2"/>
3、WelcomeActivity.java
package com.atguigu.l10_app2;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.LinearLayout;
/*** 欢迎界面**/
public class WelcomeActivity extends Activity {private LinearLayout ll_welcome_root;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_welcome);ll_welcome_root = (LinearLayout) findViewById(R.id.ll_welcome_root);//调用启动动画效果showAnimation();}//接受消息、处理消息private Handler handler = new Handler(new Handler.Callback() {@Overridepublic boolean handleMessage(Message msg) {if(msg.what==1) {//接受到消息后,启动另一个界面startActivity(new Intent(WelcomeActivity.this, Guide1Activity.class));}return true;}});/*** 显示动画*/private void showAnimation() {//设置图片背景透明度AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);animation.setDuration(2000);//设置动画监听animation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {//发送延迟的空消息handler.sendEmptyMessageDelayed(1, 1000);}});//欢迎界面的启动动画效果界面ll_welcome_root.startAnimation(animation);}
}
4、然后进入第一个向导界面如图
先看向导界面1的布局文件
activity_guide1.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="com.atguigu.l10_app2.Guide1Activity" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="第一个设置" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:text="下一步" android:onClick="next"/>
</RelativeLayout>
Guide1Activity.java(代码作用是启动界面向导二,以及显示向导界面1和2的切换效果)
package com.atguigu.l10_app2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;public class Guide1Activity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_guide1);}public void next(View v) {startActivity(new Intent(this, Guide2Activity.class));//显示一个界面切换的动画overridePendingTransition(R.anim.anim_right_in, R.anim.anim_left_out);}
}
anim_left_out.xml(当前页面向左滑动)
<?xml version="1.0" encoding="utf-8"?>
<translatexmlns:android="http://schemas.android.com/apk/res/android"android:fromXDelta="0%"android:toXDelta="-100%" android:duration="1000">
</translate>
anim_right_in.xml(下一张页面向左滑动进入到当前页面)
<?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="1000">
</translate>
5、接下来看向导页面2的布局
activity_guide2.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="com.atguigu.l10_app2.Guide1Activity" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="第二个设置" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:onClick="next"android:text="下一步" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/button2"android:layout_alignBottom="@+id/button2"android:layout_alignParentLeft="true"android:text="上一步" android:onClick="pre"/></RelativeLayout>
Guide2Activity.java(向导二的java代码,作用是点击下一张,和点击上一张,显示页面切换的滑动效果)
package com.atguigu.l10_app2;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;public class Guide2Activity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_guide2);}public void next(View v) {startActivity(new Intent(this, MainActivity.class));}public void pre(View v) {finish();//显示一个界面切换的动画overridePendingTransition(R.anim.anim_left_in, R.anim.anim_right_out);}
}
另外附上点击返回上一张界面的xml文件
anim_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translatexmlns:android="http://schemas.android.com/apk/res/android"android:fromXDelta="-100%" android:toXDelta="0%"android:duration="500">
</translate>
anim_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translatexmlns:android="http://schemas.android.com/apk/res/android"android:fromXDelta="0%"android:toXDelta="100%" android:duration="500">
</translate>
6、接下来进入主界面了(这个效果是文本框抖动效果)
activity_main.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="com.atguigu.l10_app2.MainActivity" ><EditTextandroid:id="@+id/et_main_number"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello_world" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/et_main_number"android:text="提交" android:onClick="submit"/></RelativeLayout>
MainActivity.java
package com.atguigu.l10_app2;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;public class MainActivity extends Activity {private EditText et_main_number;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_main_number = (EditText) findViewById(R.id.et_main_number);}public void submit(View v) {//水平振动Animation animation = AnimationUtils.loadAnimation(this, R.anim.shake);et_main_number.startAnimation(animation);}
}
实现水平震动的需要下面xml文件支持
cycle_6.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"android:cycles="6"/>
shake.xml
<?xml version="1.0" encoding="utf-8"?>
<translatexmlns:android="http://schemas.android.com/apk/res/android"android:fromXDelta="0.1"android:toXDelta="1.0"android:duration="500"android:interpolator="@anim/cycle_6"><!-- 配置文件里不能有f出现 -->
</translate>
这篇关于动画综合练习(延迟进入+页面向导+文本框抖动)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!