【自定義 View】相機(jī)預(yù)覽點(diǎn)擊聚焦框(帶對(duì)焦動(dòng)畫滥崩,曝光調(diào)整)

效果預(yù)覽

??時(shí)隔十個(gè)月,今天再次來分享項(xiàng)目中使用到的自定義View讹语,話不多說钙皮,直接開始,主要在相機(jī)預(yù)覽的時(shí)候點(diǎn)擊預(yù)覽界面對(duì)焦時(shí)使用顽决,并增加了對(duì)焦動(dòng)畫短条,可調(diào)節(jié)曝光〔挪ぃ可以設(shè)置曝光的上限和下限茸时,用于調(diào)整曝光時(shí)回調(diào),點(diǎn)擊后無操作5秒后隱藏赋访】啥迹總的來說就是這些缓待,下面開始上代碼

對(duì)焦框

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    val width = MeasureSpec.getSize(widthMeasureSpec)
    val height = MeasureSpec.getSize(heightMeasureSpec)
    frameRadius = width / 5f
    frameRectF.left = (width / 2f) - frameRadius
    frameRectF.right = (width / 2f) + frameRadius
    frameRectF.top = (height / 2f) - frameRadius
    frameRectF.bottom = (height / 2f) + frameRadius
    _14 = frameRectF.height() / 4f
}

override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)
    canvas?.apply {
        val points = floatArrayOf(
        frameRectF.left, frameRectF.top, frameRectF.left, frameRectF.top + _14,
        frameRectF.left, frameRectF.top, frameRectF.left + _14, frameRectF.top,
        frameRectF.left, frameRectF.bottom, frameRectF.left, frameRectF.bottom - _14,
        frameRectF.left, frameRectF.bottom, frameRectF.left + _14, frameRectF.bottom,
        frameRectF.right, frameRectF.top, frameRectF.right, frameRectF.top + _14,
        frameRectF.right, frameRectF.top, frameRectF.right - _14, frameRectF.top,
        frameRectF.right, frameRectF.bottom, frameRectF.right, frameRectF.bottom - _14,
        frameRectF.right, frameRectF.bottom, frameRectF.right - _14, frameRectF.bottom)
        drawLines(points, framePaint)
    }
}

??這個(gè)對(duì)焦框顯而易見,其實(shí)就是畫了八條線渠牲,_14是用來控制邊框線條的長度的旋炒,想要邊框間距更近一些,就改成
_14 = frameRectF.height() / 3f

小太陽

1.按住顯示的直線

override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)
    canvas?.apply {
        // 畫直線
        if (showLine) {
            if (circleY != circleRadius + dp8) {
                drawLine(centerOfCircle, 0f, centerOfCircle, (height * progress) - (circleRadius) - dp10, sunPaint)
            }
            if (circleY != height - (circleRadius) - dp8) {
                drawLine(centerOfCircle, (height * progress) + (circleRadius) + dp10, centerOfCircle, height * 1f, sunPaint)
            }
        }
    }
}

@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent?): Boolean {
    event?.let { ev ->
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                if (circleY < 0f) {
                    circleY = height * progress
                    lastCircleY = circleY
                }
                posY = event.y
                paintColor = Color.WHITE
            }
            MotionEvent.ACTION_MOVE -> {
                curPosY = event.y
                paintColor = Color.WHITE
                if ((curPosY - posY > 0) || (curPosY - posY < 0)) {
                    showLine = true
                    invalidate()
                }
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                showLine = false
                invalidate()
            }
            else -> {

            }
        }
    }
    return true
}

??直線受showLine影響顯示或隱藏签杈,showLine會(huì)在手指移動(dòng)的時(shí)候會(huì)修改為true瘫镇,這個(gè)時(shí)候就會(huì)顯示小太陽后的直線;circleY是記錄小太陽y軸的位置答姥,而(circleY != circleRadius + dp8)是用來判斷當(dāng)前小太陽是否處于最上方铣除,如果不是的話,就會(huì)繪制小太陽上方的直線踢涌,(circleY != height - (circleRadius) - dp8)同理通孽,判斷是否處于最下方。

2.小太陽外部

override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)
    canvas?.apply {
        // 畫圓,空心圓
        drawCircle(centerOfCircle, height * progress, circleRadius, sunPaint)
        // 畫線條
        for (i in 0 until 8) {
            val startPointF = calculationPoint(angle - (i * 45f), circleRadius + dp3)
            val endPointF = calculationPoint(angle - (i * 45f), circleRadius + dp5)
            borderWidth = 5f
            sunPaint.strokeWidth = borderWidth
            canvas.drawLine(startPointF.x, startPointF.y, endPointF.x, endPointF.y, sunPaint)
            borderWidth = 3f
        }
    }
}

