在屏幕上移动图像时如何定界

时间:2018-06-24 17:01:55

标签: android image kotlin motionevent

我的图像正在屏幕上移动,但没有边界,即它可以移到上侧或右侧,左侧或下侧,然后消失。 我也想敬酒图像的当前位置。我怎么能得到它?

package com.example.viram.assignment
import android.app.Activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout.LayoutParams
import com.example.viram.assignment.R
import android.R.attr.data
import android.content.ClipData
import android.os.PersistableBundle
import android.view.View.DragShadowBuilder
import android.view.View.OnTouchListener
import android.view.DragEvent
import android.view.View.OnDragListener
import android.webkit.WebChromeClient
import android.widget.TextView

class imageposition : AppCompatActivity() {

    var img: ImageView? = null
    var xCoordinate: Float? = null
    var yCoordinate: Float? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_imageposition)
        img = findViewById<ImageView>(R.id.image)
        img?.setOnTouchListener(OnTouchListener { view, event ->
            when (event.actionMasked) {
                MotionEvent.ACTION_DOWN -> {
                    xCoordinate = view.x - event.rawX
                    yCoordinate = view.y - event.rawY
                }

                MotionEvent.ACTION_MOVE -> view.animate().x(event.rawX + 
                    xCoordinate as Float).y(event.rawY + yCoordinate as Float).setDuration(0).start()
                else -> return@OnTouchListener false
            }
            true
        })
    }
}

0 个答案:

没有答案