黑马北京新闻项目连载(1)---引导界面

2024-08-31 23:38

本文主要是介绍黑马北京新闻项目连载(1)---引导界面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

看引导界面效果


先看欢迎界面的布局(动画效果)


activity_welcome.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:id="@+id/rl_root"  
  5.     android:background="@drawable/splash_bg_newyear" >  
  6.   
  7.     <ImageView  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:src="@drawable/splash_horse_newyear" />  
  11.   
  12. </RelativeLayout>  


再看欢迎界面的activity

WelcomeActivity.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.bjnews.activity;  
  2.   
  3. import com.example.bjnews.R;  
  4. import com.example.bjnews.util.SPUtil;  
  5.   
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.animation.AlphaAnimation;  
  9. import android.view.animation.Animation;  
  10. import android.view.animation.Animation.AnimationListener;  
  11. import android.view.animation.AnimationSet;  
  12. import android.view.animation.RotateAnimation;  
  13. import android.view.animation.ScaleAnimation;  
  14. import android.widget.RelativeLayout;  
  15. import android.widget.Toast;  
  16. import android.app.Activity;  
  17. import android.content.Intent;  
  18.   
  19. public class WelcomeActivity extends Activity {  
  20.     private RelativeLayout rl_root;  
  21.       
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_welcome);  
  26.         init();  
  27.     }  
  28.   
  29.     private void init() {  
  30.         rl_root = (RelativeLayout) findViewById(R.id.rl_root);  
  31.         //旋转  
  32.         RotateAnimation rotateAnimation = new RotateAnimation(0360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  
  33.         rotateAnimation.setDuration(2000);  
  34.         rotateAnimation.setFillAfter(true);  
  35.         //缩放  
  36.         ScaleAnimation scaleAnimation = new ScaleAnimation(0101, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  
  37.         scaleAnimation.setDuration(2000);  
  38.         scaleAnimation.setFillAfter(true);  
  39.         //透明度  
  40.         AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);  
  41.         alphaAnimation.setDuration(2000);  
  42.         alphaAnimation.setFillAfter(true);  
  43.         //添加动画  
  44.         AnimationSet animationSet = new AnimationSet(false);  
  45.         animationSet.addAnimation(rotateAnimation);  
  46.         animationSet.addAnimation(scaleAnimation);  
  47.         animationSet.addAnimation(alphaAnimation);  
  48.         rl_root.startAnimation(animationSet);  
  49.           
  50.         //动画监听事件  
  51.         animationSet.setAnimationListener(new MyAnimation());  
  52.     }  
  53.     class MyAnimation implements AnimationListener{  
  54.   
  55.         @Override  
  56.         public void onAnimationEnd(Animation animation) {  
  57.             //是否已经进入过主界面(默认没有进入过)  
  58.             boolean isEnterMain = SPUtil.getInstance(WelcomeActivity.this).getboolean("isEnterMain"false);  
  59.               
  60.             Log.e("TAG""isEnterMain"+isEnterMain);  
  61.             if (isEnterMain) {  
  62.                 //进入过,进入到主界面  
  63.                 startActivity(new Intent(WelcomeActivity.this,MainActivity.class ));  
  64.                 Log.e("TAG""Main");  
  65.             }else {  
  66.                 //没进入过,计入向导界面  
  67.                 Log.e("TAG""guide");  
  68.                 startActivity(new Intent(WelcomeActivity.this,GuideActivity.class ));  
  69.             }  
  70.             finish();  
  71.         }  
  72.   
  73.         @Override  
  74.         public void onAnimationRepeat(Animation animation) {  
  75.               
  76.         }  
  77.   
  78.         @Override  
  79.         public void onAnimationStart(Animation animation) {  
  80.               
  81.         }  
  82.           
  83.     }  
  84. }  
另外为上对象提供一个工具类(用来保存是否是第一次进入应用)

SPUtil.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.bjnews.util;  
  2.   
  3. import android.content.Context;  
  4. import android.content.SharedPreferences;  
  5. import android.content.SharedPreferences.Editor;  
  6.   
  7. public class SPUtil {  
  8.     // 构造器私有  
  9.     private SPUtil() {  
  10.     }  
  11.   
  12.     // 私有、静态实例  
  13.     private static SPUtil instance = new SPUtil();  
  14.   
  15.     // 公共、静态方法获取实例  
  16.     public static SPUtil getInstance(Context context) {  
  17.         if (sp == null) {  
  18.             sp = context.getSharedPreferences("atguigu", Context.MODE_PRIVATE);  
  19.         }  
  20.         return instance;  
  21.     }  
  22.   
  23.     private static SharedPreferences sp;  
  24.   
  25.     // 保存数据的方法  
  26.     public void put(String key, Object defValue) {  
  27.           
  28.         Editor edit = sp.edit();  
  29.         if (defValue instanceof Boolean) {  
  30.             edit.putBoolean(key, (Boolean) defValue);  
  31.         }  
  32.         if (defValue instanceof String) {  
  33.             edit.putString(key, (String) defValue);  
  34.         }  
  35.         if (defValue instanceof Integer) {  
  36.             edit.putInt(key, (Integer) defValue);  
  37.         }  
  38.         edit.commit();  
  39.           
  40.     }  
  41.   
  42.     // 获取数据  
  43.     public String getString(String key, String defValue) {  
  44.         return sp.getString(key, defValue);  
  45.     }  
  46.   
  47.     public int getInt(String key, int defValue) {  
  48.         return sp.getInt(key, defValue);  
  49.     }  
  50.   
  51.     public boolean getboolean(String key, boolean defValue) {  
  52.         return sp.getBoolean(key, defValue);  
  53.     }  
  54. }  

接下来开始进行引导界面的操作了 

先看布局

activity_guide.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" >  
  4.   
  5.     <android.support.v4.view.ViewPager  
  6.         android:id="@+id/viewpager"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent" >  
  9.     </android.support.v4.view.ViewPager>  
  10.   
  11.     <Button  
  12.         android:id="@+id/btn_enterMain"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentBottom="true"  
  16.         android:layout_centerHorizontal="true"  
  17.         android:layout_marginBottom="100dp"  
  18.         android:background="@drawable/enter_main_button_selector_bg"  
  19.         android:text="开始体验"  
  20.         android:textColor="@drawable/enter_main_button_text_selector"  
  21.         android:visibility="gone" />  
  22.   
  23.     <RelativeLayout  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_alignParentBottom="true"  
  27.         android:layout_centerHorizontal="true"  
  28.         android:layout_marginBottom="80dp" >  
  29.   
  30.         <LinearLayout  
  31.             android:id="@+id/ll_point_group"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="wrap_content"  
  34.             android:orientation="horizontal" >  
  35.         </LinearLayout>  
  36.   
  37.         <View  
  38.             android:id="@+id/red_point"  
  39.             android:layout_width="10dp"  
  40.             android:layout_height="10dp"  
  41.             android:background="@drawable/point_red"/>  
  42.     </RelativeLayout>  
  43.   
  44. </RelativeLayout>  
针对引导界面还需要提供4个xml文件(进入体验button的按下效果切换,以及还有3个状态切换圆点)
enter_main_button_selector_bg.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <item android:state_checked="true" android:drawable="@drawable/button_red_pressed"></item>  
  4.     <item android:state_checked="false" android:drawable="@drawable/button_red_normal"></item>  
  5.   
  6. </selector>  

enter_main_button_text_selector.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <item android:state_checked="true" android:color="@android:color/black"></item>  
  4.     <item android:state_checked="false" android:color="@android:color/white"></item>  
  5.   
  6. </selector>  

point_normal.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">  
  3.     <solid android:color="#55000000"/>  
  4. </shape>  
point_red.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">  
  3.     <solid android:color="#ff0000"/>  
  4. </shape>  

接下来就该注意的向导界面代码了

