1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| class PhotoView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { lateinit var bitmap: Bitmap lateinit var paint: Paint
var originalOffsetX = 0f var originalOffsetY = 0f
var offsetX = 0f var offsetY = 0f var smallScale = 0f var bigScale = 0f var currentScale = 0f get() { Log.i(TAG, "get: $field") return field } set(value) { field = value invalidate() Log.i(TAG, "set: $value") } var isLarge = false lateinit var gestureDetector: GestureDetector lateinit var scaleGestureDetector: ScaleGestureDetector val overScroller by lazy { OverScroller(context) } val scaleAnimation: ObjectAnimator by lazy { val temp = ObjectAnimator.ofFloat(this, "currentScale", 0f) temp.setFloatValues(smallScale, bigScale) temp }
companion object { const val SCALE_FACTOR = 1.5f }
fun initBitmap(bitmap: Bitmap) { if (width == 0 || height == 0) { throw IllegalStateException("call after View is rendered") } this.bitmap = bitmap paint = Paint(Paint.ANTI_ALIAS_FLAG) gestureDetector = GestureDetector(context, PhotoGesture()) scaleGestureDetector = ScaleGestureDetector(context, PhotoScaleGestureListener()) originalOffsetX = (width - bitmap.width) / 2f originalOffsetY = (height - bitmap.height) / 2f if (bitmap.width.toFloat() / bitmap.height > width.toFloat() / height) { smallScale = width.toFloat() / bitmap.width.toFloat() bigScale = (height.toFloat() / bitmap.height.toFloat()) * SCALE_FACTOR } else { smallScale = height.toFloat() / bitmap.height.toFloat() bigScale = (width.toFloat() / bitmap.width.toFloat()) * SCALE_FACTOR } currentScale = smallScale invalidate() }
private fun fixOffset(){ offsetX = min(offsetX,(bitmap.width * bigScale - width)/ 2) offsetX = max(offsetX, -(bitmap.width * bigScale - width) / 2) offsetY = min(offsetY, (bitmap.height * bigScale - height) / 2) offsetY = max(offsetY, -(bitmap.height * bigScale - height) / 2) }
override fun onDraw(canvas: Canvas?) { if (this::bitmap.isInitialized) { val scaleFaction = (currentScale - smallScale) / (bigScale - smallScale) canvas?.translate(offsetX * scaleFaction, offsetY * scaleFaction) canvas?.scale(currentScale, currentScale, (width / 2).toFloat(), (height / 2).toFloat()) canvas?.drawBitmap(bitmap, originalOffsetX, originalOffsetY, paint) } }
override fun onTouchEvent(event: MotionEvent?): Boolean { var res = scaleGestureDetector.onTouchEvent(event) if (!scaleGestureDetector.isInProgress) { res = gestureDetector.onTouchEvent(event) } return res }
val runnable = object :Runnable{ override fun run() { if (overScroller.computeScrollOffset()){ offsetX = overScroller.currX.toFloat() offsetY = overScroller.currY.toFloat() invalidate() postOnAnimation(this) } } }
inner class PhotoGesture : GestureDetector.SimpleOnGestureListener() {
override fun onDown(e: MotionEvent?): Boolean { return true }
override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean { if (isLarge) { overScroller.fling(offsetX.toInt(), offsetY.toInt(), velocityX.toInt(), velocityY.toInt(), (-(bitmap.width * bigScale - width) / 2).toInt(), ((bitmap.width * bigScale - width) / 2).toInt(), (-(bitmap.height * bigScale - height) / 2).toInt(), ((bitmap.height * bigScale - height) / 2).toInt(), 300,300) postOnAnimation(runnable) } return super.onFling(e1, e2, velocityX, velocityY) }
override fun onDoubleTap(e: MotionEvent?): Boolean { isLarge = !isLarge if (isLarge) { e?.let { offsetX = (it.x - width / 2f) - (it.x - width / 2f) * bigScale / smallScale offsetY = (it.y - height / 2f) - (it.y - height / 2f) * bigScale / smallScale } fixOffset() scaleAnimation.start() } else { scaleAnimation.reverse() }
return super.onDoubleTap(e) }
override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean { if (isLarge) { offsetX -= distanceX offsetY -= distanceY fixOffset() invalidate() } return super.onScroll(e1, e2, distanceX, distanceY) }
}
inner class PhotoScaleGestureListener:ScaleGestureDetector.OnScaleGestureListener{ var initScale = 0f override fun onScaleBegin(detector: ScaleGestureDetector?): Boolean { initScale = currentScale return true }
override fun onScaleEnd(detector: ScaleGestureDetector?) {
}
override fun onScale(detector: ScaleGestureDetector?): Boolean { if ((currentScale > smallScale && !isLarge) || (currentScale == smallScale && !isLarge)){ isLarge = !isLarge } detector?.let { currentScale = if (initScale * it.scaleFactor < smallScale){ smallScale } else{ initScale * it.scaleFactor } } invalidate()
return false } } }
|