android120 zhihuibeijing 开机页面[通俗易懂]

android120 zhihuibeijing 开机页面

大家好,又见面了,我是全栈君。

软件启动旋转动画:

布局:

复制代码
<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:id="@+id/rl_root"
    android:background="@drawable/splash_bg_newyear" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/splash_horse_newyear"
      />

</RelativeLayout>
复制代码

Activity:

复制代码
package com.itheima.zhbj52;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;

import com.itheima.zhbj52.utils.PrefUtils;
/**
 * 旋转首屏页
 */
public class SplashActivity extends Activity {
    RelativeLayout rlRoot;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        rlRoot = (RelativeLayout) findViewById(R.id.rl_root);//rl_root下面是一个ImageView是一张图片。
        startAnim();
        //LibUtils.doSomething();
        //rlRoot.setBackgroundResource(R.drawable.newscenter_press);
    }

    /**
     * 开启动画
     */
    private void startAnim() {
        // 动画集合
        AnimationSet set = new AnimationSet(false);
        // 旋转动画
        RotateAnimation rotate = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);//RELATIVE_TO_SELF表示自身旋转,0.5f表示自身中心。
        rotate.setDuration(1000);// 动画时间
        rotate.setFillAfter(true);// 保持动画最后的状态
        // 缩放动画
        ScaleAnimation scale = new ScaleAnimation(0, 1, 0, 1,//宽和高从0到1,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        scale.setDuration(1000);// 动画时间
        scale.setFillAfter(true);// 保持动画状态
        // 渐变动画
        AlphaAnimation alpha = new AlphaAnimation(0, 1);
        alpha.setDuration(2000);// 动画时间
        alpha.setFillAfter(true);// 保持动画状态
        
        set.addAnimation(rotate);
        set.addAnimation(scale);
        set.addAnimation(alpha);
        // 设置动画监听
        set.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
            @Override
            public void onAnimationRepeat(Animation animation) {
   
   //动画重复的时候调用,
            }
            // 动画执行结束
            @Override
            public void onAnimationEnd(Animation animation) {
                jumpNextPage();
            }
        });
        rlRoot.startAnimation(set);
    }

    /**
     * 跳转下一个页面
     */
    private void jumpNextPage() {
        // 判断之前有没有显示过新手引导
        boolean userGuide = PrefUtils.getBoolean(this, "is_user_guide_showed",
                false);
        if (!userGuide) {
            // 跳转到新手引导页
            startActivity(new Intent(SplashActivity.this, GuideActivity.class));
        } else {
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
        }
        finish();//跳转到新页面后把当前页面结束
    }
}
复制代码

引导页面:

android120 zhihuibeijing 开机页面[通俗易懂]

布局:

复制代码
<?xml version="1.0" encoding="utf-8"?>

<!-- 整体是一个相对布局,这样才会让控件叠加到另一个控件上 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- 第三方包 -->
    <android.support.v4.view.ViewPager
        android:id="@+id/vp_guide"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="60dp"
        android:background="@drawable/btn_guide_selector"   按钮背景图片选择器  
        android:padding="5dp"
        android:text="开始体验"
        android:visibility="invisible"
        android:textColor="@drawable/btn_guide_text_selector" />   按钮文字选择器  

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp" >

        <!-- 3个黑色的圆点 -->
        <LinearLayout
            android:id="@+id/ll_point_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
        <!-- 1个可以移动的红色圆点,默认压住第一个黑色圆点 -->
        <View
            android:id="@+id/view_red_point"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:background="@drawable/shape_point_red" />
    </RelativeLayout>

</RelativeLayout>
复制代码

Activity:

复制代码
package com.itheima.zhbj52;

import java.util.ArrayList;

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.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.itheima.zhbj52.utils.PrefUtils;

/**
 * 新手引导
 */
public class GuideActivity extends Activity {

