簡體   English   中英

使用 Kotlin 在 Android Studio 中的 View 中 ImageView 顫抖的問題

[英]Problem with ImageView trembling in View in Android Studio using Kotlin

當我使用動畫添加 ImageView 時,它可以正常工作。 但是,當我將播放器(宇航員)設置為通過交互移動時,ImageView 在不動時一直在顫抖。


問題

顯示播放器顫抖的 gif 圖像


我在下面附上了 Kotlin 中的所有相關代碼


我的游戲對象

open class MyGameObject(var x:Int, var y:Int, var dx:Int, var dy:Int, var image:Drawable) {
    var width:Int = 300
    var height:Int = 300

    open fun move(canvas:Canvas)
    {
        x += dx
        y += dy

        if(x > (canvas.width - width) || x < 0)
            dx = -dx
        if(y > (canvas.height - height) || y < 0)
            dy = -dy
        image.setBounds(x, y, x+width, y+width)
        image.draw(canvas)
    }
}

宇航員級(玩家)

import android.graphics.Canvas
import android.graphics.drawable.Drawable

class Astronaut(x:Int, y:Int, dx:Int, dy:Int, image: Drawable) : MyGameObject(x, y,dx, dy, image) {
    var px:Int=0
    var py:Int=0

    override fun move(canvas: Canvas) {
        if (px > x)
            x += 5
        else
            x -= 5

        if (py > y)
            y += 5
        else
            y -= 5

        image.setBounds(x, y, x + width, y + height)
        image.draw(canvas)

    }
}

我的表面視圖

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.*

class MySurfaceView(context: Context?, attrs: AttributeSet?) : SurfaceView(context, attrs), Runnable {

    var paint = Paint()
    var isRunning = true
    lateinit var myThread: Thread
    lateinit var myHolder: SurfaceHolder
    var myGameObjects = ArrayList<MyGameObject>()
    val astronautImage = context!!.resources.getDrawable(R.drawable.astronaut, null)
    val astronaut = Astronaut(100, 100, 0, 0, astronautImage)

    init {
        val asteroid = context!!.resources.getDrawable(R.drawable.asteroid, null)
        myGameObjects.add(MyGameObject(100, 100, 10, 10, asteroid))
        myGameObjects.add(astronaut)
        myThread = Thread(this)
        myThread.start()
        myHolder = holder // controls access to canvas
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        //return super.onTouchEvent(event)
        astronaut.px = event!!.x.toInt()
        astronaut.py = event!!.y.toInt()
        return true
    }

    override fun run() {
        while(isRunning)
        {
            if(!myHolder.surface.isValid)
            {
                continue
            }
            val canvas: Canvas = myHolder.lockCanvas() // prevent other threads using this section
            canvas.drawRect(0f, 0f, canvas.width.toFloat(), canvas.height.toFloat(), paint)
            for(gameObject in myGameObjects)
            {
                gameObject.move(canvas)
            }
            myHolder.unlockCanvasAndPost(canvas)
        }
    }
}

我已經嘗試更改宇航員類和 MySurfaceView 類中的值,但這沒有用。

我認為這是因為您沒有設置后備選項以防宇航員不動。 px等於宇航員的x時,宇航員進入 else 塊,因為px > x為假,然后它向左移動5 pixels ,然后返回到初始位置,因為現在px大於x

所以,根據你的邏輯,條件應該是:

x = x + when {
    px > x -> 5
    px == x -> 0
    px < x -> -5
}

y坐標相同,您需要宇航員移動的情況……這就是位圖在對角線路徑中*顫抖*的原因。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM