Canvas可运行的移动位图

时间:2019-07-13 09:03:55

标签: android kotlin

我尝试这段代码,希望位图移动并第二次在txt中运行,但只有时间运行而位图不会。我希望你能告诉我如何按时间移动位图。

mario_left=0f
lateinit var runnable : Runnable

override fun onCreate(savedInstanceState: Bundle?) {
    layout1 = findViewById<LinearLayout>(R.id.layout_game)
    handler : Handler
    var i=0
    handler = Handler(Looper.getMainLooper())
    runnable = Runnable {
        i++
        txt_time.text = "Second:" + i.toString()
        mario_left+=10f
        background = draw_game (this)
        layout1.addView (background)
        handler.postDelayed(runnable, 1000)
    }
    handler.post(runnable)        
}

class draw_game(context: Context): View(context){
    override fun onDraw (canvas: Canvas) {
        canvas.drawRGB (0, 0, 255)
        val frame = BitmapFactory.decodeResource (resources, R.drawable.theme)
        val pict_mario = BitmapFactory.decodeResource (resources, R.drawable.mario)
        canvas.drawBitmap (frame, 0f, 0f, null)
        canvas.drawBitmap (pict_mario, Actvt_3().mario_left, Actvt_3().mario_top, null)
    }
}

0 个答案:

没有答案