国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android自定義圓點指示器

瀏覽:2日期:2022-09-18 09:08:48

本文實例為大家分享了Android自定義圓點指示器的具體代碼,供大家參考,具體內容如下

先上效果圖

Android自定義圓點指示器

大概思路就是自定義View 從左至右繪制圓點 然后在ViewPager的OnPageChangeListener中設置當前頁面的圓點

下面是代碼

先定義屬性

<resources> <attr name='selectedColor' format='color'/> <attr name='unselectedColor' format='color'/> <declare-styleable name='Indicator'><attr name='selectedColor'/><attr name='unselectedColor'/> </declare-styleable></resources>

接下來是自定義的View

public class Indicator extends View{ private static final int DEFAULT_TOTAL_INDEX = 5; private static final int DEFAULT_CURRENT_INDEX = 0; private static final int DEFAULT_CIRCLE_DISTANCE = 40; private static final int DEFAULT_CIRCLE_RADIUS = 8; private static final int DEFAULT_CIRCLE_SELECTED_RADIUS = 11; private int selectedColor; private int unselectedColor; private int currentIndex; private int totalIndex; private Paint paint; private int startX; private int startSelectedY; private int startY; private int centreX; public Indicator(Context context) {this(context,null); } public Indicator(Context context, AttributeSet attrs) {this(context, attrs,0); } public Indicator(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,R.styleable.Indicator,defStyleAttr,0);selectedColor = typedArray.getColor(R.styleable.Indicator_selectedColor, Color.LTGRAY);unselectedColor = typedArray.getColor(R.styleable.Indicator_unselectedColor,Color.WHITE);typedArray.recycle();totalIndex = DEFAULT_TOTAL_INDEX;currentIndex = DEFAULT_CURRENT_INDEX;paint = new Paint(); }

從TypedArray中獲取自定義的屬性,totalIndex是總的圓點個數,currentIndex是當前頁面的圓點接下來是重寫的OnDraw()方法

@Override protected void onDraw(Canvas canvas) {super.onDraw(canvas);centreX = getWidth() / 2;startSelectedY = getHeight() / 2 - DEFAULT_CIRCLE_SELECTED_RADIUS;startY = getHeight() / 2 - DEFAULT_CIRCLE_RADIUS;if (totalIndex % 2 == 0){ startX = centreX - (int)(1.0 * (totalIndex - 1)/2 * DEFAULT_CIRCLE_DISTANCE);}else{ startX = centreX - totalIndex / 2 * DEFAULT_CIRCLE_DISTANCE;}paint.setAntiAlias(true);paint.setColor(unselectedColor);int tempX = startX;for(int i = 0 ; i < totalIndex ; i++ ){ RectF rectF = new RectF(tempX - DEFAULT_CIRCLE_RADIUS,startY, tempX + DEFAULT_CIRCLE_RADIUS,startY + 2 * DEFAULT_CIRCLE_RADIUS); if (i == currentIndex) {paint.setColor(selectedColor);rectF = new RectF(tempX - DEFAULT_CIRCLE_SELECTED_RADIUS,startSelectedY,tempX + DEFAULT_CIRCLE_SELECTED_RADIUS,startSelectedY + 2 * DEFAULT_CIRCLE_SELECTED_RADIUS); } canvas.drawOval(rectF,paint); if (paint.getColor() == selectedColor)paint.setColor(unselectedColor); tempX += DEFAULT_CIRCLE_DISTANCE;} }

因為當前頁面的圓點和未選中頁面的圓點要設置不同的大小 所以分別設置每個圓點的坐標 然后用for循環繪制圓點這里有一點要注意 new RectF() 的四個參數分別是圓點外面的矩形的左上角的X,Y和右下角的X,Y

接下來是設置當前頁面的圓點的方法

