suer TIL
eye catch
【Android,Kotlin】端末で写真を撮って表示する
アプリのイメージ カメラアプリで写真を撮って表示したい。 実装 レイアウト ImageViewとボタンを設置しておく。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="300dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/imageView" /> </androidx.constraintlayout.widget.ConstraintLayout> cacheフォルダへの書き込みを許可する 外部のカメラアプリがアプリのcacheフォルダへ書き込み、アプリはそれを画面に表示する。
ANDROID
2022-08-23 2023-03-31
eye catch
【Android,Kotlin】GetContentで端末に保存された画像ファイルを選択して表示する
アプリのイメージ 端末に保存されている画像を選択したい。 関連: 【Android,Kotlin】OpenDocumentで端末内の画像ファイルを選択して表示する 実装 レイアウト ImageViewとボタンを設置しておく。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="300dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/imageView" /> </androidx.constraintlayout.widget.ConstraintLayout> Fragment/ViewModel フィールドにregisterForActivityResultでActivityResultContracts.GetContent()のlauncherを作る。
ANDROID
2022-08-23 2023-03-31
eye catch
【Android,Kotlin】Kotlinらしいダウンキャスト
ダウンキャストが必要となる例 open class Super {} class A : Super() { fun f() {} } val x: Super = A() x.f() // x は Super なので f メソッドがわからずエラー: if+is演算子で分岐する Kotlinでは実行箇所の文脈によって自動的に推論されてダウンキャストされる。 例えば上記例では if (x is A) でチェックすると、そのなかではxはAのインスタンスであると判断されるので Aクラスのインスタンスメソッドf()が呼べるようになる。
ANDROID
2022-08-22 2023-03-31
eye catch
【Android,Kotlin】KtlintでKotlinコードのチェック
Ktlint KtlintはKotlin用のlinter。 本記事ではビルド設定(Gradle)に組み込んで使用する。 app/build.gradleに設定を追加する configurations { ktlint } dependencies { ... // バージョンは本家を確認すること https://github.com/pinterest/ktlint ktlint "com.pinterest:ktlint:0.48.2" } // lintタスクを追加 task ktlint(type: JavaExec, group: "verification") { description = "Check Kotlin code style." classpath = configurations.ktlint main = "com.pinterest.ktlint.Main" args "src/**/*.kt" } check.dependsOn ktlint // フォーマットタスクを追加 task ktlintFormat(type: JavaExec, group: "formatting") { description = "Fix Kotlin code style deviations." classpath = configurations.ktlint main = "com.pinterest.ktlint.Main" args "-F", "src/**/*.kt" } argsに指定しているパスはプロジェクトの環境に合わせる。
ANDROID
2022-08-21 2023-03-31
eye catch
【HUGO】シンタックスハイライトのスタイルを変更する
デフォルトのハイライトが視認性が悪い hugoで作られた当サイトのソースコードハイライトでコントラスト比の悪いところがあり、 見づらい箇所がところどころあるのでスタイルを変更したい。
HUGO
2022-08-21 2022-08-21
eye catch
【Java】Files.list()の戻り値Stream<Path>はclose()が必要
java.nio.Files.list(Path)でリストアップしたらクローズしないといけない java.nio.Files.list(Path)で指定したディレクトリ直下のファイルのパス一覧をStream<Path>型で取得できる Stream<Path> paths = Files.list(Paths.get("/path/to/directory")); このメソッドの戻り値をcloseしないとToo many open filesでエラーが発生するようになる。 java.nio.Files.list(Path)の戻り値をcloseする Stream<T>インタフェースの親インタフェースBaseStream<T>は、さらに親がAutoClosableインタフェースなので try-with-resoucesで確実に閉じることができる。
JAVA
2022-08-21 2022-08-21
eye catch
【Android,Kotlin】SoundPoolクラスを利用して効果音を鳴らす
SoundPoolクラスを利用する 本記事ではSoundPoolクラスを利用して効果音を鳴らす。 SoundPoolクラスはあらかじめ音声ファイルをデコードしてメモリに持っておく仕組みになっていてレイテンシが小さい。 速いフィードバックが求められる効果音の再生に向いている。
ANDROID
2022-08-20 2023-03-31
eye catch
【Android,Kotlin】Null許容型(?)をunwrapするパターン
Null許容型をunwrapしたい Null許容型をnon-nullableな型のメソッドの引数にわたすとき困ることがある。 fun f(foo: Foo) { … } var foo: Foo? = … fun g() { f(foo) // コンパイルエラー } メソッドfの引数はNull許容型ではないので、Foo?型の変数を引数に書くとコンパイルエラーとなる。
ANDROID
2022-08-19 2023-03-31
eye catch
【Android,Kotlin】ConstraintLayoutでビューがどうしても重複するときに確認すること
0dp = 制約に合致 = 制約を満たす最大サイズ Androidで調べ物をしていてよく0dpという指定を見かけることがある。 制約で高さや幅を決めたいときに指定するんだろうな、というイメージはあったが、 ちゃんと調べると公式できちんと説明が書かれていた。
ANDROID
2022-08-19 2023-03-31
eye catch
【Android,Kotlin】'VIBRATOR_SERVICE: String' is deprecated. Deprecated in Java への対処
‘VIBRATOR_SERVICE: String’ is deprecated. Deprecated in Java 以下の様なコードを書いているとdeprecated warningが出る。 val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator メッセージは 'VIBRATOR_SERVICE: String' is deprecated. Deprecated in Java で、Context.VIBRATOR_SERVICEはdeprecatedになったので置き換えてくれという内容。
ANDROID
2022-08-19 2023-03-31
  • ««
  • «
  • 22
  • 23
  • 24
  • 25
  • 26
  • »
  • »»
AUTHOR
author image
suer
ふつうのプログラマ
LATESTS
eye catch
【VSCode】vscodevimがCopilot Next Edit Suggestions(NES)のキー入力を邪魔する問題を解決する
VSCODE
2025-02-23 2025-02-23
eye catch
【Docker】docker-composeでホスト名を指定して立ち上げる
LINUX
2025-02-02 2025-02-02
eye catch
【Terraform】removedブロックでリソースを破棄せずにtfstateからのみ削除する
TERRAFORM
2025-01-20 2025-01-20
eye catch
【Terraform】import ブロックで既存リソースを取り込む
TERRAFORM
2025-01-18 2025-01-19
eye catch
【Terraform】terraform_remote_stateでtfstateを分割する
TERRAFORM
2025-01-13 2025-01-13
  • ホーム
  • お問い合わせ
  • プライバシーポリシー

© 2022 suer