    private static final int[] mImageIds = new int[] { R.drawable.guide_1,
            R.drawable.guide_2, R.drawable.guide_3 };//三张图片
    private ViewPager vpGuide;
    private ArrayList<ImageView> mImageViewList;
    private LinearLayout llPointGroup;// 引导圆点的父控件
    private int mPointWidth;// 圆点间的距离
    private View viewRedPoint;// 小红点
    private Button btnStart;// 开始体验

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题,在setContentView方法之前调用。
        setContentView(R.layout.activity_guide);
        vpGuide = (ViewPager) findViewById(R.id.vp_guide);
        llPointGroup = (LinearLayout) findViewById(R.id.ll_point_group);//3个黑色的圆点
        viewRedPoint = findViewById(R.id.view_red_point);
        btnStart = (Button) findViewById(R.id.btn_start);//开始体验按钮
        btnStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 更新sp, 表示已经展示了新手引导
                PrefUtils.setBoolean(GuideActivity.this,
                        "is_user_guide_showed", true);
                // 跳转主页面
                startActivity(new Intent(GuideActivity.this, MainActivity.class));
                finish();//当前页面消失
            }
        });
        initViews();
        vpGuide.setAdapter(new GuideAdapter());//ViewPager要设置Adapter
        vpGuide.setOnPageChangeListener(new GuidePageListener());//ViewPager滑动时的监听器
    }

    /**
     * 初始化界面
     */
    private void initViews() {
        mImageViewList = new ArrayList<ImageView>();

        // 初始化引导页的3个页面
        for (int i = 0; i < mImageIds.length; i++) {
            ImageView image = new ImageView(this);
            image.setBackgroundResource(mImageIds[i]);// 设置引导页背景
            mImageViewList.add(image);
        }

        // 动态初始化小圆点,便于代码的扩展。
        for (int i = 0; i < mImageIds.length; i++) {
            View point = new View(this);
            point.setBackgroundResource(R.drawable.shape_point_gray);// 所有的View都有背景,
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    10, 10);//宽高为10像素,因为圆点的父控件是LinearLayout所以这里用LinearLayout.LayoutParams,
            if (i > 0) {
                params.leftMargin = 10;// 设置圆点间隔
            }
            point.setLayoutParams(params);// 设置圆点的大小
            llPointGroup.addView(point);// 将圆点动态添加给线性布局
        }
        //控件出现的时候会经过measure()方法测量控件的宽高,layout()方法用来决定将控件放在哪个地方,ondraw()方法来画控件。
        // 获取视图树(因为整个页面的布局是一层一层的xml节点树), 对layout方法结束的事件进行监听
        llPointGroup.getViewTreeObserver().addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {
                    // 当layout方法执行结束后回调此方法
                    @Override
                    public void onGlobalLayout() {
                        System.out.println("layout 结束");
                        llPointGroup.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(this);//以后不再监听,this是OnGlobalLayoutListener对象,这是一个内部类。
                        mPointWidth = llPointGroup.getChildAt(1).getLeft()
                                - llPointGroup.getChildAt(0).getLeft();//getLeft()是返回圆点的左边到页面左边的距离,getRight()是返回圆点的右边到页面左边的距离。
                        System.out.println("圆点距离:" + mPointWidth);//20
                    }
                });
    }

    /**
     * ViewPager数据适配器
     */
    class GuideAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mImageIds.length;
        }
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
   
   //类似于listView的getView()方法,
            container.addView(mImageViewList.get(position));//mImageViewList.get(position)返回的是ImageView,
            return mImageViewList.get(position);
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);//删除一个
        }
    }

    /**
     * viewpager的滑动监听
     */
    class GuidePageListener implements OnPageChangeListener {
        // 滑动事件,滑动的过程中这个方法一直调用。
        @Override
        public void onPageScrolled(int position, float positionOffset,
                int positionOffsetPixels) {
            //position是当前页面的位置,positionOffset偏移百分比,positionOffsetPixels是偏移的像素,
            //从一个页面滑到第二个页面的时候positionOffset从0到100%并且到第二个页面的时候百分比又变为0,positionOffsetPixels从0到页面的宽度到达第二个页面的时候偏移量又淸0。
            System.out.println("当前位置:" + position + ";百分比:" + positionOffset
            + ";移动距离:" + positionOffsetPixels);
            int len = (int) (mPointWidth * positionOffset) + position
                    * mPointWidth;//mPointWidth * positionOffset是圆点的距离乘以移动的百分比,
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewRedPoint
                    .getLayoutParams();// 获取当前红点的布局参数,因为红色的点是在UI文件写的所以已经有了LayoutParams,而黑色的点是动态生成的没有LayoutParams,红色点的父节点是相对布局,
            params.leftMargin = len;// 设置左边距
            viewRedPoint.setLayoutParams(params);// 重新给小红点设置布局参数
        }

        // 某个页面被选中
        @Override
        public void onPageSelected(int position) {
            if (position == mImageIds.length - 1) {
   
   // 最后一个页面
                btnStart.setVisibility(View.VISIBLE);// 显示开始体验的按钮
            } else {
                btnStart.setVisibility(View.INVISIBLE);
            }
        }

        // 滑动状态发生变化(正在滑动   滑动松开状态,)
        @Override
        public void onPageScrollStateChanged(int state) {

        }

    }

}
复制代码

