20210606のAndroidに関する記事は5件です。

Androidアプリのパフォーマンス 改善②(RecyclerView.Adapter)

この記事では、Androidアプリのパフォーマンス改善のための、方法について記載しています。 改善対象 リストなどを表示する場合、RecyclerViewを使うことがあると思います。 RecyclerViewはとても便利なクラスですが、パフォーマンスには注意が必要です。 計測 下記のRecyclerViewを使ったカレンダーのパフォーマンスをSystraceを使って計測しました。 すると、AdapterのonCreateViewHolderのinflateに時間がかかっている(下記の場合は、15.506ms)ことがわかりました。 onCreateViewHolderはRecyclerViewのitemの数だけ呼ばれるので、onCreateViewHolderに時間がかかるとパフォーマンスにかなり影響があります。 改善方法 改善には、RecyclerViewのitem生成にinflateを使用せず、Viewを生成(CustomView)する方法があります。 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DateAdapterHolder { - val view = inflater.inflate(R.layout.calendar_item, parent, false) - return DateAdapterHolder(view) + return DateAdapterHolder( + CalenderItemView(parent.context).apply { + layoutParams = LinearLayout.LayoutParams(parent.width / 7, ITEM_HEIGHT.toInt()) + } + ) } CalenderItemView.kt class CalenderItemView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyle: Int = 0 ) : LinearLayout(context, attrs, defStyle) { companion object { private val TEXT_SIZE = 20f.sp private val MARGIN_SIZE = 5f.sp val ITEM_HEIGHT = TEXT_SIZE + MARGIN_SIZE * 2 private val textPaint = Paint().apply { textSize = TEXT_SIZE } } var date: String = "" var color: Int = Color.BLACK init { gravity = Gravity.RIGHT setBackgroundColor(Color.WHITE) } override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) textPaint.color = color canvas?.drawText( date, if (date.length == 1) TEXT_SIZE / 2 else 0f.sp, TEXT_SIZE + MARGIN_SIZE, textPaint ) } } この修正により、onCreateViewHolderの時間(Systraceで計測)は1/10(平均6.638msから0.735ms)に改善されました。 実装の詳細は下記を参照してください。 https://github.com/yoshihiro-kato/android-samples/tree/4e8ca216a71bf4f147ff854e4a8872b889639ea3/KotlinSampleApplication 補足 CustomViewにするとCanvasにテキストやBitmapを直接描画することになるので、TextViewのellipsize等が使えなくなります。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

[AndroidStudio]The color "hogehoge" in values has no declaration in the base values folder; this can lead to crashes when the resource is queried in a configuration that does not match this qualifier

リソースファイルにて 色やレイアウトを定義してもタイトルのような文と共に赤字になる時解決策 黙って再展開 File->CloseProject
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

SendGridでメール送信を試してみた

