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" ><ImageViewandroid: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;@Overrideprotected 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() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {//動畫重復的時候調用, }// 動畫執行結束 @Overridepublic 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();//跳轉到新頁面后把當前頁面結束 } }引導頁面:
布局:
<?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.ViewPagerandroid:id="@+id/vp_guide"android:layout_width="match_parent"android:layout_height="match_parent" /><Buttonandroid: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" /> 按鈕文字選擇器 <RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="20dp" ><!-- 3個黑色的圓點 --><LinearLayoutandroid:id="@+id/ll_point_group"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal" ></LinearLayout><!-- 1個可以移動的紅色圓點,默認壓住第一個黑色圓點 --><Viewandroid: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;// 開始體驗 @Overrideprotected 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() {@Overridepublic 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要設置AdaptervpGuide.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方法執行結束后回調此方法 @Overridepublic 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 {@Overridepublic int getCount() {return mImageIds.length;}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;}@Overridepublic Object instantiateItem(ViewGroup container, int position) {//類似于listView的getView()方法,container.addView(mImageViewList.get(position));//mImageViewList.get(position)返回的是ImageView,return mImageViewList.get(position);}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView((View) object);//刪除一個 }}/*** viewpager的滑動監聽*/class GuidePageListener implements OnPageChangeListener {// 滑動事件,滑動的過程中這個方法一直調用。 @Overridepublic 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);// 重新給小紅點設置布局參數 }// 某個頁面被選中 @Overridepublic void onPageSelected(int position) {if (position == mImageIds.length - 1) {// 最后一個頁面btnStart.setVisibility(View.VISIBLE);// 顯示開始體驗的按鈕} else {btnStart.setVisibility(View.INVISIBLE);}}// 滑動狀態發生變化(正在滑動 滑動松開狀態,) @Overridepublic 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();} }?
清單文件:
<activity android:name=".GuideActivity" />
<activity android:name=".MainActivity" />
總結
以上是生活随笔為你收集整理的android120 zhihuibeijing 开机页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Struts 2框架实现文件下载
- 下一篇: iOS-UICollectionView