给手机应用增添欢迎页面

给手机应用添加欢迎页面

    现在的手机应用基本上都有一个欢迎页面,一个好的欢迎页面往往能赢得客户的青睐,那么如何添加这个欢迎页面呢?

    手机的欢迎页面一般有两种形式:一种是:ViewPager类型的滑屏效果,通过展示几张精心设计好的图片来展示手机的功能,另一种是单张图片的动画演示,如微信的欢迎页面。

   这两种欢迎页面都是可以轻松实现的,下面我用代码实现微信效果的那种单张图片的动画效果。

  1.     给应用写个欢迎页的XML文件,布局和图片自己可以随意设计。

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

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

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="#fff"

        android:orientation="vertical" >

        <ImageView

            android:id="@+id/imageView_welcome"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:src="@drawable/welcome" />

    </LinearLayout>

  2.     需要写个Activity来加载这个欢迎页面

    public class WelcomeActivity extends Activity implements AnimationListener {

        private ImageView  imageView = null;

        private Animation alphaAnimation = null;

        @Override

        protected void onCreate(Bundle savedInstanceState) {

            // TODO Auto-generated method stub

            super.onCreate(savedInstanceState);

            setContentView(R.layout.welcome);

            imageView = (ImageView)findViewById(R.id.imageView_welcome);

            alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.welcome_alpha);

            alphaAnimation.setFillEnabled(true); //启动Fill保持

            alphaAnimation.setFillAfter(true);  //设置动画的最后一帧是保持在View上面

            imageView.setAnimation(alphaAnimation);

            alphaAnimation.setAnimationListener(this);  //为动画设置监听

        }

      @Override

     public void onAnimationStart(Animation animation) {

         // TODO Auto-generated method stub

     }

     @Override

     public void onAnimationEnd(Animation animation) {

        //动画结束时结束欢迎界面并转到软件的主界面

        Intent intent = new Intent(this, MainActivity.class);

        startActivity(intent);

        this.finish();

    }

    @Override

    public void onAnimationRepeat(Animation animation) {

        // TODO Auto-generated method stub

    }

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        //在欢迎界面屏蔽BACK键,在退出的时候就不会返回这个欢迎页

        if(keyCode==KeyEvent.KEYCODE_BACK) {

          return false;

          }

        return false;

        }

      }

  3.   给欢迎页添加动画,动画的种类有很多种,自己可以随意设计。注意:添加动画时需要在res目录下新建文件夹:anim,动画的文件放在这里面。

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

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

    android:interpolator="@android:anim/accelerate_interpolator">

    <alpha 

        android:fromAlpha="0.0"

        android:toAlpha="1.0"

        android:duration="500" 

    />

    <alpha 

        android:fromAlpha="1.0"

        android:toAlpha="0.2"

        android:startOffset="1000"

        android:duration="300" 

    />

    </set>

==========================  Android图形图像与动画之AnimationListener简介

 

 

就像Button控件有监听器一样,动画效果也有监听器,只需要实现AnimationListener就可以实现对动画效果的监听,其中需要重载三个函数,就是下面的这几个函数:

 

private class MyListenr implements AnimationListener{

 
        @Override
        public void onAnimationEnd(Animation arg0) {
            // TODO Auto-generated method stub
 
        }
 
        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub
 
        }
 
        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub
 
        }
         

    }

其中第一个函数的意思是在动画执行完之后需要开发者做什么,第二个函数的意思是在动画重复执行的过程中应该做什么,第三个函数的意思是当动画开始执行时有什么动作发生。

 

 

 

 

=========== Android数据存取 getSharedPreferences

 

 

getSharedPreferences(String name, int mode)

 

得到名为‘name’的偏好文件。同时你可以更改和返回他的值。任何调用者在调用同样名字的偏好文件时只有一个实例返回,这就意味着这些调用者都可以看到其他调用者做出的更改。

 

name为本组件的配置文件名( 自己定义,也就是一个文件名),当这个文件不存在时,直接创建,如果已经存在,则直接使用,

mode为操作模式,默认的模式为0或MODE_PRIVATE,还可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLEmode指定为MODE_PRIVATE,则该配置文件只能被自己的应用程序访问。mode指定为MODE_WORLD_READABLE,则该配置文件除了自己访问外还可以被其它应该程序读取。mode指定为MODE_WORLD_WRITEABLE,则该配置文件除了自己访问外还可以被其它应该程序读取和写入

 

 

 

SharedPreferences是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下:

 

 

 

 

将数据保存至SharedPreferences:
SharedPreferences preferences=getSharedPreferences("user",Context.MODE_PRIVATE);
Editor editor=preferences.edit();
String name="xixi";
String age="22";
editor.putString("name", name);
editor.putString("age", age);
editor.commit();
 
从SharedPreferences获取数据:
SharedPreferences preferences=getSharedPreferences("user", Context.MODE_PRIVATE);
String name=preferences.getString("name", "defaultname");
String age=preferences.getString("age", "0");

 

 

================ 实例

 

WelcomeActivity.java

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.widget.FrameLayout;
import android.widget.ImageView;


public class WelcomeActivity extends Activity implements AnimationListener {

