Skip to content

Commit

Permalink
改善启动页,增加点点点平滑移动效果
Browse files Browse the repository at this point in the history
  • Loading branch information
ddz-mark committed Oct 15, 2016
1 parent 07f07e6 commit acc66c0
Show file tree
Hide file tree
Showing 20 changed files with 242 additions and 103 deletions.
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
android:theme="@style/AppTheme">

<activity
android:name=".modules.launch.LaunchActivity"
android:name=".modules.launch.SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.Launch">
<intent-filter>
Expand All @@ -33,6 +33,10 @@
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"/>

<activity android:name=".modules.launch.LaunchActivity"
android:theme="@style/AppTheme.NoActionBar"/>

<activity android:name=".modules.login.LoginActivity"></activity>

</application>
Expand Down
32 changes: 4 additions & 28 deletions app/src/main/java/com/dudaizhong/news/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.Display;
import android.view.WindowManager;

import com.dudaizhong.news.base.utils.SharedPreferencesUtil;
import com.dudaizhong.news.common.api.RetrofitSingleton;
import com.dudaizhong.news.di.component.AppComponent;
import com.dudaizhong.news.di.component.DaggerAppComponent;
Expand Down Expand Up @@ -57,6 +58,9 @@ public void onCreate() {
//初始化日志
Logger.init(getPackageName()).hideThreadInfo();

//初始化SharedPreferences
SharedPreferencesUtil.init(this);

/**
* 如果存在SD卡则将缓存写入SD卡,否则写入手机内存
*/
Expand All @@ -79,34 +83,6 @@ public static AppComponent getAppComponent(){
return appComponent;
}

// public void addActivity(Activity activity) {
// if (activitySet == null) {
// activitySet = new HashSet<>();
// }
// activitySet.add(activity);
// }
//
// public void removeActivity(Activity activity) {
// if (null != activitySet) {
// activitySet.remove(activity);
// }
// }
//
// /**
// * 杀掉所有Activity,并退出程序
// */
// public void removeAllActivity() {
// if (null != activitySet) {
// synchronized (activitySet) {
// for (Activity activity : activitySet) {
// activity.finish();
// }
// }
// }
// android.os.Process.killProcess(android.os.Process.myPid());
// System.exit(0);
// }

public void getScreenSize() {
WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/dudaizhong/news/app/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

public class Constants {

public static final String VERSION = "version";
public static final String FIRST = "first";

public static final int ZHIHU_FRAGMENT = 1;
public static final int GANK_FRAGMENT = 2;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.dudaizhong.news.base.utils;

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

import com.dudaizhong.news.app.Constants;

/**
* Created by Dudaizhong on 2016/10/15.
*/

public class SharedPreferencesUtil {


private final static String USER_INFO = "user_info";
private final static String FIRST = "first";

private static Context context;

public static void init(Context context) {
SharedPreferencesUtil.context = context;
}

public static void setVersion(String version) {
SharedPreferences.Editor editor = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE).edit();
editor.putString(Constants.VERSION, version);
editor.apply();
}

public static String getVersion() {
SharedPreferences preferences = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
return preferences.getString(Constants.VERSION, "");
}

/**
* 检查是否加载引导页
* @param first
*/
public static void setFirst(boolean first) {
SharedPreferences.Editor editor = context.getSharedPreferences(FIRST, Context.MODE_PRIVATE).edit();
editor.putBoolean(Constants.FIRST, first);
editor.apply();
}

public static Boolean getFirst() {
SharedPreferences preferences = context.getSharedPreferences(FIRST, Context.MODE_PRIVATE);
return preferences.getBoolean(Constants.FIRST, false);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.dudaizhong.news.modules.launch;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dudaizhong.news.R;
import com.dudaizhong.news.base.BaseActivity;
import com.dudaizhong.news.base.utils.SharedPreferencesUtil;
import com.dudaizhong.news.modules.main.MainActivity;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;

/**
Expand All @@ -24,23 +32,25 @@

public class LaunchActivity extends BaseActivity<LaunchPresenter> implements LaunchContract.View {


@Bind(R.id.viewPager_launch)
ViewPager viewPagerLaunch;
@Bind(R.id.img_vp_page1)
ImageView imgVpPage1;
@Bind(R.id.img_vp_page2)
ImageView imgVpPage2;
@Bind(R.id.img_vp_page3)
ImageView imgVpPage3;
@Bind(R.id.textView_launch)
TextView textViewLaunch;

private ArrayList<View> views;
@Bind(R.id.point_group)
LinearLayout pointGroup;
@Bind(R.id.white_point)
ImageView whitePoint;
@Bind(R.id.re_point)
RelativeLayout rePoint;

private ArrayList<ImageView> views;
private int mPointMargin;
private LaunchAdapter launchAdapter;

// 引导页图片资源
private static final int[] pics = {R.layout.guid_view_1,
R.layout.guid_view_2, R.layout.guid_view_3};
private static final int[] pics = {R.mipmap.view1,
R.mipmap.view2, R.mipmap.view3};

// @Override
// protected LaunchContract.Presenter createPresenter() {
Expand All @@ -59,44 +69,82 @@ protected int getLayoutId() {

@Override
protected void initEventAndData(Bundle savedInstanceState) {

//设置pf成第一次进入
SharedPreferencesUtil.setFirst(true);

views = new ArrayList<>();
for (int i = 0; i < pics.length; i++) {
View view = LayoutInflater.from(this).inflate(pics[i], null);
views.add(view);
// 准备好显示的图片
ImageView image = new ImageView(this);
image.setBackgroundResource(pics[i]);
views.add(image);

// 设置底部小圆点
ImageView point = new ImageView(this);
point.setImageResource(R.drawable.shape_point_normal);

// 设置白点的布局参数
int pointSize = getResources().getDimensionPixelSize(R.dimen.point_size);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(pointSize, pointSize);
whitePoint.setLayoutParams(params1);

// 设置灰色点的布局参数
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(pointSize, pointSize);
if (i > 0) {
params2.leftMargin = getResources().getDimensionPixelSize(R.dimen.point_margin);
}
point.setLayoutParams(params2);

pointGroup.addView(point);
}

launchAdapter = new LaunchAdapter(views);
viewPagerLaunch.setAdapter(launchAdapter);
setListener();
}

private void setListener() {

// 获取视图树对象,通过监听白点布局的显示,然后获取两个圆点之间的距离
whitePoint.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
// 此时layout布局已经显示出来了,可以获取小圆点之间的距离了
mPointMargin = pointGroup.getChildAt(1).getLeft() - pointGroup.getChildAt(0).getLeft();

// 将自己移除掉
whitePoint.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});


/**
* 对View Pager添加监听
*/
viewPagerLaunch.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

// 页面滑动的时候,动态的获取小圆点的左边距
int leftMargin = (int) (mPointMargin * (position + positionOffset));

// 获取布局参数,然后设置布局参数
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) whitePoint.getLayoutParams();
// 修改参数
params.leftMargin = leftMargin;
// 重新设置布局参数
whitePoint.setLayoutParams(params);
}

@Override
public void onPageSelected(int position) {

switch (position) {
case 0:
textViewLaunch.setVisibility(View.INVISIBLE);
imgVpPage1.setImageResource(R.mipmap.circle_selected);
imgVpPage2.setImageResource(R.mipmap.circle_unselected);
imgVpPage3.setImageResource(R.mipmap.circle_unselected);
break;
case 1:
textViewLaunch.setVisibility(View.INVISIBLE);
imgVpPage1.setImageResource(R.mipmap.circle_unselected);
imgVpPage2.setImageResource(R.mipmap.circle_selected);
imgVpPage3.setImageResource(R.mipmap.circle_unselected);
break;
case 2:
textViewLaunch.setVisibility(View.VISIBLE);
imgVpPage1.setImageResource(R.mipmap.circle_unselected);
imgVpPage2.setImageResource(R.mipmap.circle_unselected);
imgVpPage3.setImageResource(R.mipmap.circle_selected);
break;
default:
break;
// 最后一页
if (position == pics.length - 1) {
textViewLaunch.setVisibility(View.VISIBLE);
} else {
textViewLaunch.setVisibility(View.GONE);
}
}

Expand All @@ -105,13 +153,6 @@ public void onPageScrollStateChanged(int state) {

}
});

textViewLaunch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPresenter.jumpToMain();
}
});
}

@Override
Expand All @@ -125,4 +166,10 @@ public void jumpToMain() {
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}

@OnClick(R.id.textView_launch)
public void onClick() {
mPresenter.jumpToMain();
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.dudaizhong.news.modules.launch;

import android.media.Image;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

Expand All @@ -16,9 +18,9 @@

public class LaunchAdapter extends PagerAdapter {

private ArrayList<View> views;
private ArrayList<ImageView> views;

public LaunchAdapter(ArrayList<View> views) {
public LaunchAdapter(ArrayList<ImageView> views) {
this.views = views;
}

Expand Down
Loading

0 comments on commit acc66c0

Please sign in to comment.