PullBezierZoomView 一個(gè)具有貝塞爾曲線下拉效果的自定義view

該控件具有下拉放大背景圖和貝塞爾曲線的效果.

該控件效果基于PullZoomView源碼改動(dòng)的而來(lái),感謝Frank-Zhu的開(kāi)源代碼.

github:https://github.com/X-FAN/PullBezierZoomView 歡迎star

我主要寫了一個(gè)自定義貝塞爾曲線效果的控件并整合到了Frank-Zhu的項(xiàng)目中的一個(gè)子項(xiàng)中.

這里面有個(gè)小數(shù)學(xué)知識(shí)的求解,因?yàn)樾Ч愘悹柷€的曲線頂點(diǎn)要恰好在控件底部邊界的重點(diǎn).所以我們是知道ABC三點(diǎn),去求貝塞爾曲線的控制點(diǎn).具體求解過(guò)程就不分析了,大家google二階貝塞爾曲線的公式,很容易就可以推算出來(lái).

這里寫圖片描述

貝塞爾曲線效果控件源碼如下:

public class BezierView extends View {

    private int mWidth = 500;
    private int mHeight = 500;
    private float mMaxHeight = Integer.MAX_VALUE;
    private float mY = 0;

    private Paint mPaint;
    private Path mPath;


    public BezierView(Context context) {
        this(context, null);
    }

    public BezierView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BezierView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPath = new Path();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            width = Math.min(mWidth, widthSize);
        } else {
            width = mWidth;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            height = Math.min(mHeight, heightSize);
        } else {
            height = mHeight;
        }
        setMeasuredDimension(width, height);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();//繪制的主要邏輯代碼
        mPath.moveTo(0, mHeight - mY);
        mPath.quadTo(mWidth / 2, mHeight + mY, mWidth, mHeight - mY);
        mPath.lineTo(mWidth, mHeight);
        mPath.lineTo(0, mHeight);
        mPath.close();
        canvas.drawPath(mPath, mPaint);
    }

    public void setArcHeight(float height) {
        if (Math.abs(height) < mMaxHeight) {
            mY = height;
            invalidate();
        }
    }

    public float getArcHeight() {
        return mY;
    }

    public void setColor(int color) {
        mPaint.setColor(color);
    }

    public void setMaxHeight(float height) {
        mMaxHeight = height;
    }

這里提下Frank-Zhu的項(xiàng)目中放大縮小功能的實(shí)現(xiàn)是相當(dāng)?shù)穆斆?看了源碼發(fā)現(xiàn)他是利用ImagView中的scaleType="centerCrop"屬性,只要改變控件的高度,就具有了放大縮小的效果.不用自己寫額外的代碼,確實(shí)很方便.

效果圖:

bezier.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評(píng)論 25 708
  • 一、Path常用方法表 二.Path詳解 上一次除了一些常用函數(shù)之外,講解的基本上都是直線,本次需要了解其中的曲線...
    呂侯爺閱讀 1,766評(píng)論 1 6
  • 貝塞爾曲線開(kāi)發(fā)的藝術(shù) 一句話概括貝塞爾曲線:將任意一條曲線轉(zhuǎn)化為精確的數(shù)學(xué)公式。 很多繪圖工具中的鋼筆工具,就是典...
    eclipse_xu閱讀 28,022評(píng)論 38 370
  • 前言 扯來(lái)扯去,前面三篇自定義 View 文章,終于扯完了一些知識(shí)點(diǎn),有些枯燥,所以我也是講下核心點(diǎn),沒(méi)有細(xì)分析,...
    GitHubClub閱讀 1,638評(píng)論 2 27
  • 以前總是自我感覺(jué)良好,覺(jué)得只能靠自己的力量來(lái)帶孩子,而且,仿佛只有自己才是真正為孩子們好,他們是那么嬌弱、那么需要...
    華麗的美麗麗閱讀 219評(píng)論 0 0

友情鏈接更多精彩內(nèi)容