Guest User

Untitled

a guest
Nov 16th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. // The scale type for image is "center".
  2. private fun setScroll(image: Drawable) {
  3. // DisplayMetrics displayMetrics = new DisplayMetrics();
  4. // getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
  5.  
  6. // Set maximum scroll amount (based on center of image)
  7. val bitmapWidth = (image as BitmapDrawable).bitmap.width.toFloat()
  8. val screenWidth = MATCH_PARENT
  9. val maxX = (bitmapWidth / 2 - screenWidth / 2).toInt()
  10.  
  11. val bitmapHeight = image.bitmap.height.toFloat()
  12. val screenHeight = MATCH_PARENT
  13. val maxY = (bitmapHeight / 2 - screenHeight / 2).toInt()
  14.  
  15. // Set scroll limits
  16. val maxLeft = maxX * -1
  17. val maxTop = maxY * -1
  18.  
  19. // Set Touch Listener
  20. wallpaperImageView.setOnTouchListener(object : View.OnTouchListener {
  21. var downX: Float = 0.toFloat()
  22. var downY: Float = 0.toFloat()
  23. var totalX: Int = 0
  24. var totalY: Int = 0
  25. var scrollByX: Int = 0
  26. var scrollByY: Int = 0
  27.  
  28. override fun onTouch(view: View, event: MotionEvent): Boolean {
  29. val currentX: Float
  30. val currentY: Float
  31. when (event.action) {
  32. MotionEvent.ACTION_DOWN -> {
  33. downX = event.x
  34. downY = event.y
  35. }
  36.  
  37. MotionEvent.ACTION_MOVE -> {
  38. currentX = event.x
  39. currentY = event.y
  40. scrollByX = (downX - currentX).toInt()
  41. scrollByY = (downY - currentY).toInt()
  42.  
  43. // Scrolling to left side of image (pic moving to the right)
  44. if (currentX > downX) {
  45. if (totalX == maxLeft) {
  46. scrollByX = 0
  47. }
  48. if (totalX > maxLeft) {
  49. totalX += scrollByX
  50. }
  51. if (totalX < maxLeft) {
  52. scrollByX = maxLeft - (totalX - scrollByX)
  53. totalX = maxLeft
  54. }
  55. }
  56.  
  57. // Scrolling to right side of image (pic moving to the left)
  58. if (currentX < downX) {
  59. if (totalX == maxX) {
  60. scrollByX = 0
  61. }
  62. if (totalX < maxX) {
  63. totalX += scrollByX
  64. }
  65. if (totalX > maxX) {
  66. scrollByX = maxX - (totalX - scrollByX)
  67. totalX = maxX
  68. }
  69. }
  70.  
  71. // Scrolling to top of image (pic moving to the bottom)
  72. if (currentY > downY) {
  73. if (totalY == maxTop) {
  74. scrollByY = 0
  75. }
  76. if (totalY > maxTop) {
  77. totalY += scrollByY
  78. }
  79. if (totalY < maxTop) {
  80. scrollByY = maxTop - (totalY - scrollByY)
  81. totalY = maxTop
  82. }
  83. }
  84.  
  85. // Scrolling to bottom of image (pic moving to the top)
  86. if (currentY < downY) {
  87. if (totalY == maxY) {
  88. scrollByY = 0
  89. }
  90. if (totalY < maxY) {
  91. totalY += scrollByY
  92. }
  93. if (totalY > maxY) {
  94. scrollByY = maxY - (totalY - scrollByY)
  95. totalY = maxY
  96. }
  97. }
  98.  
  99. wallpaperImageView.scrollBy(scrollByX, 0)
  100. downX = currentX
  101. downY = currentY
  102. }
  103. }
  104.  
  105. return true
  106. }
  107. })
  108. }
Add Comment
Please, Sign In to add comment