GuideActivity.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.bjnews.activity;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.support.v4.view.PagerAdapter;  
  9.   
  10. import android.support.v4.view.ViewPager;  
  11. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  12. import android.view.View;  
  13. import android.view.ViewGroup;  
  14. import android.view.ViewTreeObserver.OnGlobalLayoutListener;  
  15. import android.widget.Button;  
  16. import android.widget.ImageView;  
  17. import android.widget.LinearLayout;  
  18. import android.widget.LinearLayout.LayoutParams;  
  19. import android.widget.RelativeLayout;  
  20.   
  21. import com.example.bjnews.R;  
  22. import com.example.bjnews.util.SPUtil;  
  23.   
  24. public class GuideActivity extends Activity {  
  25.     private ViewPager viewpager;// 三个滑动的页面  
  26.     private ArrayList<ImageView> imageViews;// 三个滑动页面对应的图片集合  
  27.     private Button btn_enterMain;// 开始体验按钮  
  28.   
  29.     private LinearLayout ll_point_group;// 下方的三个显示圆点为  
  30.     private View red_point;// 当前页面的高亮显示圆点  
  31.   
  32.     private int leftMax;// 2个原点之前的距离  
  33.   
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_guide);  
  38.         // 获取点的集合  
  39.         ll_point_group = (LinearLayout) findViewById(R.id.ll_point_group);  
  40.         // 获取高亮的红点  
  41.         red_point = findViewById(R.id.red_point);  
  42.         // 获取viewpager  
  43.         viewpager = (ViewPager) findViewById(R.id.viewpager);  
  44.         // 获取button(开始体验)  
  45.         btn_enterMain = (Button) findViewById(R.id.btn_enterMain);  
  46.         // 创建集合  
  47.         imageViews = new ArrayList<ImageView>();  
  48.   
  49.         // 准备数据  
  50.         int[] ids = { R.drawable.guide_1, R.drawable.guide_2,  
  51.                 R.drawable.guide_3 };  
  52.         for (int i = 0; i < ids.length; i++) {  
  53.             ImageView imageView = new ImageView(this);// 创建图片对象  
  54.             imageView.setBackgroundResource(ids[i]);// 为图片设置背景  
  55.             imageViews.add(imageView);// 添加图片  
  56.             View point = new View(this);// 添加下标点-创建点击  
  57.             LayoutParams params = new LayoutParams(1010);// 创建布局参数  
  58.             // 除开第0个点,其他的都要距离左边有10个像素  
  59.             if (i != 0) {  
  60.                 params.leftMargin = 10;  
  61.             }  
  62.   
  63.             point.setBackgroundResource(R.drawable.point_normal);// 添加背景图片  
  64.   
  65.             point.setLayoutParams(params);// 设置布局参数  
  66.   
  67.             ll_point_group.addView(point);// 添加指示点击  
  68.   
  69.         }  
  70.   
  71.         // 设置适配器  
  72.         viewpager.setAdapter(new viewpagerAdapter());  
  73.   
  74.         // 监听当onLayout方法执行的时候再去  
  75.         red_point.getViewTreeObserver().addOnGlobalLayoutListener(  
  76.                 new OnGlobalLayoutListener() {  
  77.                     @Override  
  78.                     public void onGlobalLayout() {  
  79.   
  80.                         // 取消注册监听--因为孩子也会调用  
  81.                         red_point.getViewTreeObserver()  
  82.                                 .removeGlobalOnLayoutListener(this);  
  83.                         // 两点间的间距  
  84.                         leftMax = ll_point_group.getChildAt(1).getLeft()  
  85.                                 - ll_point_group.getChildAt(0).getLeft();  
  86.                     }  
  87.                 });  
  88.   
  89.         /** 
  90.          * 设置页面改变监听 
  91.          */  
  92.         viewpager.setOnPageChangeListener(new OnPageChangeListener() {  
  93.   
  94.             @Override  
  95.             public void onPageSelected(int position) {  
  96.                 //只有第三种页面显示进入按钮  
  97.                 if (position == 2) {  
  98.                     btn_enterMain.setVisibility(View.VISIBLE);  
  99.                 } else {  
  100.                     btn_enterMain.setVisibility(View.GONE);  
  101.                 }  
  102.             }  
  103.   
  104.             /* 
  105.              * 位置 屏幕上滑动的百分比 滑动的显示 
  106.              */  
  107.             @Override  
  108.             public void onPageScrolled(int position, float positionOffset,  
  109.                     int positionOffsetPixels) {  
  110.                 // 计算要滑动的距离=间距*在屏幕上滑动的百分比  
  111.                 int distance = (int) (leftMax * (positionOffset + position));  
  112.                 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(  
  113.                         1010);  
  114.                 params.leftMargin = distance;  
  115.                 red_point.setLayoutParams(params);  
  116.             }  
  117.   
  118.             @Override  
  119.             public void onPageScrollStateChanged(int arg0) {  
  120.   
  121.             }  
  122.         });  
  123.         /** 
  124.          * 为开始体验button添加点击事件 
  125.          */  
  126.         btn_enterMain.setOnClickListener(new View.OnClickListener() {  
  127.   
  128.             @Override  
  129.             public void onClick(View v) {  
  130.                 // 标记为true  
  131.                 SPUtil.getInstance(GuideActivity.this).put("isEnterMain"true);  
  132.                 // 进入主界面  
  133.                 startActivity(new Intent(GuideActivity.this, MainActivity.class));  
  134.                 // 干掉自己  
  135.                 finish();  
  136.             }  
  137.         });  
  138.     }  
  139.   
  140.     class viewpagerAdapter extends PagerAdapter {  
  141.         @Override  
  142.         public int getCount() {  
  143.             return imageViews.size();  
  144.         }  
  145.   
  146.         @Override  
  147.         public boolean isViewFromObject(View arg0, Object arg1) {  
  148.             return (arg0 == arg1);  
  149.         }  
  150.   
  151.         @Override  
  152.         public void destroyItem(ViewGroup container, int position, Object object) {  
  153.             container.removeView((View) object);  
  154.         }  
  155.   
  156.         @Override  
  157.         public Object instantiateItem(ViewGroup container, int position) {  
  158.             ImageView imageView = imageViews.get(position);  
  159.             container.addView(imageView);  
  160.   
  161.             return imageView;  
  162.         }  
  163.     }  
  164. }  


