android 引导页

先上图再看代码,废话不多说。

package com.sxyrd.yrdapp.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Created by Fly0116 on 2016/4/16 0016.
 */
public class FViewPaper extends ViewGroup {

    private final Scroller mScroller;
    private VelocityTracker mVelocityTracker;
    private int mCurScreen;
    private final int mDefaultScreen = 0;
    private static final int TOUCH_STATE_REST = 0;
    private static final int TOUCH_STATE_SCROLLING = 1;
    private static final int SNAP_VELOCITY = 600;
    private int mTouchState = TOUCH_STATE_REST;
    private final int mTouchSlop;
    private float mLastMotionX;
    private float mLastMotionY;
    private OnViewChangeListener mOnViewChangeListener;

    /**
     * 设置是否可左右滑动
     */
    private boolean isScroll = true;

    public FViewPaper(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FViewPaper(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mScroller = new Scroller(context);
        mCurScreen = mDefaultScreen;
        mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    }

    /**
     * 对该控件里面的子控件定位
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childLeft = 0;
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View childView = getChildAt(i);
            if (childView.getVisibility() != View.GONE) {
                final int childWidth = childView.getMeasuredWidth();
                childView.layout(childLeft, 0, childLeft + childWidth,
                        childView.getMeasuredHeight());
                childLeft += childWidth;
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        // final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        // if (widthMode != MeasureSpec.EXACTLY) {
        // throw new IllegalStateException(
        // "ScrollLayout only canmCurScreen run at EXACTLY mode!");
        // }
        // final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        // if (heightMode != MeasureSpec.EXACTLY) {
        // throw new IllegalStateException(
        // "ScrollLayout only can run at EXACTLY mode!");
        // }

        // The children are given the same width and height as the scrollLayout
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
        }
        // Log.e(TAG, "moving to screen "+mCurScreen);
        scrollTo(mCurScreen * width, 0);
    }

    /**
     * According to the position of current layout scroll to the destination
     * page.
     */
    public void snapToDestination() {
        final int screenWidth = getWidth();
        final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;
        snapToScreen(destScreen);
    }

    public void snapToScreen(int whichScreen) {
        // 是否可滑动
        if (!isScroll) {
            setToScreen(whichScreen);
        } else {
            scrollToScreen(whichScreen);
        }
    }

    public void scrollToScreen(int whichScreen) {
        // get the valid layout page
        whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
        if (getScrollX() != (whichScreen * getWidth())) {
            final int delta = whichScreen * getWidth() - getScrollX();
            mScroller.startScroll(getScrollX(), 0, delta, 0,
                    Math.abs(delta) * 1);// 持续滚动时间 以毫秒为单位
            mCurScreen = whichScreen;
            invalidate(); // Redraw the layout

            if (mOnViewChangeListener != null) {
                mOnViewChangeListener.OnViewChange(mCurScreen);
            }
        }
    }

    public void setToScreen(int whichScreen) {
        whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
        mCurScreen = whichScreen;
        scrollTo(whichScreen * getWidth(), 0);

        if (mOnViewChangeListener != null) {
            mOnViewChangeListener.OnViewChange(mCurScreen);
        }
    }

    public int getCurScreen() {
        return mCurScreen;
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isScroll) {
            return false;
        }
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);
        final int action = event.getAction();
        final float x = event.getX();
        final float y = event.getY();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }
                mLastMotionX = x;
                mLastMotionY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = (int) (mLastMotionX - x);
                int deltaY = (int) (mLastMotionY - y);
                if (Math.abs(deltaX) < 200 && Math.abs(deltaY) > 10)
                    break;
                mLastMotionY = y;
                mLastMotionX = x;
                scrollBy(deltaX, 0);
                break;
            case MotionEvent.ACTION_UP:
                // if (mTouchState == TOUCH_STATE_SCROLLING) {
                final VelocityTracker velocityTracker = mVelocityTracker;
                velocityTracker.computeCurrentVelocity(1000);
                int velocityX = (int) velocityTracker.getXVelocity();
                if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
                    // Fling enough to move left
                    snapToScreen(mCurScreen - 1);
                } else if (velocityX < -SNAP_VELOCITY
                        && mCurScreen < getChildCount() - 1) {
                    // Fling enough to move right
                    snapToScreen(mCurScreen + 1);
                } else {
                    snapToDestination();
                }
                if (mVelocityTracker != null) {
                    mVelocityTracker.recycle();
                    mVelocityTracker = null;
                }
                mTouchState = TOUCH_STATE_REST;
                break;
            case MotionEvent.ACTION_CANCEL:
                mTouchState = TOUCH_STATE_REST;
                break;
        }
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (!isScroll) {
            return false;
        }
        final int action = ev.getAction();
        if ((action == MotionEvent.ACTION_MOVE)
                && (mTouchState != TOUCH_STATE_REST)) {
            return true;
        }
        final float x = ev.getX();
        final float y = ev.getY();
        switch (action) {
            case MotionEvent.ACTION_MOVE:
                final int xDiff = (int) Math.abs(mLastMotionX - x);
                if (xDiff > mTouchSlop) {
                    mTouchState = TOUCH_STATE_SCROLLING;
                }
                break;
            case MotionEvent.ACTION_DOWN:
                mLastMotionX = x;
                mLastMotionY = y;
                mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST
                        : TOUCH_STATE_SCROLLING;
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                mTouchState = TOUCH_STATE_REST;
                break;
        }
        return mTouchState != TOUCH_STATE_REST;

    }

    /**
     * 设置屏幕切换监听器
     *
     * @param listener
     */
    public void setOnViewChangeListener(OnViewChangeListener listener) {
        mOnViewChangeListener = listener;
    }

    /**
     * 屏幕切换监听器
     */
    public interface OnViewChangeListener {
        public void OnViewChange(int view);
    }

    /**
     * 设置控件是否可以左右滑动
     *
     * @param scroll
     */
    public void setCanScroll(boolean scroll) {
        this.isScroll = scroll;
    }
}