    private SharedPreferences mConfig;
    private String mFirst = "isfirst";
   
    private FrameLayout mFrameLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_welcome);
        mFrameLayout = (FrameLayout)findViewById(R.id.welcome_frame);
        mConfig = this.getSharedPreferences(Util.CONFIG_FILE, MODE_PRIVATE);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (mConfig.contains(mFirst)) {
            if (mConfig.getBoolean(mFirst, false)) {
                startIntroduce();
            } else {
                startWelcome();
            }
        }else{
            startIntroduce();//进入图片动画简介
        }
        setIsFirst(false);
    }
   
    private void startWelcome(){
        AnimationSet animationset = new AnimationSet(true);
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 1);
        alphaAnimation.setDuration(Util.SPLASH_DISPLAY_LENGHT);
        animationset.addAnimation(alphaAnimation);
        animationset.setAnimationListener(this);
        mFrameLayout.startAnimation(animationset);
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub
        mFrameLayout.setVisibility(View.INVISIBLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        startMain();
    }
   
    private void setIsFirst(boolean isFirst){
        Editor edit = mConfig.edit();
        edit.putBoolean(mFirst, isFirst);
        edit.commit();
    }

    private void startMain() {
        Intent mainIntent = new Intent(WelcomeActivity.this,
                CpsMediaMainActivity.class);
        startActivity(mainIntent);
        finish();
        overridePendingTransition(R.anim.news_dync_in_from_left,R.anim.news_dync_out_to_right);
    }

    private void startIntroduce() {
        Intent mainIntent = new Intent(WelcomeActivity.this,
                IntroduceActivity.class);
        startActivity(mainIntent);
        finish();
//        overridePendingTransition(R.anim.news_dync_in_from_left,R.anim.news_dync_out_to_right);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onBackPressed() {
       
    }

}

 

 

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/welcome_frame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/welcome_a"
    android:orientation="vertical" >
    
</FrameLayout>

 

 

IntroduceActivity.java

 

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class IntroduceActivity extends Activity implements OnPageChangeListener,OnClickListener {

    private ViewPager mViewPager;

    private ImageView mPageA;
    private ImageView mPageB;
    private ImageView mPageC;
    private ImageView mPageD;
   
    private List<View> mViewList;
   
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_introduce);
        mViewPager = (ViewPager) findViewById(R.id.introduce_viewpager);

        mPageA = (ImageView) findViewById(R.id.page_a);
        mPageB = (ImageView) findViewById(R.id.page_b);
        mPageC = (ImageView) findViewById(R.id.page_c);
        mPageD = (ImageView) findViewById(R.id.page_d);

        mViewPager.setOnPageChangeListener(this);
       
        LayoutInflater li = LayoutInflater.from(this);
        View viewA = li.inflate(R.layout.layout_introduce_a, null);
        View viewB = li.inflate(R.layout.layout_introduce_b, null);
        View viewC = li.inflate(R.layout.layout_introduce_c, null);
        View viewD = li.inflate(R.layout.layout_introduce_d, null);
        viewD.setOnClickListener(this);
        mViewList = new ArrayList<View>();
        mViewList.add(viewA);
        mViewList.add(viewB);
        mViewList.add(viewC);
        mViewList.add(viewD);
       
        mAdapter = new MyAdapter();
       
        mViewPager.setAdapter(mAdapter);
    }
   
    class MyAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mViewList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager)container).removeView(mViewList.get(position));
        }

        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager)container).addView(mViewList.get(position));
            return mViewList.get(position);
        }
       
    }

    @Override
    public void onPageScrollStateChanged(int page) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int page, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int page) {
        switch (page) {
        case 0:
            mPageA.setImageResource(R.drawable.page_now);
            mPageB.setImageResource(R.drawable.page);
            break;
        case 1:
            mPageA.setImageResource(R.drawable.page);
            mPageB.setImageResource(R.drawable.page_now);
            mPageC.setImageResource(R.drawable.page);
            break;
        case 2:
            mPageB.setImageResource(R.drawable.page);
            mPageC.setImageResource(R.drawable.page_now);
            mPageD.setImageResource(R.drawable.page);
            break;
        case 3:
            mPageC.setImageResource(R.drawable.page);
            mPageD.setImageResource(R.drawable.page_now);
            break;
        default:
            break;
        }
    }

    @Override
    public void onClick(View v) {
        Intent mainIntent = new Intent(IntroduceActivity.this,
                CpsMediaMainActivity.class);
        startActivity(mainIntent);
        finish();
    }

}

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/introduce_viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
    </android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginBottom="30dp"
        android:gravity="center_horizontal" >

        <ImageView
            android:id="@+id/page_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="matrix"
            android:src="@drawable/page_now" />

        <ImageView
            android:id="@+id/page_b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:scaleType="matrix"
            android:src="@drawable/page" />

        <ImageView
            android:id="@+id/page_c"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:scaleType="matrix"
            android:src="@drawable/page" />

        <ImageView
            android:id="@+id/page_d"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:scaleType="matrix"
            android:src="@drawable/page" />
    </LinearLayout>

</FrameLayout>