Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The scale type for image is "center".
- private fun setScroll(image: Drawable) {
- // DisplayMetrics displayMetrics = new DisplayMetrics();
- // getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
- // Set maximum scroll amount (based on center of image)
- val bitmapWidth = (image as BitmapDrawable).bitmap.width.toFloat()
- val screenWidth = MATCH_PARENT
- val maxX = (bitmapWidth / 2 - screenWidth / 2).toInt()
- val bitmapHeight = image.bitmap.height.toFloat()
- val screenHeight = MATCH_PARENT
- val maxY = (bitmapHeight / 2 - screenHeight / 2).toInt()
- // Set scroll limits
- val maxLeft = maxX * -1
- val maxTop = maxY * -1
- // Set Touch Listener
- wallpaperImageView.setOnTouchListener(object : View.OnTouchListener {
- var downX: Float = 0.toFloat()
- var downY: Float = 0.toFloat()
- var totalX: Int = 0
- var totalY: Int = 0
- var scrollByX: Int = 0
- var scrollByY: Int = 0
- override fun onTouch(view: View, event: MotionEvent): Boolean {
- val currentX: Float
- val currentY: Float
- when (event.action) {
- MotionEvent.ACTION_DOWN -> {
- downX = event.x
- downY = event.y
- }
- MotionEvent.ACTION_MOVE -> {
- currentX = event.x
- currentY = event.y
- scrollByX = (downX - currentX).toInt()
- scrollByY = (downY - currentY).toInt()
- // Scrolling to left side of image (pic moving to the right)
- if (currentX > downX) {
- if (totalX == maxLeft) {
- scrollByX = 0
- }
- if (totalX > maxLeft) {
- totalX += scrollByX
- }
- if (totalX < maxLeft) {
- scrollByX = maxLeft - (totalX - scrollByX)
- totalX = maxLeft
- }
- }
- // Scrolling to right side of image (pic moving to the left)
- if (currentX < downX) {
- if (totalX == maxX) {
- scrollByX = 0
- }
- if (totalX < maxX) {
- totalX += scrollByX
- }
- if (totalX > maxX) {
- scrollByX = maxX - (totalX - scrollByX)
- totalX = maxX
- }
- }
- // Scrolling to top of image (pic moving to the bottom)
- if (currentY > downY) {
- if (totalY == maxTop) {
- scrollByY = 0
- }
- if (totalY > maxTop) {
- totalY += scrollByY
- }
- if (totalY < maxTop) {
- scrollByY = maxTop - (totalY - scrollByY)
- totalY = maxTop
- }
- }
- // Scrolling to bottom of image (pic moving to the top)
- if (currentY < downY) {
- if (totalY == maxY) {
- scrollByY = 0
- }
- if (totalY < maxY) {
- totalY += scrollByY
- }
- if (totalY > maxY) {
- scrollByY = maxY - (totalY - scrollByY)
- totalY = maxY
- }
- }
- wallpaperImageView.scrollBy(scrollByX, 0)
- downX = currentX
- downY = currentY
- }
- }
- return true
- }
- })
- }
Add Comment
Please, Sign In to add comment