public void setCurrentIndex(int currentIndex){//if (currentIndex < 0)// currentIndex += totalIndex ;//if (currentIndex > totalIndex - 1)// currentIndex %= totalIndex;this.currentIndex = currentIndex;invalidate(); }

注釋里的代碼是當頁面可以循環的時候設置的接下來是設置總的圓點個數的方法

public void setTotalIndex(int totalIndex){int oldTotalIndex = this.totalIndex;if (totalIndex < 1) return;if (totalIndex < oldTotalIndex){ if (currentIndex == totalIndex )currentIndex = totalIndex - 1;}this.totalIndex = totalIndex;invalidate(); }

當刪除圓點的時候 如果currentIndex是最后一個 讓currentIndex向前移動接下來是重寫的OnMeasure()方法

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec)); } private int measureHeight(int measureSpec){int result;int specMode = MeasureSpec.getMode(measureSpec);int specSize = MeasureSpec.getSize(measureSpec);int desired = DEFAULT_CIRCLE_SELECTED_RADIUS * 2 + getPaddingBottom() + getPaddingTop();if(specMode == MeasureSpec.EXACTLY) { result = Math.max(desired,specSize);}else{ if(specMode == MeasureSpec.AT_MOST){result = Math.min(desired,specSize); } else result = desired;}return result; } private int measureWidth(int measureSpec){int result;int specMode = MeasureSpec.getMode(measureSpec);int specSize = MeasureSpec.getSize(measureSpec);int desired = (totalIndex - 1) * DEFAULT_CIRCLE_DISTANCE + DEFAULT_CIRCLE_SELECTED_RADIUS * 2 + getPaddingLeft() + getPaddingRight();if(specMode == MeasureSpec.EXACTLY) { result = Math.max(desired,specSize);}else{ if(specMode == MeasureSpec.AT_MOST){result = Math.min(desired,specSize); }else result = desired;}return result; }

下面是MainActivity的布局代碼,很簡單

<RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' xmlns:app='http://schemas.android.com/apk/res-auto' android:layout_width='match_parent' android:layout_height='match_parent' tools:context='com.example.lzh123.learnviewpager.MainActivity'> <android.support.v4.view.ViewPagerandroid: android:layout_width='match_parent'android:layout_height='match_parent'/> <com.example.lzh123.learnviewpager.Indicatorapp:selectedColor='#FFFFFF'app:unselectedColor='#C7C7C7'android: android:layout_centerInParent='true'android:layout_width='wrap_content'android:layout_height='wrap_content' /> </RelativeLayout>

下面是MainActivity的代碼

public class MainActivity extends AppCompatActivity { View layout1,layout2,layout3; ViewPager viewPager; Indicator indicator; List<View> viewList; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.viewPager);LayoutInflater inflater = getLayoutInflater();layout1 = inflater.inflate(R.layout.layout1,null);layout2 = inflater.inflate(R.layout.layout2,null);layout3 = inflater.inflate(R.layout.layout3,null); viewList = new ArrayList<>();viewList.add(layout1);viewList.add(layout2);viewList.add(layout3); indicator = (Indicator) findViewById(R.id.indicator);indicator.setTotalIndex(viewList.size());PagerAdapter pagerAdapter = new PagerAdapter() { @Override public int getCount() {return viewList.size(); } @Override public void destroyItem(ViewGroup container, int position, Object object) {container.removeView(viewList.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) {container.addView(viewList.get(position)); return position; } @Override public boolean isViewFromObject(View view, Object object) {return view == viewList.get(Integer.parseInt(object.toString())); }}; viewPager.setAdapter(pagerAdapter);viewPager.setOnPageChangeListener(new PageChangeListener()); } public class PageChangeListener implements ViewPager.OnPageChangeListener{ @Overridepublic void onPageSelected(int position) { indicator.setCurrentIndex(position);} @Overridepublic void onPageScrollStateChanged(int state) { } @Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } }

ViewPager里添加了三個空頁面 然后設置指示器的圓點個數,最后在ViewPager的OnPageChangeListener中設置當前的 頁面的圓點。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 亚洲精选在线 | 国产性大片黄在线观看在线放 | 女人夜色黄网在线观看 | 国产一区二区免费视频 | 99久久精品6在线播放 | 日本韩国一区二区三区 | 91av福利 | 99视频在线精品免费 | 日韩欧美精品在线观看 | 大美女香蕉丽人视频网站 | 亚洲精品一级片 | 国产三级精品在线 | 亚洲一级毛片中文字幕 | 农村寡妇一级毛片免费看视频 | 日韩黄色在线 | 免费高清特黄a 大片 | 高清一级片 | 一本大道香蕉大vr在线吗视频 | 国产精品亚洲欧美云霸高清 | 国产欧美日韩综合一区二区三区 | 国产高清精品一级毛片 | 中文一区二区在线观看 | 久久一区二区三区99 | 国产成人做受免费视频 | 一区二区三区日韩精品 | 亚洲天堂中文网 | 99国内精品久久久久久久 | 日韩国产欧美精品综合二区 | 性欧洲精品videos' | 亚洲综合成人网 | 久草资源在线 | 亚洲天堂网在线观看 | 欧美巨大另类极品videohd | 亚洲一区二区三区不卡在线播放 | 12至16末成年毛片视频 | 国产三级精品在线观看 | 久久视频在线免费观看 | 国产一区亚洲 | 可以免费看黄的网站 | 亚洲m男在线中文字幕 | 国产盗摄视频 |