/**
 * 計(jì)算圓上任意點(diǎn)的坐標(biāo)
 * @param angle 角度
 * @param radius 半徑
 * @return 點(diǎn)坐標(biāo)
 */
private fun calculationPoint(angle: Float, radius: Float): PointF {
    val x = (centerOfCircle) + (radius) * cos(angle * Math.PI / 180f).toFloat()
    val y = (height * progress) + (radius) * sin(angle * Math.PI / 180f).toFloat()
    return PointF(x, y)
}

??畫空心圓沒啥好說的睁壁,height * progress其實(shí)意義上也就是circleY背苦。至于小太陽上下滑動(dòng)外部線條也跟著轉(zhuǎn)是不斷改變angle,從線條最上面移動(dòng)到最下面潘明,就是從0到360的變化行剂。不斷的改變角度,達(dá)到根據(jù)手指移動(dòng)旋轉(zhuǎn)的效果钳降。

3.小太陽內(nèi)部

override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)
    canvas?.apply {
        // 畫中間月亮效果
        if (realProcess < .5f) {
            // 張弦月
            val left = centerOfCircle - (((circleRadius - dp2) * 2f) * abs(realProcess - 0.5f))
            val top = (height * progress) - (circleRadius - dp2)
            val right = centerOfCircle + (((circleRadius - dp2) * 2f) * abs(realProcess - 0.5f))
            val bottom = (height * progress) + (circleRadius - dp2)
            drawOval(left, top, right, bottom, moonPaint)
            drawArc(centerOfCircle - (circleRadius - dp2), (height * progress) - (circleRadius - dp2),
                centerOfCircle + (circleRadius - dp2), (height * progress) + (circleRadius - dp2),
                90f, 180f, false, moonPaint)
        } else if (realProcess == .5f) {
            // 下弦月
            drawArc(centerOfCircle - (circleRadius - dp2), (height * progress) - (circleRadius - dp2),
                centerOfCircle + (circleRadius - dp2), (height * progress) + (circleRadius - dp2),
                90f, 180f, false, moonPaint)
        } else {
            // 殘?jiān)?            val save = saveLayer(null, null)
            val left = centerOfCircle - (((circleRadius - dp2) * 2f) * abs(realProcess - 0.5f))
            val top = (height * progress) - (circleRadius - dp2)
            val right = centerOfCircle + (((circleRadius - dp2) * 2f) * abs(realProcess - 0.5f))
            val bottom = (height * progress) + (circleRadius - dp2)
            drawArc(centerOfCircle - (circleRadius - dp2 - 1), (height * progress) - (circleRadius - dp2 - 1),
                centerOfCircle + (circleRadius - dp2 - 1), (height * progress) + (circleRadius - dp2 - 1),
                90f, 180f, false, moonPaint)
            moonPaint.xfermode = porterDuffDstOut
            drawOval(left, top, right, bottom, moonPaint)
            moonPaint.xfermode = null
            restoreToCount(save)
        }
    }
}

??小太陽中間的月亮效果是參考了蹭中秋熱度來了~Android 自定義View——月有陰晴圓缺這個(gè)文章厚宰,感興趣的可以去看一下∷焯睿總的來說铲觉,這個(gè)月亮主要分為三個(gè)部分,殘?jiān)?> 下弦月 > 張弦月吓坚。

??殘?jiān)潞蛷埾以碌脑硎钱嬃艘粋€(gè)下弦月撵幽,在下弦月的基礎(chǔ)上,再畫一個(gè)橢圓礁击,這個(gè)橢圓是高度和下弦月相同盐杂,寬度動(dòng)態(tài)向兩邊延伸的。橢圓的效果如下:


效果圖

??這里放大了整個(gè)View哆窿,也是為了更加直觀的看到橢圓的變化链烈。看到這里就可以明白張弦月其實(shí)就是這個(gè)橢圓和下弦月疊放在一起的效果挚躯,那殘?jiān)率侨绾螌?shí)現(xiàn)的呢强衡,這里用到了PorterDuffXfermode,對(duì)于PorterDuffXfermode不了解的可以看下Google的官方文檔码荔,文章中用到的是DST_OUT漩勤,效果如圖:

效果圖

??使用PorterDuff.Mode.DST_OUT將可變化的橢圓和下弦月疊放在一起就實(shí)現(xiàn)了殘?jiān)碌男Ч?/p>

總結(jié)

