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
- Android使用ViewPager实现带指示点的页面导航效果
- Android实践-使用ViewPager和Fragment实现带Tab导航条的仿新闻类页面
- Android 利用ViewPager实现底部圆点导航左右滑动效果以及Fragment页面切换
- 带导航点的ViewPager
- android的viewpager 指示文字过渡效果
- 实现ViewPager新手引导页的指示点跟随移动的效果
- ViewPager(一):带指示图标的滑动页面的实现
- 属性动画实现viewpager改变页面的时候,带有指示条的动画效果
- 安卓:ViewPager及带指示圆点的
- ViewPager的详解(使用viewPager实现导航页面)
- 使用Viewpager实现带指示器的导航页
- Tablayout和Viewpager的结合使用,实现导航的效果
- Android 使用ViewGroup实现ViewPager的效果
- ViewPager实现页面导航(使用pageradapter)
- ViewPager实现页面导航(使用FragmentPagerAdapter)
- Android中使用ViewPager和PagerTabStrip实现页面滑动效果
- Android中使用ViewPager和PagerTabStrip实现页面滑动效果
- 一个案例教你简单地玩转ViewPager(三)之带指示点的图片自动轮播效果
- 可视化Google Inception V3模型的网络结构
- Sql server安装,提示is not a valid login
- BZOJ 3339 线段树
- JSTL_1
- Singletons
- Android使用ViewPager实现带指示点的页面导航效果
- jface WizardPage切换事件
- C++注释
- mysql中的utf8mb4,可以支持Emoji表情的编码格式
- 通知栏,状态栏的隐藏与各种细节(坑)
- linux c之出现warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]这个问题
- csan博客 I am coming
- storm 集成kafka时遇见的问题
- Android Studio如何把自己的工程变成第三方库,如何导入第三方库和删除第三方库