layout文件
activity_guide.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainRLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000">

    <com.sxyrd.yrdapp.widget.FViewPaper
        android:id="@+id/scrollLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="visible">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:background="@mipmap/guide1" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@mipmap/guide2" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@mipmap/guide3" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@mipmap/guide4" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@mipmap/guide1">

            <Button
                android:id="@+id/startBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="90.0dip"
                android:text="开启购物生活"
                android:textColor="#ffffff"
                android:textSize="18sp" />
        </RelativeLayout>
    </com.sxyrd.yrdapp.widget.FViewPaper>

    <LinearLayout
        android:id="@+id/pointLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30.0dip"
        android:visibility="visible">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="@drawable/selector_page_point"
            android:clickable="true"
            android:padding="5.0dip" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:background="@drawable/selector_page_point"
            android:clickable="true"
            android:padding="5.0dip" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:background="@drawable/selector_page_point"
            android:clickable="true"
            android:padding="5.0dip" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:background="@drawable/selector_page_point"
            android:clickable="true"
            android:padding="5.0dip" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:background="@drawable/selector_page_point"
            android:clickable="true"
            android:padding="5.0dip" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"
        android:background="@android:color/white"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/scan_app_tip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="2dp"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="请稍等,系统正在初始化中..."
            android:textSize="13sp" />
    </LinearLayout>
</RelativeLayout>



下面是activity文件

package com.sxyrd.yrdapp.ui;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sxyrd.yrdapp.R;
import com.sxyrd.yrdapp.base.BaseActivity;
import com.sxyrd.yrdapp.utils.Common;
import com.sxyrd.yrdapp.utils.Utils;
import com.sxyrd.yrdapp.widget.FViewPaper;
import com.wz.utils.PreferenceHelper;

import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Fly0116 on 2016/4/16 0016.
 */
public class GuideActivity extends BaseActivity implements FViewPaper.OnViewChangeListener {