复制代码
<!-- 图片颜色选择器(drawable文件夹里面) -->

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

    <item android:drawable="@drawable/button_red_pressed" android:state_pressed="true"/>  <!-- 被点击之后 -->
    <item android:drawable="@drawable/button_red_normal"/>

</selector>
复制代码

复制代码
<!-- 文字颜色选择器(drawable文件夹里面) -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="@android:color/black"/>  <!-- 文字点击后是黑色 -->
    <item android:color="@android:color/white"/>  <!-- 文字默认是白色 -->

</selector>
复制代码

复制代码
<!-- 不可以移动的黑色的圆点 -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" > <!-- shape下面的oval是圆形 -->

    <solid android:color="@android:color/darker_gray" />  <!-- 圆形是灰色 -->

</shape>
复制代码

复制代码
<!-- 红色可以移动的圆点 -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <solid android:color="#f00" />    <!-- 圆点颜色是红色 -->

</shape>
复制代码

复制代码
package com.itheima.zhbj52.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SharePreference封装
 * 
 * @author Kevin
 * 
 */
public class PrefUtils {

    public static final String PREF_NAME = "config";

    public static boolean getBoolean(Context ctx, String key,
            boolean defaultValue) {
        SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
                Context.MODE_PRIVATE);
        return sp.getBoolean(key, defaultValue);
    }

    public static void setBoolean(Context ctx, String key, boolean value) {
        SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
                Context.MODE_PRIVATE);
        sp.edit().putBoolean(key, value).commit();
    }
}
复制代码

 android120 zhihuibeijing 开机页面[通俗易懂]

本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5034229.html,如需转载请自行联系原作者

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108051.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 视频标清、高清、全高清的分类分辨率码率帧率参考(附图)

    视频标清、高清、全高清的分类分辨率码率帧率参考(附图)视频标清、高清、全高清的分类分辨率码率帧率参考(附图)视频标清、高清、全高清的分类分辨率码率帧率参考(附图)

    2022年5月6日
    174
  • LaTeX入门级教程

    LaTeX入门级教程    LaTeX(LATEX,音译“拉泰赫”)是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(LeslieLamport)在20世纪80年代初期开发,利用这种格式,即使使用者没有排版和程序设计的知识也可以充分发挥由TeX所提供的强大功能,能在几天,甚至几小时内生成很多具有书籍质量的印刷品。对于生成复杂表格和数学公式,这一点表现得尤为突出。因此它非常适用于生成高印刷质量的科技和数学类…

    2022年7月16日
    11
  • 《LaTeX教程》

    《LaTeX教程》一位小仙女分享给我的LaTeX排版教程书,本人感觉特别好用。人生最快乐的事就是分享你的快乐给别人。独乐不如众乐,愿你我都有所获。链接:https://pan.baidu.com/s/1IkRzLz-Vmt5uL5TksX1lxg提取码:v9ag(“Ifyouhavegreattalents,industrywillimprovethem;ifyouhavebutm…

    2022年7月16日
    11
  • 常用八大测试用例设计方法有哪些_测试用例编写方法

    常用八大测试用例设计方法有哪些_测试用例编写方法1、等价类划分(EquivalancePartitioning)测试的思想:将程序的输入域划分为若干个区域(等价类),并在每个等价类中选择一个具有代表性的元素生成测试用例。该方法是常用的黑盒(BlackboxTesting)测试用例(Testcase)设计方法。等价类划分可有两种不同的情况:有效等价类和无效等价类。有效等价类是指对于程序的规格说明来说是合理的、有意义的输入数据构成的集合,它能检验程序是否可以实现规格说明中所规定的功能需求。无效等价类是指对程序的规格说明是不合理的或无意义的输入数据所

    2022年10月12日
    0
  • c++ stl源码剖析_stl源码 qt源码

    c++ stl源码剖析_stl源码 qt源码C++stl库手写前言序列式关联式容器适配器ListVector函数dequestringstackqueuebitset关联式容器setmultisetmultiset算法库仿函数前言stl版本abcd,四个版本,接口肯定是一样的代码复用性强,效率高,通用性高,vectordeque他有六个组件,空间配置器,容器,迭代器,算法,仿函数,容器适配器容器和算法中间,靠迭代器连接算法为了通用性,有辅助的东西,让算法通用,也就是使用仿函数仿函数就是一个对象容器通过适配器,可以相

    2022年10月15日
    0
  • idea替换所有文件中的内容[通俗易懂]

    idea替换所有文件中的内容[通俗易懂]通过快捷键Ctrl+Shift+R打开窗口,或者通过点击Edit–>Find–>Replaceinpath打开窗口。输入被替换和要替换的内容后,点击replaceall即可替换全部。

    2022年9月29日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号