Android使用ViewPager实现带指示点的页面导航效果

来源:互联网 发布:plsql备份oracle数据库 编辑:程序博客网 时间:2024/05/22 01:34

老样子,先上效果图:


参考博客:Android App 第一次打开时的引导界面

先上布局文件:其中activity_main.xml文件并没有什么作用。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.martsforever.owa.indicaterexample.MainActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="主界面" /></RelativeLayout>
activity_launch.xml:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_launch"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.martsforever.owa.indicaterexample.LaunchActivity">    <android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <com.martsforever.owa.indicaterexample.IndicatorView        android:id="@+id/indicatorView"        android:layout_width="match_parent"        android:layout_height="45dp"        android:layout_gravity="bottom|center_horizontal"        android:layout_marginBottom="30dp" /></FrameLayout>
view1.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:background="#f00"    android:layout_height="match_parent"></LinearLayout>
view2.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:background="#0f0"    android:layout_height="match_parent"></LinearLayout>
view3.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:background="#00f"    android:layout_height="match_parent">    <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_gravity="center_vertical"        android:text="start"        android:layout_marginBottom="120dp" /></RelativeLayout>
IndicatorView.java:

package com.martsforever.owa.indicaterexample;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;/** * Created by StormLion on 2016/5/6 16:57. */public class IndicatorView extends View{    private static final String LOG_TAG = IndicatorView.class.getName();//    当前标签页    private int currentIndex = 0;//    标签页的个数,初始化为0,当LaunchActivity初始化的时候设置标签页的个数    private int count = 0;//    indicater之间的距离    private int distance = 30; // 圆点之间的距离(圆心距)//    以View左上角为原点,开始绘制indicater的横轴距离    private int x;//    以View左上角为原点,开始绘制indicater的纵轴距离    private int y;//    indicater被选中时的颜色    private int selectedColor = 0xffffffff;//    indicater未被选中时的颜色    private int unselectedColor = 0xff000000;    private Paint paint;    public IndicatorView(Context context) {        super(context, null);    }    public IndicatorView(Context context, AttributeSet attrs){        super(context, attrs);        paint = new Paint(Paint.ANTI_ALIAS_FLAG);        paint.setStyle(Paint.Style.FILL);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){        int viewWidth = MeasureSpec.getSize(widthMeasureSpec);        int viewHeight = MeasureSpec.getSize(heightMeasureSpec);        int drawWidth = (count + 1) * distance;//        水平居中        x = (viewWidth - drawWidth) / 2;//        垂直居中        y = viewHeight / 2;        setMeasuredDimension(viewWidth, viewHeight) ;    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        float currentX = x;        for(int i = 0; i < count; i++) {            currentX += distance;            if(i == currentIndex) {                paint.setColor(selectedColor);            } else {                paint.setColor(unselectedColor);            }            canvas.drawCircle(currentX, y, 10, paint);        }    }    public void setIndicatorCount(int count) {        this.count = count;    }    public void setCurIndicatorIndex(int index) {        currentIndex = index;        invalidate();    }}
LaunchActivity.java:

package com.martsforever.owa.indicaterexample;import android.content.Intent;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import java.util.ArrayList;public class LaunchActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {    private ViewPager viewPager;    private ArrayList<View> viwePagerItems;    private Button btnStart;    private IndicatorView indicatorView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_launch);        initView();        initData();    }    private void initView() {        LayoutInflater inflater = LayoutInflater.from(this);        View view1 = inflater.inflate(R.layout.view1, null);        View view2 = inflater.inflate(R.layout.view2, null);        View view3 = inflater.inflate(R.layout.view3, null);        btnStart = (Button) view3.findViewById(R.id.startBtn);        viewPager = (ViewPager) findViewById(R.id.viewpager);        viwePagerItems = new ArrayList<View>();        viwePagerItems.add(view1);        viwePagerItems.add(view2);        viwePagerItems.add(view3);        indicatorView = (IndicatorView)findViewById(R.id.indicatorView);        indicatorView.setIndicatorCount(viwePagerItems.size());    }    private void initData() {        ViewPagerAdapter vpAdapter = new ViewPagerAdapter(viwePagerItems);        viewPager.setOnPageChangeListener(this);        viewPager.setAdapter(vpAdapter);        btnStart.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                startbutton();            }        });    }    @Override    public void onPageScrollStateChanged(int arg0) {    }    @Override    public void onPageScrolled(int arg0, float arg1, int arg2) {    }    @Override    public void onPageSelected(int arg0) {        indicatorView.setCurIndicatorIndex(arg0);    }    private void startbutton() {        Intent intent = new Intent();        intent.setClass(LaunchActivity.this,MainActivity.class);        startActivity(intent);        this.finish();    }    class ViewPagerAdapter extends PagerAdapter {        private ArrayList<View> mViewList;        public ViewPagerAdapter (ArrayList<View> views){            this.mViewList = views;        }        @Override        public int getCount() {            if (mViewList != null) {                return mViewList.size();            }            return 0;        }        @Override        public Object instantiateItem(View view, int position) {            ((ViewPager) view).addView(mViewList.get(position), 0);            return mViewList.get(position);        }        @Override        public boolean isViewFromObject(View view, Object arg1) {            return (view == arg1);        }        @Override        public void destroyItem(View view, int position, Object arg2) {            ((ViewPager) view).removeView(mViewList.get(position));        }    }}

MainActivity.java:

package com.martsforever.owa.indicaterexample;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}






0 0
原创粉丝点击