另外最后还有一个主界面测试用(布局文件没有东西)

activity_main.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" >  
  4.   
  5. </RelativeLayout>  

MainActivity.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.bjnews.activity;  
  2.   
  3. import com.example.bjnews.R;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7.   
  8. public class MainActivity extends Activity {  
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         // TODO Auto-generated method stub  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.     }  
  15. }  

这篇关于黑马北京新闻项目连载(1)---引导界面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125327

相关文章

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

如何用Docker运行Django项目

本章教程,介绍如何用Docker创建一个Django,并运行能够访问。 一、拉取镜像 这里我们使用python3.11版本的docker镜像 docker pull python:3.11 二、运行容器 这里我们将容器内部的8080端口,映射到宿主机的80端口上。 docker run -itd --name python311 -p

在cscode中通过maven创建java项目

在cscode中创建java项目 可以通过博客完成maven的导入 建立maven项目 使用快捷键 Ctrl + Shift + P 建立一个 Maven 项目 1 Ctrl + Shift + P 打开输入框2 输入 "> java create"3 选择 maven4 选择 No Archetype5 输入 域名6 输入项目名称7 建立一个文件目录存放项目,文件名一般为项目名8 确定

Vue3项目开发——新闻发布管理系统(六)

文章目录 八、首页设计开发1、页面设计2、登录访问拦截实现3、用户基本信息显示①封装用户基本信息获取接口②用户基本信息存储③用户基本信息调用④用户基本信息动态渲染 4、退出功能实现①注册点击事件②添加退出功能③数据清理 5、代码下载 八、首页设计开发 登录成功后,系统就进入了首页。接下来,也就进行首页的开发了。 1、页面设计 系统页面主要分为三部分,左侧为系统的菜单栏,右侧

SpringBoot项目是如何启动

启动步骤 概念 运行main方法,初始化SpringApplication 从spring.factories读取listener ApplicationContentInitializer运行run方法读取环境变量,配置信息创建SpringApplication上下文预初始化上下文,将启动类作为配置类进行读取调用 refresh 加载 IOC容器,加载所有的自动配置类,创建容器在这个过程

Maven创建项目中的groupId, artifactId, 和 version的意思

文章目录 groupIdartifactIdversionname groupId 定义:groupId 是 Maven 项目坐标的第一个部分,它通常表示项目的组织或公司的域名反转写法。例如,如果你为公司 example.com 开发软件,groupId 可能是 com.example。作用:groupId 被用来组织和分组相关的 Maven artifacts,这样可以避免

2. 下载rknn-toolkit2项目

官网链接: https://github.com/airockchip/rknn-toolkit2 安装好git:[[1. Git的安装]] 下载项目: git clone https://github.com/airockchip/rknn-toolkit2.git 或者直接去github下载压缩文件,解压即可。

9.8javaweb项目总结

1.主界面用户信息显示 登录成功后,将用户信息存储在记录在 localStorage中,然后进入界面之前通过js来渲染主界面 存储用户信息 将用户信息渲染在主界面上,并且头像设置跳转,到个人资料界面 这里数据库中还没有设置相关信息 2.模糊查找 检测输入框是否有变更,有的话调用方法,进行查找 发送检测请求,然后接收的时候设置最多显示四个类似的搜索结果

maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令

maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令 在日常的工作中由于各种原因,会出现这样一种情况,某些项目并没有打包至mvnrepository。如果采用原始直接打包放到lib目录的方式进行处理,便对项目的管理带来一些不必要的麻烦。例如版本升级后需要重新打包并,替换原有jar包等等一些额外的工作量和麻烦。为了避免这些不必要的麻烦,通常我们

html css jquery选项卡 代码练习小项目

在学习 html 和 css jquery 结合使用的时候 做好是能尝试做一些简单的小功能,来提高自己的 逻辑能力,熟悉代码的编写语法 下面分享一段代码 使用html css jquery选项卡 代码练习 <div class="box"><dl class="tab"><dd class="active">手机</dd><dd>家电</dd><dd>服装</dd><dd>数码</dd><dd