APP首次打开的欢迎界面

Welcome.java

//此功能曾由于只提供一套图片,没有区分xhml而在部分机型上发生过内存溢出问题.
public class Welcome extends SFBaseActivity {

    private ArrayList<View> pageViews;
    private IndicatorViewPager indicatorViewPager;
    private LayoutInflater inflater;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置无标题窗口
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.welcome);
        inflater = LayoutInflater.from(this);

        ViewPager viewPager = (ViewPager) findViewById(R.id.guidePages);
        Indicator guide_indicator = (Indicator) findViewById(R.id.guide_indicator);
        indicatorViewPager = new IndicatorViewPager(guide_indicator, viewPager);

        pageViews = new ArrayList<>();
        View v1 = inflater.inflate(R.layout.welcome_1, null, false);
        View v2 = inflater.inflate(R.layout.welcome_2, null, false);
        View v3 = inflater.inflate(R.layout.welcome_3, null, false);
        View v4 = inflater.inflate(R.layout.welcome_4, null, false);
        pageViews.add(v1);
        pageViews.add(v2);
        pageViews.add(v3);
        pageViews.add(v4);

        indicatorViewPager.setAdapter(new MyAdapter());

        Button btn_start  = (Button) v4.findViewById(R.id.btn_start);
        btn_start.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                goHome();
            }
        });

        //过渡页
        v4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                goHome();
            }
        });
    }

    private void goHome() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        Welcome.this.finish();
    }

    private class MyAdapter extends IndicatorViewPager.IndicatorViewPagerAdapter {

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.tab_guide, container, false);
            }
            return convertView;
        }

        @Override
        public View getViewForPage(int position, View convertView, ViewGroup container) {
            return pageViews.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            //这是ViewPager适配器的特点,有两个值 POSITION_NONE,POSITION_UNCHANGED,默认就是POSITION_UNCHANGED,
            // 表示数据没变化不用更新.notifyDataChange的时候重新调用getViewForPage
            return PagerAdapter.POSITION_NONE;
        }

        @Override
        public int getCount() {
            return pageViews.size();
        }
    }
}

welcome.xml(activity对应的界面):

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/guidePages"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom|center_horizontal">
        <com.sf.sdk.viewpagerindicator.view.indicator.FixedIndicatorView
            android:id="@+id/guide_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="78dp"/>


    </RelativeLayout>


</merge>

每次切屏的界面:welcome_1.xml(其他welcome_2.xml, welcome_3.xml, welcome_4.xml类似,都是一个layout里面设置一个背景,当然welcome_4.xml除外,welcome_4.xml有一个按钮,点击之后进入APP主界面):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@mipmap/welcome_1">
</LinearLayout>

welcome_4.xml:

<?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:background="@mipmap/welcome_4"
    android:orientation="vertical">
    <!--<View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"/>-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3"
        android:gravity="bottom">
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/btn_start"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="48dp"
            android:layout_marginBottom="10dp"
            android:text="开始体验"
            android:textColor="@color/red"
            android:textSize="18sp"
            android:background="@drawable/self_btn_vip"/>

        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    </LinearLayout>


</RelativeLayout>

指示器的UI:tab_guide.xml

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="8dp"
    android:layout_height="8dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:background="@drawable/tab_selector"
    android:orientation="vertical" />

指示器的两种状态:
tab_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!--<item android:drawable="@mipmap/tab_2" android:state_selected="true"></item>
    <item android:drawable="@mipmap/tab_1"></item>-->
    <item android:state_selected="true">
        <shape android:shape="oval">
            <solid android:color="@color/colorPrimaryDarkReal"></solid>
        </shape>
    </item>
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/gray_line"></solid>
        </shape>
    </item>

</selector>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ithouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值