SendGridを使用した際の備忘録として残しております。 参考 KotlinからSendGridを利用してメール送信する | SendGridブログ fuel-coroutines Android開発でAPIキーを隠蔽してGitHubに上げる - Qiita 特殊文字(特殊記号)の名称とHTMLへの記述の仕方 | ぱそめも 注意点 自分だけかもしれませんが、$argsがhtmlでなければ送信できないことを忘れてしまい、文字列として送信しようとして400番エラーになったことがありました。 実際に試したコード build.gradle dependencies { implementation "com.github.kittinunf.fuel:fuel:2.3.1" implementation "com.github.kittinunf.fuel:fuel-gson:1.9.0" implementation 'com.github.kittinunf.result:result:3.1.0' implementation 'com.github.kittinunf.fuel:fuel-coroutines:2.3.1' implementation 'com.github.kittinunf.fuel:fuel-android:2.2.0' } MainActivity.kt package com.example.sendgridsample001 import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.view.View import android.widget.Button import com.github.kittinunf.fuel.core.FuelManager import com.github.kittinunf.fuel.coroutines.awaitStringResponseResult import com.github.kittinunf.fuel.httpPost import com.github.kittinunf.result.Result import kotlinx.coroutines.runBlocking class MainActivity : AppCompatActivity() { private var flag = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val body = StringBuilder() body.append("Kotlinテスト") main(body) if(flag) { val button = findViewById<Button>(R.id.button) button.setOnClickListener (object : View.OnClickListener{ override fun onClick(v: View?) { val intent = Intent(this@MainActivity, SubActivity::class.java) startActivity(intent) } }) }else{ Log.d("[ERROR]", "送信に失敗しました。") } } fun main(args : StringBuilder) { val to = "xxxxxx@gmail.com" val from = "xxxxxx.x.xxxxxx@gmail.com" var fromName = "SenderKotlin" var content = """ { "personalizations": [ { "to": [ { "email": "$to" } ] } ], "from": { "email": "$from", "name": "$fromName" }, "subject": "テスト", "content": [ { "type": "text/plain", "value": "Kotlinから送った" }, { "type": "text/html", "value": "<html><body>&nbsp;$args&nbsp;</body></html>" } ] } """ runBlocking { FuelManager.instance.baseHeaders = mapOf("Content-Type" to "application/json", "Authorization" to "Bearer ${BuildConfig.API_KEY}") val (req, res, result) = "https://api.sendgrid.com/v3/mail/send".httpPost().body(content).awaitStringResponseResult() when(result) { is Result.Failure -> { val ex = result.getException() println(ex) flag = false } is Result.Success -> { val data = result.get() println(data) when(res.statusCode) { 202 -> flag = true 400, 401, 413 -> flag = false else -> flag = false } } } println(req) println(res) println(result) } } } activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SendGrid テスト!" android:id="@+id/textView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:layout_width="65dp" android:layout_height="40dp" android:text="次へ" android:id="@+id/button" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> </androidx.constraintlayout.widget.ConstraintLayout> activity_sub.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SubActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="送信完了!" android:id="@+id/textView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> SubActivity.kt package com.example.sendgridsample001 import androidx.appcompat.app.AppCompatActivity import android.os.Bundle class SubActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_sub) } }
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

DeployGate で配信後、Google認証しようとすると「ApiException:10」になる時の対処法

DeployGate で配信後、Google認証しようとすると「ApiException:10」になる時の対処法 参考 Google認証で ApiException:10 が発生 - Qiita やったこと release側のフィンガープリントが足らないため出たエラーでした。 リリースビルドを実行しました。 releaseバリアントに変更 build.gradle android{ lintOptions { checkReleaseBuilds false // Or, if you prefer, you can continue to check for errors in release builds, // but continue the build even when errors are found: abortOnError false } } 署名付きrelease.apkの作成 こちらを参考にrelease.apkを作成しました。 最後に下記を参考にフィンガープリントを取得しました。 How to get the SHA-1 fingerprint certificate in Android Studio ...
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Caused by: java.lang.VerifyError: Verifier rejected class com.sun.mail.handlers.handler_base: java.awt.datatransfer.DataFlavor[] com.sun.mail.handlers.handler_base.getTransferDataFlavors() failed to verify: java.awt.datatransfer.DataFlavor[]が出た時の対処法

AndroidMail(SunMail)を使うときに以下のエラーが出た時の対処法 Caused by: java.lang.VerifyError: Verifier rejected class com.sun.mail.handlers.handler_base: java.awt.datatransfer.DataFlavor[] com.sun.mail.handlers.handler_base.getTransferDataFlavors() failed to verify: java.awt.datatransfer.DataFlavor[] com.sun.mail.handlers.handler_base.getTransferDataFlavors(): [0x4] can't resolve returned type 'Unresolved Reference: java.awt.datatransfer.DataFlavor[]' or 'Reference: javax.activation.ActivationDataFlavor[]' (declaration of 'com.sun.mail.handlers.handler_base' appears in /data/app/xxx 参考 Android Studio VerifyError rejecting class text_plain from ... 結果 TargetSdkVersionを29にするとエラーは出なくなりました。 build.gradle android { compileSdkVersion 29 defaultConfig { targetSdkVersion 29 } }
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む