最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)小米相機(jī)底部滑動(dòng)指示器

 更新時(shí)間:2021年04月14日 17:35:38   作者:奮斗的小鷹  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)小米相機(jī)底部滑動(dòng)指示器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

近期工作內(nèi)容需要涉及到相機(jī)開發(fā),其中一個(gè)功能點(diǎn)就是實(shí)現(xiàn)一個(gè)相機(jī)預(yù)覽頁底部的滑動(dòng)指示器,現(xiàn)在整理出來供大家討論參考。

先上一張圖看下效果:

主要實(shí)現(xiàn)功能有:

1.支持左右滑動(dòng),每次滑動(dòng)一個(gè)tab

2.支持tab點(diǎn)擊,直接跳到對(duì)應(yīng)tab

3.選中的tab一直處于居中位置

4.支持部分UI自定義(大家可根據(jù)需要自己改動(dòng))

5.tab點(diǎn)擊回調(diào)

6.內(nèi)置Tab接口,放入的內(nèi)容需要實(shí)現(xiàn)Tab接口

7.設(shè)置預(yù)選中tab

public class CameraIndicator extends LinearLayout {
    // 當(dāng)前選中的位置索引
    private int currentIndex;
    //tabs集合
    private Tab[] tabs;
 
    // 利用Scroller類實(shí)現(xiàn)最終的滑動(dòng)效果
    public Scroller mScroller;
    //滑動(dòng)執(zhí)行時(shí)間(ms)
    private int mDuration = 300;
    //選中text的顏色
    private int selectedTextColor = 0xffffffff;
    //未選中的text的顏色
    private int normalTextColor = 0xffffffff;
    //選中的text的背景
    private Drawable selectedTextBackgroundDrawable;
    private int selectedTextBackgroundColor;
    private int selectedTextBackgroundResources;
    //是否正在滑動(dòng)
    private boolean isScrolling = false;
 
    private int onLayoutCount = 0;
 
 
    public CameraIndicator(Context context) {
        this(context, null);
    }
 
