Skip to content

Upload 깊이선택, Diary 깊이수정

Jo Hyun Jin edited this page Jan 15, 2021 · 3 revisions

Upload 깊이선택, Diary 깊이수정

image

  • 배경은 scrollview, 안에 각각의 깊이는 imageView
    • 각 깊이의 높이는 디바이스의 높이
// 한 단계의 height = 디바이스 height
val displayHeight = applicationContext.resources.displayMetrics.heightPixels
for(i in 1..7) {
    val params = resources.getIdentifier(
        "@id/bg_deep${i}",
        "id",
        this.packageName
    )
    val img = view.findViewById(params) as ImageView
    val layoutParams = img.layoutParams
    layoutParams.height = displayHeight
}
  • 총 3개의 Vertical 시크바 사용 - custom seekbar
    • 가장 왼쪽 mainSeekbar를 기준으로 오른쪽에 선과 텍스트(깊이)도 연동
// 수정 전 단계를 progress에 넣어줘야함 !!
mainSeekbar.progress = depth
scrollToDepth() // 수정 전 단계에 따라 배경색 변화

// 처음 실행될 때 - line과 text 시크바를 main과 동일한 단계로 설정
lineSeekbar.progress = mainSeekbar.progress
lineSeekbar.thumb = lineThumb.getThumb()
lineSeekbar.setOnTouchListener { _, _ -> true }

textSeekbar.progress = mainSeekbar.progress
(textThumb.findViewById(R.id.tv_seekbar_depth) as TextView).text = getDepth(textSeekbar.progress)
textSeekbar.thumb = textThumb.getThumb()
textSeekbar.setOnTouchListener { _, _ -> true }


// mainSeekbar listener
mainSeekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
    override fun onProgressChanged(p0: SeekBar?, progress: Int, p2: Boolean) {
        lineSeekbar.progress = progress
        lineSeekbar.thumb = lineThumb.getThumb()

        textSeekbar.progress = progress
        (textThumb.findViewById(R.id.tv_seekbar_depth) as TextView).text = getDepth(textSeekbar.progress)
        textSeekbar.thumb = textThumb.getThumb()

        svDeep.smoothScrollToView(depthImg())
    }

    override fun onStartTrackingTouch(p0: SeekBar?) {

    }

    override fun onStopTrackingTouch(p0: SeekBar?) {

    }

})
  • 부드럽게 배경이 움직이는 효과를 위해 custom scrollTo 사용
// scrollview 부드럽게 움직이도록
private fun ScrollView.computeDistanceToView(view: View): Int {
    return abs(calculateRectOnScreen(this).top - (this.scrollY + calculateRectOnScreen(view).top))
}

private fun calculateRectOnScreen(view: View): Rect {
    val location = IntArray(2)
    view.getLocationOnScreen(location)
    return Rect(
        location[0],
        location[1],
        location[0] + view.measuredWidth,
        location[1] + view.measuredHeight
    )
}

private fun ScrollView.smoothScrollToView(
    view: View,
    marginTop: Int = 0,
    maxDuration: Long = 500L,
    onEnd: () -> Unit = {}
) {
    if (this.getChildAt(0).height <= this.height) {
        onEnd()
        return
    }
    val y = computeDistanceToView(view) - marginTop
    val ratio = abs(y - this.scrollY) / (this.getChildAt(0).height - this.height).toFloat()
    ObjectAnimator.ofInt(this, "scrollY", y).apply {
        duration = (maxDuration * ratio).toLong()
        interpolator = AccelerateDecelerateInterpolator()
        doOnEnd {
            onEnd()
        }
        start()
    }
}