android软件引导界面

来源:互联网 发布:nginx和squid代理 编辑:程序博客网 时间:2024/05/13 02:19
软件的引导界面


·大多数软件在安装成功后的第一次运行,或版本更新后的第一次运行时
会使用3-5张图片对软件的使用,版本更新作简单的介绍或演示,这样的界面称之为“引导界面”


·在切换显示到最后一张图片时,点击按钮即可进入软件的主界面,且以后再次运行该应用程序时都不再显示引导界面


案例相关知识点包括:
·手势判断
·ImageSwitcher控件;
·动画;

·SharedPreferences


Guide的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=".GuideActivity" >    <ImageSwitcher        android:id="@+id/imageSwitcher"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_alignParentLeft="true" >    </ImageSwitcher>    <Button        android:id="@+id/btn_go_to_main"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerInParent="true"        android:layout_marginBottom="50dp"        android:onClick="startMain"        android:text="进入主界面"        android:visibility="gone" /></RelativeLayout>



GuideActivity.java

package com.example;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.graphics.Point;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ImageView.ScaleType;import android.widget.ViewSwitcher.ViewFactory;public class GuideActivity extends Activity {private ImageSwitcher mImageSwitcher;private int[] mImgResIds;private int mCurrentImageIndx;private Point mDownPoint = new Point();private Animation mRight2LeftIn,mRight2leftOut;private String filename = "config";private String firstRunning = "true";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_guide);SharedPreferences sp = getSharedPreferences(filename, MODE_PRIVATE);String config = sp.getString(firstRunning, null);if(config != null){Intent intent = new Intent(this, MainActivity.class);startActivity(intent);finish();}mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);mImgResIds = new int[]{R.drawable.image01,R.drawable.image02,R.drawable.image03,R.drawable.image04};/* * 给使用setFactoru给mImageSwitcher添加两个子级控件 */mImageSwitcher.setFactory(new ViewFactory() {@Overridepublic View makeView() {// TODO Auto-generated method stubImageView v = new ImageView(getApplicationContext());//这里是内部类所以不能用this 要用getApplicationContext()v.setScaleType(ScaleType.FIT_XY);v.setImageResource(mImgResIds[mCurrentImageIndx]);return v;}});mRight2LeftIn = AnimationUtils.loadAnimation(this, R.anim.right2left_in);mRight2leftOut = AnimationUtils.loadAnimation(this, R.anim.right2left_out);}/** * 判断手势 */@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_DOWN:mDownPoint.x = (int) event.getX();break;case MotionEvent.ACTION_UP:if(event.getX() - mDownPoint.x >10){//left ->right :previous}if(mDownPoint.x - event.getX() >10){// right->left :nextif(mCurrentImageIndx < mImgResIds.length - 1){mCurrentImageIndx++;((ImageView)mImageSwitcher.getNextView()).setImageResource(mImgResIds[mCurrentImageIndx]);mImageSwitcher.setInAnimation(mRight2LeftIn);mImageSwitcher.setOutAnimation(mRight2leftOut);mImageSwitcher.showNext();/* * 判断如果到最后一张图片就显示按钮 * */if(mCurrentImageIndx == mImgResIds.length - 1){findViewById(R.id.btn_go_to_main).setVisibility(View.VISIBLE);}}}break;default:break;}return super.onTouchEvent(event);}public void startMain(View v){SharedPreferences sp = getSharedPreferences(filename, MODE_PRIVATE);Editor editor = sp.edit();editor.putString(firstRunning, "false");editor.commit();Intent intent = new Intent(this,MainActivity.class);startActivity(intent);finish();//引导界面的Activity就会退出,回退栈中也会清除该Activity,点击回退按钮也不会回退到引导界面}}

切换动画效果实现xml

right2left_in

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

right2left_out.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" ><!-- 出场动画 1.5秒 -->    <translate        android:duration="1500"        android:fromXDelta="0"        android:toXDelta="-100%" /></set>





0 0
原创粉丝点击