    public CameraIndicator(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public CameraIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mScroller = new Scroller(context);
 
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //測量所有子元素
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        //處理wrap_content的情況
        int width = 0;
        int height = 0;
        if (getChildCount() == 0) {
            setMeasuredDimension(0, 0);
        } else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                width +=  child.getMeasuredWidth();
                height = Math.max(height, child.getMeasuredHeight());
            }
            setMeasuredDimension(width, height);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                width +=  child.getMeasuredWidth();
            }
            setMeasuredDimension(width, heightSize);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                height = Math.max(height, child.getMeasuredHeight());
            }
            setMeasuredDimension(widthSize, height);
        } else {
            //如果自定義ViewGroup之初就已確認(rèn)該ViewGroup寬高都是match_parent,那么直接設(shè)置即可
            setMeasuredDimension(widthSize, heightSize);
        }
    }
 
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //給選中text的添加背景會(huì)多次進(jìn)入onLayout,會(huì)導(dǎo)致位置有問題,暫未解決
        if (onLayoutCount > 0) {
            return;
        }
        onLayoutCount++;
 
        int counts = getChildCount();
        int childLeft = 0;
        int childRight = 0;
        int childTop = 0;
        int childBottom = 0;
        //居中顯示
        int widthOffset = 0;
 
 
        //計(jì)算最左邊的子view距離中心的距離
        for (int i = 0; i < currentIndex; i++) {
            View childView = getChildAt(i);
            widthOffset += childView.getMeasuredWidth() + getMargins(childView).get(0)+getMargins(childView).get(2);
        }
 
        //計(jì)算出每個(gè)子view的位置
        for (int i = 0; i < counts; i++) {
            View childView = getChildAt(i);
            childView.setOnClickListener(v -> moveTo(v));
            if (i != 0) {
                View preView = getChildAt(i - 1);
                childLeft = preView.getRight() +getMargins(preView).get(2)+ getMargins(childView).get(0);
            } else {
                childLeft = (getWidth() - getChildAt(currentIndex).getMeasuredWidth()) / 2 - widthOffset;
            }
            childRight = childLeft + childView.getMeasuredWidth();
            childTop = (getHeight() - childView.getMeasuredHeight()) / 2;
            childBottom = (getHeight() + childView.getMeasuredHeight()) / 2;
            childView.layout(childLeft, childTop, childRight, childBottom);
        }
 
        TextView indexText = (TextView) getChildAt(currentIndex);
        changeSelectedUIState(indexText);
 
    }
 
    private List<Integer> getMargins(View view) {
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        List<Integer> listMargin = new ArrayList<Integer>();
        listMargin.add(params.leftMargin);
        listMargin.add(params.topMargin);
        listMargin.add(params.rightMargin);
        listMargin.add(params.bottomMargin);
        return listMargin;
    }
 
    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            // 滑動(dòng)未結(jié)束,內(nèi)部使用scrollTo方法完成實(shí)際滑動(dòng)
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        } else {
            //滑動(dòng)完成
            isScrolling = false;
            if (listener != null) {
                listener.onChange(currentIndex,tabs[currentIndex]);
            }
        }
        super.computeScroll();
    }
 
 
    /**
     * 改變選中TextView的顏色
     *
     * @param currentIndex 滑動(dòng)之前選中的那個(gè)
     * @param nextIndex    滑動(dòng)之后選中的那個(gè)
     */
    public final void scrollToNext(int currentIndex, int nextIndex) {
        TextView selectedText = (TextView) getChildAt(currentIndex);
        if (selectedText != null) {
            selectedText.setTextColor(normalTextColor);
            selectedText.setBackground(null);
        }
        selectedText = (TextView) getChildAt(nextIndex);
        if (selectedText != null) {
            changeSelectedUIState(selectedText);
        }
    }
 
    private void changeSelectedUIState(TextView view) {
        view.setTextColor(selectedTextColor);
        if (selectedTextBackgroundDrawable != null) {
            view.setBackground(selectedTextBackgroundDrawable);
        }
 
        if (selectedTextBackgroundColor != 0) {
            view.setBackgroundColor(selectedTextBackgroundColor);
        }
        if (selectedTextBackgroundResources != 0) {
            view.setBackgroundResource(selectedTextBackgroundResources);
        }
    }
 
 
    /**
     * 向右滑一個(gè)
     */
    public void moveToRight() {
        moveTo(getChildAt(currentIndex - 1));
    }
 
 
    /**
     * 向左滑一個(gè)
     */
    public void moveToLeft() {
        moveTo(getChildAt(currentIndex + 1));
    }
 
    /**
     * 滑到目標(biāo)view
     *
     * @param view 目標(biāo)view
     */
    private void moveTo(View view) {
        for (int i = 0; i < getChildCount(); i++) {
            if (view == getChildAt(i)) {
                if (i == currentIndex) {
                    //不移動(dòng)
                    break;
                } else if (i < currentIndex) {
                    //向右移
                    if (isScrolling) {
                        return;
                    }
                    isScrolling = true;
                    int dx = getChildAt(currentIndex).getLeft() - view.getLeft() + (getChildAt(currentIndex).getMeasuredWidth() - view.getMeasuredWidth()) / 2;
                    //這里使用scroll會(huì)使滑動(dòng)更平滑不卡頓,scroll會(huì)根據(jù)起點(diǎn)、終點(diǎn)及時(shí)間計(jì)算出每次滑動(dòng)的距離,其內(nèi)部有一個(gè)插值器
                    mScroller.startScroll(getScrollX(), 0, -dx, 0, mDuration);
                    scrollToNext(currentIndex, i);
                    setCurrentIndex(i);
                    invalidate();
                } else if (i > currentIndex) {
                    //向左移
                    if (isScrolling) {
                        return;
                    }
                    isScrolling = true;
                    int dx = view.getLeft() - getChildAt(currentIndex).getLeft() + (view.getMeasuredWidth() - getChildAt(currentIndex).getMeasuredWidth()) / 2;
                    mScroller.startScroll(getScrollX(), 0, dx, 0, mDuration);
                    scrollToNext(currentIndex, i);
                    setCurrentIndex(i);
                    invalidate();
                }
            }
        }
    }
 
 
    /**
     * 設(shè)置tabs
     *
     * @param tabs
     */
    public void setTabs(Tab... tabs) {
        this.tabs = tabs;
        //暫時(shí)不通過layout布局添加textview
        if (getChildCount()>0){
            removeAllViews();
        }
        for (Tab tab : tabs) {
            TextView textView = new TextView(getContext());
            textView.setText(tab.getText());
            textView.setTextSize(14);
            textView.setTextColor(selectedTextColor);
            textView.setPadding(dp2px(getContext(),5), dp2px(getContext(),2), dp2px(getContext(),5),dp2px(getContext(),2));
            LayoutParams layoutParams= new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            layoutParams.rightMargin=dp2px(getContext(),2.5f);
            layoutParams.leftMargin=dp2px(getContext(),2.5f);
            textView.setLayoutParams(layoutParams);
            addView(textView);
        }
    }
 
 
    public int getCurrentIndex() {
        return currentIndex;
    }
 
    //設(shè)置默認(rèn)選中第幾個(gè)
    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
    }
 
    //設(shè)置滑動(dòng)時(shí)間
    public void setDuration(int mDuration) {
        this.mDuration = mDuration;
    }
 
    public void setSelectedTextColor(int selectedTextColor) {
        this.selectedTextColor = selectedTextColor;
    }
 
    public void setNormalTextColor(int normalTextColor) {
        this.normalTextColor = normalTextColor;
    }
 
    public void setSelectedTextBackgroundDrawable(Drawable selectedTextBackgroundDrawable) {
        this.selectedTextBackgroundDrawable = selectedTextBackgroundDrawable;
    }
 
    public void setSelectedTextBackgroundColor(int selectedTextBackgroundColor) {
        this.selectedTextBackgroundColor = selectedTextBackgroundColor;
    }
 
    public void setSelectedTextBackgroundResources(int selectedTextBackgroundResources) {
        this.selectedTextBackgroundResources = selectedTextBackgroundResources;
    }
 
    public interface OnSelectedChangedListener {
        void onChange(int index, Tab tag);
    }
 
    private OnSelectedChangedListener listener;
 
    public void setOnSelectedChangedListener(OnSelectedChangedListener listener) {
        if (listener != null) {
            this.listener = listener;
        }
    }
 
    private int dp2px(Context context, float dpValue) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return (int) (metrics.density * dpValue + 0.5F);
    }
 
 
    public interface Tab{
        String getText();
    }
 
    private float startX = 0f;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            startX = event.getX();
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            float endX = event.getX();
            //向左滑條件
            if (endX - startX > 50 && currentIndex > 0) {
                moveToRight();
            }
            if (startX - endX > 50 && currentIndex < getChildCount() - 1) {
                moveToLeft();
            }
        }
        return true;
    }
 
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            startX = event.getX();
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            float endX = event.getX();
            //向左滑條件
            if (Math.abs(startX-endX)>50){
                onTouchEvent(event);
            }
        }
        return super.onInterceptTouchEvent(event);
    }
}

