原创代码禁止转载!
本篇为闪屏页与引导页的实现
知识点:
闪屏页:补间动画的使用,SharedPreferences,定时器的用法
引导页:ViewPager,PagerAdapter,绑定数据源,会移动的小圆点,获取视图树
有问题下面评论,一起探讨;
首先来看下闪屏页的实现:
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
import java.util.Timer;
import java.util.TimerTask;
public class SplashActivity extends AppCompatActivity {
RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
relativeLayout= (RelativeLayout) findViewById(R.id.relativeLayout);
startAnimation();
Timer timer=new Timer();
TimerTask task =new TimerTask() {
@Override
public void run() {
guide();
finish();
}
};
timer.schedule(task,6000);
}
private void startAnimation(){
AnimationSet animationSet=new AnimationSet(false);
RotateAnimation rotate=new RotateAnimation(0,360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
rotate.setDuration(5000);
rotate.setFillAfter(true);
ScaleAnimation scale=new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
scale.setDuration(5000);
scale.setFillAfter(true);
AlphaAnimation alpha=new AlphaAnimation(0,1);
alpha.setDuration(5000);
alpha.setFillAfter(true);
animationSet.addAnimation(rotate);
animationSet.addAnimation(scale);
animationSet.addAnimation(alpha);
relativeLayout.startAnimation(animationSet);
}
public void guide(){
//是否是第一次登陆
SharedPreferences sharedPreferences = this.getSharedPreferences("share", MODE_PRIVATE);
boolean isFirstRun = sharedPreferences.getBoolean("isFirstRun", true);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (isFirstRun){
Log.e("debug", "第一次运行");
editor.putBoolean("isFirstRun", false);
editor.commit();
Intent intent = new Intent();
intent.setClass(SplashActivity.this,GuideActivity.class);
startActivity(intent);
} else {
Log.e("debug", "不是第一次运行");
Intent intent = new Intent();
intent.setClass(SplashActivity.this,MainActivity.class);
startActivity(intent);
}
}
}
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.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.ArrayList;
/**
* Created by Administrator on 2017/5/19 0019.
*/
public class GuideActivity extends Activity{
private static final int[] mImageIds={R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3};
private ViewPager viewPager;
private ArrayList mImageViewList;
private View viewRedPoint;
private LinearLayout llPointGroup;//引导圆点控件
private int mPointWidth;//圆点间的距离
private Button start_button;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
setContentView(R.layout.guide_activity);
viewPager=(ViewPager)findViewById(R.id.vp_guide);
llPointGroup=(LinearLayout) findViewById(R.id.ll_point_group);
viewRedPoint=(View)findViewById(R.id.view_red_point);
viewPager.setAdapter(new GuideAdapter());
viewPager.setOnPageChangeListener(new GuidePageListener());
start_button = (Button) findViewById(R.id.buttonStart);
start_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(GuideActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
initViews();
}
/*
*初始化界面
*/
private void initViews(){
mImageViewList = new ArrayList();
//初始化引导页的3个页面
for (int i=0;i0){
params.leftMargin = 10;//设置圆点间隔
}
point.setLayoutParams(params);//设置圆点大小
llPointGroup.addView(point);//将圆点添加到线性布局
}
//获取视图树
llPointGroup.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
// 当layout执行结束后回调此方法
@Override
public void onGlobalLayout() {
// System.out.println("layout 结束");
llPointGroup.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//measure(测量大小) layout(界面位置) ondraw
mPointWidth = llPointGroup.getChildAt(1).getLeft()-llPointGroup.getChildAt(0).getLeft();
// System.out.println("圆点距离:"+mPointWidth);
}
});
}
/*
*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) {
container.addView(mImageViewList.get(position));
return mImageViewList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
/*
*设置viewpager的滑动监听
*/
class GuidePageListener implements ViewPager.OnPageChangeListener{
//滑动事件
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// System.out.println("当前位置:"+position +";百分比:" + positionOffset +
// ";移动距离:"+positionOffsetPixels);
int len = (int) (mPointWidth * positionOffset) + position * mPointWidth;
RelativeLayout.LayoutParams Params= (RelativeLayout.LayoutParams)
viewRedPoint.getLayoutParams();//获取当前红点布局参数
Params.leftMargin = len;//设置左边距
viewRedPoint.setLayoutParams(Params);//重新给红点设置布局参数
}
//某个页面被选中
@Override
public void onPageSelected(int position) {
if ( position == mImageIds.length-1) {
start_button.setVisibility(View.VISIBLE);
}else{
start_button.setVisibility(View.INVISIBLE);
}
}
//滑动状态发生变化
@Override
public void onPageScrollStateChanged(int state) {
}
}
}