欢迎页开发

动画效果

  • RotateAnimation
  • ScaleAnimation
  • AlphaAnimation
  • AnimationSet
    // 初始化欢迎页面的动画
    private void initViews() {
    RelativeLayout rlRoot = (RelativeLayout) findViewById(R.id.rl_root);
RotateAnimation rotate = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(1000);// 动画执行时间
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(1000);
scale.setFillAfter(true);

AlphaAnimation alpha = new AlphaAnimation(0, 1);
alpha.setDuration(2000);
alpha.setFillAfter(true);

AnimationSet set = new AnimationSet(false);
set.addAnimation(rotate);
set.addAnimation(scale);
set.addAnimation(alpha);

rlRoot.startAnimation(set);

}

替换logo

引导页开发

SharePreference工具类实现

记录是否要展示新手引导, 默认需要展现, 一旦用户点击开始体验按钮进入主页面时, 下次不再展现

布局页面

ViewPager + 开始体验Button + LinearLayout(位置指示器)

"开始体验"按钮效果处理

1. Button背景selector
2. 文字颜色selector

<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>

android:textColor="@drawable/txt_guide_selector"

去除标题

requestWindowFeature(Window.FEATURE_NO_TITLE);// 去除标题,必须在setContentView之前调用

ViewPager填充数据

// 初始化ViewPager的数据
private void initData() {
int[] imageResIDs = { R.drawable.guide_1, R.drawable.guide_2,
R.drawable.guide_3 };

mImageList = new ArrayList<ImageView>();
for (int i = 0; i < imageResIDs.length; i++) {
ImageView image = new ImageView(this);
image.setBackgroundResource(imageResIDs[i]);// 注意设置背景, 才可以填充屏幕
mImageList.add(image);
}
}

增加位置指示器

  • 使用shape绘制选中和不选中的圆形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@android:color/darker_gray"/>
</shape>
  • 根据新手引导页数量动态添加圆点
View point = new View(this);
point.setBackgroundResource(R.drawable.shape_point_default);
LayoutParams params = new LayoutParams(10, 10);

if(i!=0) {
params.leftMargin = 10;
}

point.setLayoutParams(params);
llPointGroup.addView(point);
  • 添加红色圆点
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp" >

<LinearLayout
android:id="@+id/ll_point_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>

<View
android:id="@+id/view_red_point"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/shape_point_selected" />
</RelativeLayout>
  • 计算两个圆点之间的距离
1. 视图树的解释(可以参照hierarchyviewer这个sdk工具)

// measure -> layout -> draw
// 获得视图树观察者, 观察当整个布局的layout时的事件
viewRedPoint.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
//此方法只需要执行一次就可以: 把当前的监听事件从视图树中移除掉, 以后就不会在回调此事件了.
viewRedPoint.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);

// 点的间距 = 第1个点的左边 - 第0个点的左边;
int width = llPointGroup.getChildAt(1).getLeft()
- llPointGroup.getChildAt(0).getLeft();

System.out.println("间距: " + width);
}
});
  • ViewPager监听滑动事件, 动态设置红点位置
/**
* 页面滑动监听
*
* @params position 当前选中的位置
* @params positionOffset 偏移百分比
* @params positionOffsetPixels 页面偏移长度
*/
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
int leftMargin = (int) (mPointWidth * (positionOffset + position));
// Log.d(TAG, "当前位置:" + position + ";偏移比例:" + positionOffset
// + ";点偏移:" + leftMargin);

RelativeLayout.LayoutParams lp = (android.widget.RelativeLayout.LayoutParams) viewRedPoint
.getLayoutParams();
lp.leftMargin = leftMargin;
viewRedPoint.setLayoutParams(lp);
}
  • 处理"开始体验"按钮的显示和隐藏, 以及点击跳转页面