在Activity或fragment中使用

private var tabs = listOf("慢動(dòng)作", "短視頻", "錄像", "拍照", "108M", "人像", "夜景", "萌拍", "全景", "專業(yè)")
    lateinit var  imageAnalysis:ImageAnalysis
 
    override fun initView() {
 
        //實(shí)現(xiàn)了CameraIndicator.Tab的對(duì)象
        val map = tabs.map {
            CameraIndicator.Tab { it }
        }?.toTypedArray() ?: arrayOf()
        //將tab集合設(shè)置給cameraIndicator,(binding.cameraIndicator即xml布局里的控件)
        binding.cameraIndicator.setTabs(*map)
        //默認(rèn)選中  拍照
        binding.cameraIndicator.currentIndex = 3
        
//點(diǎn)擊某個(gè)tab的回調(diào)
binding.cameraIndicator.setSelectedTextBackgroundResources(R.drawable.selected_text_bg)
 
        binding.cameraIndicator.setOnSelectedChangedListener { index, tag ->
            Toast.makeText(this,tag.text,Toast.LENGTH_SHORT).show()
        }
 
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android中使用listview實(shí)現(xiàn)qq/微信好友列表

    Android中使用listview實(shí)現(xiàn)qq/微信好友列表

    本文主要介紹了android中使用listview實(shí)現(xiàn)qq/微信好友列表(頭像,昵稱,個(gè)性簽名)的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • Android 簡單好用的屏幕適配方案

    Android 簡單好用的屏幕適配方案

    這篇文章主要介紹了Android 簡單好用的屏幕適配方案,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04
  • Android 下載網(wǎng)絡(luò)圖片并顯示到本地

    Android 下載網(wǎng)絡(luò)圖片并顯示到本地

    本文主要介紹了Android實(shí)現(xiàn)下載網(wǎng)絡(luò)圖片并顯示到本地功能的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-03-03
  • android生命周期深入分析(二)

    android生命周期深入分析(二)

    Android 程序的生命周期是由系統(tǒng)控制而非程序自身直接控制。這和我們編寫桌面應(yīng)用程序時(shí)的思維有一些不同,本文將詳細(xì)介紹,需要了解的朋友可以參考下
    2012-12-12
  • Android編程設(shè)計(jì)模式之原型模式實(shí)例詳解

    Android編程設(shè)計(jì)模式之原型模式實(shí)例詳解

    這篇文章主要介紹了Android編程設(shè)計(jì)模式之原型模式,結(jié)合實(shí)例形式詳細(xì)分析了Android設(shè)計(jì)模式之原型模式的概念、原理、定義、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-12-12
  • Android五大布局與實(shí)際應(yīng)用詳解

    Android五大布局與實(shí)際應(yīng)用詳解

    這篇文章主要為大家詳細(xì)介紹了Android五大布局與實(shí)際應(yīng)用,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Android實(shí)現(xiàn)ViewFlipper圖片動(dòng)畫滑動(dòng)

    Android實(shí)現(xiàn)ViewFlipper圖片動(dòng)畫滑動(dòng)

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)ViewFlipper圖片動(dòng)畫滑動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android設(shè)計(jì)模式之適配器(Adapter)模式

    Android設(shè)計(jì)模式之適配器(Adapter)模式

    這篇文章主要介紹了Android設(shè)計(jì)模式之適配器(Adapter)模式,以源碼解析的方式分析適配器模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android自定義控件實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)

    Android自定義控件實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)

    這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Flutter自定義下拉刷新時(shí)的loading樣式的方法詳解

    Flutter自定義下拉刷新時(shí)的loading樣式的方法詳解

    Flutter中的下拉刷新,我們通常RefreshIndicator,可以通過color或strokeWidth設(shè)置下拉刷新的顏色粗細(xì)等樣式,但如果要自定義自己的widget,RefreshIndicator并沒有暴露出對(duì)應(yīng)的屬性,那如何修改呢,文中給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01

最新評(píng)論

来凤县| 三明市| 萨迦县| 莲花县| 禹州市| 五台县| 藁城市| 建阳市| 思南县| 遂溪县| 宝兴县| 康乐县| 岑溪市| 双鸭山市| 万年县| 湛江市| 南丰县| 邵阳县| 五莲县| 佛教| 卫辉市| 五家渠市| 中卫市| 清远市| 阿城市| 云霄县| 永兴县| 共和县| 寿光市| 长葛市| 镇康县| 同德县| 西和县| 讷河市| 永嘉县| 博白县| 佛山市| 万州区| 枝江市| 新野县| 柘荣县|