    @InjectView(R.id.scrollLayout)
    FViewPaper scrollLayout;
    @InjectView(R.id.pointLayout)
    LinearLayout pointLayout;
    @InjectView(R.id.scan_app_tip)
    TextView scanAppName;

    private int count;
    private ImageView[] imgs;
    private int currentItem;
    private String scanToast;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_guide;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        count = scrollLayout.getChildCount();
        imgs = new ImageView[count];
        for (int i = 0; i < count; i++) {
            imgs[i] = (ImageView) pointLayout.getChildAt(i);
            imgs[i].setEnabled(true);
            imgs[i].setTag(i);
        }
        currentItem = 0;
        imgs[currentItem].setEnabled(false);
        scrollLayout.setOnViewChangeListener(this);
        writeLog();
    }

    @Override
    @OnClick({R.id.startBtn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.startBtn:
                Utils.jumpActivity(this, MainTabActivity.class);
                finish();
                break;
        }
    }

    @Override
    public void OnViewChange(int postion) {
        if (postion < 0 || postion > count - 1 || currentItem == postion) {
            return;
        }
        imgs[currentItem].setEnabled(true);
        imgs[postion].setEnabled(false);
        currentItem = postion;
        switch (postion) {
            case 0:
                scanToast = "美食";
                break;
            case 1:
                scanToast = "娱乐";
                break;
            case 2:
                scanToast = "本地";
                break;
            case 3:
                scanToast = "生活";
                break;
            case 4:
                scanToast = "尽在友人岛";
                break;

        }
        scanAppName.setText(scanToast);
        /*if (postion == count - 2) {
            scanAppName.setText(scanToast);
        }*/
    }

    private void writeLog() {
        PreferenceHelper.write(this, Common.FIRSTINSTALL_FILE, Common.FIRSTINSTALL_KEY, false);
    }
}
代码中用到了butterknife注解的方式,大家可以按照自己的需求改动,如果是android studio

直接使用

compile 'com.jakewharton:butterknife:6.1.0'
还有一个baseActivity;

大家也可以根据自己的写的基类来继承,或者也可以改成Acitivity,只需要简单改动代码可以。

下面贴上自己的BaseActivity代码,仅供参考

package com.sxyrd.yrdapp.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.sxyrd.yrdapp.R;
import com.sxyrd.yrdapp.inter.BaseViewInterface;
import com.sxyrd.yrdapp.inter.DialogControl;
import com.sxyrd.yrdapp.utils.AppManager;
import com.sxyrd.yrdapp.utils.DialogHelp;

import butterknife.ButterKnife;

/**
 * Created by Fly0116 on 2016/4/8 0008.
 */
public abstract class BaseActivity extends Activity implements View.OnClickListener,DialogControl,BaseViewInterface{


    private boolean _isVisible;
    private ProgressDialog _waitDialog;
    protected LayoutInflater mInflater;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }
        mInflater = getLayoutInflater();

        // 通过注解绑定控件
        ButterKnife.inject(this);

        init(savedInstanceState);
        initView();
        initData();
        _isVisible = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    protected int getLayoutId() {
        return 0;
    }

    protected void init(Bundle savedInstanceState){}
    @Override
    public ProgressDialog showWaitDialog() {
        return showWaitDialog(R.string.loading);
    }

    @Override
    public ProgressDialog showWaitDialog(int resid) {
        return showWaitDialog(getString(resid));
    }

    @Override
    public ProgressDialog showWaitDialog(String message) {
        if (_isVisible) {
            if (_waitDialog == null) {
                _waitDialog = DialogHelp.getWaitDialog(this, message);
            }
            if (_waitDialog != null) {
                _waitDialog.setMessage(message);
                _waitDialog.show();
            }
            return _waitDialog;
        }
        return null;
    }

    @Override
    public void hideWaitDialog() {
        if (_isVisible && _waitDialog != null) {
            try {
                _waitDialog.dismiss();
                _waitDialog = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }


}
以上代码就写到这里,如果有任何问题可以留言给我,相互学习,技术无边界。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值