티스토리 뷰

아래에 동영상 링크

class MainActivity : AppCompatActivity() {
    private lateinit var TargetNo: ImageView
    private lateinit var ElapsedTime: TextView
    private var displayText: String = "Press the Bluetooth Remote" // Initialize with default text
    private val handler = Handler(Looper.getMainLooper())
    private var ElapsedTime_Seconds:Int = 0

    companion object {
        private const val ELAPSED_TIME_KEY = "elapsed_time"
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        TargetNo = findViewById(R.id.iv_targetno)
        ElapsedTime = findViewById(R.id.tv_timestring)

        savedInstanceState?.let {
            ElapsedTime_Seconds = it.getInt(ELAPSED_TIME_KEY, 0)
        }

        startTimer()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        // 상태 저장
        outState.putInt(ELAPSED_TIME_KEY, ElapsedTime_Seconds)
    }

    private fun startTimer() {
        // 1초마다 현재 시간을 업데이트하여 텍스트뷰에 표시
        handler.postDelayed(object : Runnable {
            override fun run() {
                val currentTime = SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date())
                ElapsedTime_Seconds += 1
                ElapsedTime.text = "경과 시간: $ElapsedTime_Seconds [초]"
                handler.postDelayed(this, 1000) // 1초 후에 다시 호출
            }
        }, 1000) // 처음 실행은 1초 후에 시작
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        // Check if the back button is pressed
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Finish the activity, which will effectively close the app
            finish()
            return true
        }
        // Get the key value and the current time
        val keyName = KeyEvent.keyCodeToString(keyCode)
        val currentTime = SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date())
        displayText = "Key: $keyName\nTime: $currentTime"
        // Convert the text to a bitmap and set it to the ImageView
        updateImageView()

        return true
    }

    override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {
        super.onConfigurationChanged(newConfig)
        // Update the image view with a new color on configuration change
        updateImageView()
    }

    private fun updateImageView() {
        val colors = listOf(Color.RED, Color.GREEN, Color.BLUE, Color.MAGENTA, Color.CYAN)
        val color = colors.random()
        val randonNo = Random.nextInt(10)
        val bitmap = textToBitmap(randonNo.toString(), color)
        TargetNo.setImageBitmap(bitmap)
    }

    private fun textToBitmap(text: String, color: Int): Bitmap {
        val paint = Paint()
        paint.color = color
        paint.textAlign = Paint.Align.CENTER

        // ImageView의 크기 가져오기
        val imageViewWidth = TargetNo.width.toFloat()
        val imageViewHeight = TargetNo.height.toFloat()

        // 텍스트 크기를 ImageView의 크기에 맞게 조정하기
        paint.textSize = imageViewHeight * 0.8f // 임의의 비율로 조정 (예: 80%)

        // 텍스트 그리기
        val textBounds = android.graphics.Rect()
        paint.getTextBounds(text, 0, text.length, textBounds)

        val bitmap = Bitmap.createBitmap(imageViewWidth.toInt(), imageViewHeight.toInt(), Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        canvas.drawText(text, imageViewWidth / 2f, (imageViewHeight + textBounds.height()) / 2f, paint)
        return bitmap
    }


}

 

 

 

https://youtu.be/CjGfzzY3FYc

 

 

반응형