??到這里就結(jié)束了号涯,那么如何使用呢,代碼如下锯七,首先是xml:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <View
        android:id="@+id/preview_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black" />

    <com.lazyiones.focussunview.FocusSunView
        android:id="@+id/focus_sun_view"
        android:layout_width="120dp"
        android:layout_height="140dp"
        android:visibility="invisible"
        app:layout_constraintStart_toStartOf="@+id/preview_view"
        app:layout_constraintTop_toTopOf="@+id/preview_view" />

</androidx.constraintlayout.widget.ConstraintLayout>

??這里一定要把visibility設(shè)置為invisible,不然第一次顯示無法正常處理寬高誉己,對(duì)焦框顯示的位置會(huì)偏離眉尸,preview_view顧名思義就是預(yù)覽的View。下面是Activity中:

val focusSunView = findViewById<FocusSunView>(R.id.focus_sun_view)

findViewById<View>(R.id.preview_view).setOnTouchListener { _, motionEvent ->
    when (motionEvent.action) {
        MotionEvent.ACTION_DOWN -> {
            focusSunView.visibility = View.VISIBLE
            focusSunView.translationX = motionEvent.x - (focusSunView.width / 2f)
            focusSunView.translationY = motionEvent.y - (focusSunView.height / 2f)
            focusSunView.startCountdown()
        }
    }
    return@setOnTouchListener true
}

focusSunView.setOnExposureChangeListener(object : FocusSunView.OnExposureChangeListener {
    override fun onExposureChangeListener(exposure: Float) {
        // 處理相機(jī)曝光
        Log.e("FocusSunView", "onExposureChangeListener: -----------> $exposure")
    }
})

??OK巨双,大致就是這樣噪猾,有寫的不好的地方歡迎指正。
源碼地址:FocusSunView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末筑累,一起剝皮案震驚了整個(gè)濱河市袱蜡,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌慢宗,老刑警劉巖坪蚁,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異镜沽,居然都是意外死亡敏晤,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門缅茉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嘴脾,“玉大人,你說我怎么就攤上這事蔬墩∫氪颍” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵拇颅,是天一觀的道長奏司。 經(jīng)常有香客問我,道長蔬蕊,這世上最難降的妖魔是什么结澄? 我笑而不...
    開封第一講書人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮岸夯,結(jié)果婚禮上麻献,老公的妹妹穿的比我還像新娘。我一直安慰自己猜扮,他們只是感情好勉吻,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著旅赢,像睡著了一般齿桃。 火紅的嫁衣襯著肌膚如雪惑惶。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評(píng)論 1 305
  • 那天短纵,我揣著相機(jī)與錄音带污,去河邊找鬼。 笑死香到,一個(gè)胖子當(dāng)著我的面吹牛鱼冀,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播悠就,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼千绪,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了梗脾?” 一聲冷哼從身側(cè)響起荸型,我...
    開封第一講書人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎炸茧,沒想到半個(gè)月后瑞妇,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡梭冠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年踪宠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片妈嘹。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡柳琢,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出润脸,到底是詐尸還是另有隱情柬脸,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布毙驯,位于F島的核電站倒堕,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏爆价。R本人自食惡果不足惜垦巴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望铭段。 院中可真熱鬧骤宣,春花似錦、人聲如沸序愚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至芬膝,卻和暖如春望门,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背锰霜。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來泰國打工筹误, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人癣缅。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓纫事,卻偏偏與公主長得像,于是被迫代替她去往敵國和親所灸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 文/攝圖‖琴心劍膽teniuqi 初七八 月半又半期 我拉如弓上弦 朝向日落之西 一半正圓 一半正缺 是人們眼...
    琴心劍膽閱讀 3,140評(píng)論 49 43
  • 如果說冬至是一個(gè)關(guān)于太陽的故事炫七,中秋爬立,就是一個(gè)關(guān)于月亮的故事。 你知道月亮都在什么時(shí)間升起和降落嗎万哪? 當(dāng)然知道侠驯,太...
    董點(diǎn)國學(xué)大董閱讀 1,118評(píng)論 0 6
  • 1、人類對(duì)宇宙的認(rèn)識(shí)過程:天圓地方說奕巍、地圓說吟策、地心說、日心說的止、大爆炸宇宙學(xué)說檩坚。 2、宇宙的基本特點(diǎn):由各種形態(tài)的物...
    阿稻兒閱讀 230評(píng)論 0 3
  • 下面咱們繼續(xù)“日月盈昃”,日月咱都不講了氓润,大家都知道是什么東西赂乐,也不展開《易經(jīng)》講的什么“兩儀生四象”了等等...
    臥云軒主人閱讀 764評(píng)論 0 1
  • 今年的大年初一挨措,也就是2019年2月5日,是我在周歷中提到的“水瓶座新月”的日子崩溪。東西方歷法有很多相似之處浅役,比如,...
    膩蟲閱讀 1,141評(píng)論 1 2