大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

Kotlinで作るAndroidアプリ・・アナログ時計の日付と曜日の表示

著者:川上洋
公開日:2019/08/07
最終更新日:2019/08/07
カテゴリー:技術情報 雑記

こんにちは。川上です。

一応、蛇足の後なので、背ビレと腹ヒレかな。

やっぱり、アナログ時計に日付と曜日がないとね。

    // ⚡️--ライフサイクル:レイアウト処理終了メソッド
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)

        val metrics : DisplayMetrics = this.getResources().getDisplayMetrics()
        println("--- metrics.density = $metrics.density ---")

        val width = metrics.widthPixels   
        val height = metrics.heightPixels
        println("--- println() --> Width = $width,Height  = $height ---")


        // 半分の3/4の長さ
        var hankei_bs = (if (width > height) height else width ) / 2
        hankei  = hankei_bs * 3 / 4
        println("--- hankei = $hankei ---")

        // 時計盤の数字表示
        dispClockBoard(hankei)

        // ---- Now時刻の表示
        updateTimeClock()

        // 日付、Weekday表示
        dispKoyomiDay(hankei)
    }
    // 日付、Weekday表示
    fun dispKoyomiDay(hankei_dp :Int){
        val weekdays: Array<String> = arrayOf("日", "月", "火", "水", "木", "金", "土")
        val baseVw = this.base_view
        val dateTime: DateTime = DateTime.now()

        for(i in 0..1) {
            val textView = TextView(this)
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.text_size))
            val layoutParams = ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.WRAP_CONTENT,
                ConstraintLayout.LayoutParams.WRAP_CONTENT)

            layoutParams.circleConstraint =  base_png.id
            // 時計数字位置の取得
            layoutParams.circleAngle = CircleUtil.computeAngleByIndex(PARTITIONS, 3)

            val font_w =  textView.textSize
            if (i == 0){ //  日
                if (dateTime.day < 10 ) {
                    layoutParams.circleRadius = hankei_dp/2 - font_w.toInt() / 2
                    textView.text = " " + dateTime.day.toString() + " "
                }
                else {
                    layoutParams.circleRadius = hankei_dp/2 - font_w.toInt()
                    textView.text = dateTime.day.toString()
                }
            }
            else {      // 曜日
                layoutParams.circleRadius = hankei_dp/2 + font_w.toInt()
                textView.text = weekdays[dateTime.dayOfWeek - 1]
                if (dateTime.dayOfWeek - 1 == 0) {      // 日曜日
                    textView.setTextColor(Color.RED)
                }
                else if (dateTime.dayOfWeek - 1 == 6) { // 土曜日
                    textView.setTextColor(Color.BLUE)
                }
                else {
                    textView.setTextColor(Color.BLACK)
                }
            }

            textView.layoutParams = layoutParams
            textView.setBackgroundColor(Color.WHITE)

            // 時計盤の数文字の組み込み
            baseVw.addView(textView)
        }
    }

アナログ時計アプリのSwift版コチラとKotlin版こちらは、一切りでオ・ワ・リ。

以前に、寝床本で読んでた

これで覚えたら・・ラッキーかもね

ではでは。

    上に戻る