溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android怎么實(shí)現(xiàn)引導(dǎo)頁(yè)的圓點(diǎn)指示器

發(fā)布時(shí)間:2021-06-16 17:07:05 來(lái)源:億速云 閱讀:328 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)Android怎么實(shí)現(xiàn)引導(dǎo)頁(yè)的圓點(diǎn)指示器,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

在App引導(dǎo)界面通常有引導(dǎo)界面提示小圓點(diǎn),我們使用一個(gè)集成的類 來(lái)整體封裝實(shí)現(xiàn):

Android怎么實(shí)現(xiàn)引導(dǎo)頁(yè)的圓點(diǎn)指示器

使用的方法:

首先在 XML布局中引入這個(gè)自定義的控件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.chen.weibo.GuideAty">
 
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager">
 
    </android.support.v4.view.ViewPager>
 
    <com.chen.weibo.PageNumberPoint    
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:id="@+id/myPoint"
        android:layout_marginBottom="20dp"
        >
    </com.chen.weibo.PageNumberPoint>
 
 
</FrameLayout>

然后在Activity中綁定ViewPager對(duì)象

point.addViewPager(viewPager);

下面就是主的UI代碼 引入工程即可使用:

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
 
import java.util.ArrayList;
 
/**
 * Class: ViewPager 頁(yè)碼圓點(diǎn)指示器UI組件.
 * author: ChenDeng.
 * Date: 2017-11-8.
 * explain:直接在XML文件中調(diào)用,需要在Activity中綁定 ViewPager 對(duì)象,使用addViewPager()方法.
 */
public class PageNumberPoint extends LinearLayout {
    private Context context;
    private PagerAdapter adapter;
    private int countPoint = 0;
    private ArrayList<Circle> point;
    private ObjectAnimator scaleX;
    private ObjectAnimator scaleY;
 
    public PageNumberPoint(Context context) {
        super(context);
        this.context = context;
        initView();
    }
 
    public PageNumberPoint(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }
 
    public PageNumberPoint(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
 
 
    private void initView() {
        this.setOrientation(LinearLayout.HORIZONTAL);
        this.setGravity(Gravity.CENTER);
        this.setClickable(false);
    }
 
    /**
     * 綁定頁(yè)碼
     *
     * @param pager
     */
    public void addViewPager(ViewPager pager) {
        this.adapter = pager.getAdapter();
        addPagerPoint();
        //設(shè)置監(jiān)聽(tīng)器
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
 
            @Override
            public void onPageSelected(int position) {
                setSelectPoint(position);
            }
 
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
 
    private void addPagerPoint() {
        countPoint = adapter.getCount();
        point = new ArrayList<>();
 
        for (int i = 0; i < countPoint; i++) {
            Circle circle = new Circle(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
            params.setMargins(20, 0, 0, 0); //設(shè)置外邊距
            circle.setLayoutParams(params);
            this.addView(circle);
            point.add(circle);
        }
 
        point.get(0).setChecked(true);  //默認(rèn)第一個(gè)是選中的
    }
 
    /**
     * 縮放動(dòng)畫(huà)效果
     * @param view
     */
    private void playAnimator(View view){
        scaleX = ObjectAnimator.ofFloat(view,"scaleX",0.0f,1.0f);
        scaleY = ObjectAnimator.ofFloat(view,"scaleY",0.0f,1.0f);
        //通過(guò)動(dòng)畫(huà)集合組合動(dòng)畫(huà)
        AnimatorSet animatorSet =new AnimatorSet();
        //這兩個(gè)動(dòng)畫(huà)同時(shí)執(zhí)行 綁定起來(lái)
        animatorSet.play(scaleX).with(scaleY);
        animatorSet.setDuration(300);
        animatorSet.start();
    }
 
    private void setSelectPoint(int position) {
        point.get(position).setChecked(true);
        //開(kāi)啟動(dòng)畫(huà)
        playAnimator(point.get(position));  
        for (int i = 0; i < point.size(); i++) {
            if (i == position)
                continue;
            point.get(i).setChecked(false);
        }
    }
 
    /***************************自定義的小圓點(diǎn)UI組件******************************************/
    public class Circle extends View {
        private float circleRadius = 6.8f;   //默認(rèn)的圓的半徑
        private boolean checked = false;
 
        public Circle(Context context) {
            super(context);
            initViewSize();
        }
 
        public Circle(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            initViewSize();
        }
 
        public Circle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initViewSize();
        }
 
        private void initViewSize() {
            //推薦使用 寬高 各50dp
            this.setLayoutParams(new ViewGroup.LayoutParams(50, 50));
            this.setClickable(false);
        }
 
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            int verticalCenter = getHeight() / 2;
            int horizontalCenter = getWidth() / 2;
            Paint paint = new Paint();
            paint.setAntiAlias(true);  //防鋸齒
            paint.setDither(true);   //防抖動(dòng)
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
 
            if (checked) {  //如果是選中狀態(tài)
                //畫(huà)圓心填充物
                paint.setColor(Color.WHITE);
                float radius = circleRadius + circleRadius / 2;
                canvas.drawCircle(verticalCenter - (circleRadius / 2), horizontalCenter - (circleRadius / 2), radius, paint);
            } else {
                paint.setColor(Color.rgb(146, 146, 146));
                canvas.drawCircle(verticalCenter, horizontalCenter, circleRadius, paint);
            }
        }
 
        /**
         * 設(shè)置圓的半徑
         *
         * @param radius
         */
        public void setCircleRadius(float radius) {
            this.circleRadius = radius;
            invalidate();//重新繪制組件
        }
 
        /**
         * 設(shè)置選擇 還是非選擇
         *
         * @param checked
         */
        public void setChecked(boolean checked) {
            this.checked = checked;
            invalidate();
        }
    }
 
}

關(guān)于“Android怎么實(shí)現(xiàn)引導(dǎo)頁(yè)的圓點(diǎn)指示器”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI