首页 > 系统 > Android > 正文

Android实现闪屏效果

2019-10-21 21:48:12
字体:
来源:转载
供稿:网友

在登陆一些页面时,通常能看见“一闪而过”效果并进入页面。下面看看是怎样实现这样的效果的吧

首先,在布局里(可以说和平常没有什么不同),划线部分是进度条:

<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"  android:background="@drawable/bg" >   <ImageView    android:id="@+id/welcome"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerHorizontal="true"    android:layout_marginBottom="45dp"    android:src="@drawable/welcome" />   <ProgressBar    android:id="@+id/progressBar"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_below="@+id/welcome"    android:layout_centerHorizontal="true"     android:layout_marginBottom="70dp"    />   <TextView    android:id="@+id/textView1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_below="@+id/progressBar"    android:layout_centerHorizontal="true"    android:padding="@dimen/padding_medium"    android:text="@string/welcome"    android:textAppearance="?android:attr/textAppearanceMedium"    tools:context=".MainActivity" /> </RelativeLayout>

在String中定义:

<resources>   <string name="app_name">ShanP01</string>  <string name="welcome">欢迎加入!/n一起快乐学习!</string>//(/n)实现换行  <string name="menu_settings">Settings</string>  <string name="title_activity_main">MainActivity</string>  <string name="title_study">学习</string>  <string name="title_search">搜查</string>  <string name="title_game">游戏</string>  <string name="title_save">保存</string>  <string name="title_help">帮助</string>  <string name="title_activity_welcome">WelcomeActivity</string> </resources>

如果想在运行项目时不显示标题栏,则在想隐藏标题栏的Activity中加一句即可(在AndroidManifest.xml文件中):

android:theme="@android:style/Theme.NoTitleBar" 

最主要的实现方法:

 

public class WelcomeActivity extends Activity { private ImageView welcomeImage;    @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_welcome);    welcomeImage=(ImageView) this.findViewById(R.id.welcome);    AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f, 1.0f);////定义一个具有淡入效果的对象    alphaAnimation.setDuration(3000);//定义闪屏时间(毫秒)    welcomeImage.startAnimation(alphaAnimation);    alphaAnimation.setAnimationListener(new AnimationListener() {    @Override  public void onAnimationStart(Animation animation) {      }    @Override  public void onAnimationRepeat(Animation animation) {      }    @Override  public void onAnimationEnd(Animation animation) {  Intent intent=new Intent();  intent.setClass(WelcomeActivity.this, MainActivity.class);//定义闪屏效果从哪一界面跳到哪一页面  startActivity(intent);  finish();    } });  }   @Override  public boolean onCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.activity_welcome, menu);    return true;  }  }

其实闪屏效果不止这一种,但这是我认为简便的一种。还有一种:

public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_welcome);     welcomeImage=(ImageView) this.findViewById(R.id.welcome);       AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f);////定义1个具有淡入效果的对象     welcomeImage.startAnimation(alphaAnimation);     new Handler().postDelayed(new Runnable() {          @Override       public void run() {         Intent intent=new Intent();         intent.setClass(WelcomeActivity.this, MainActivity.class);         startActivity(intent);         finish();       }     },3000);//细心不要漏了   } 

你觉得哪一种更适合你呢?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VEVB武林网。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表