Last active
November 24, 2017 09:49
-
-
Save TomoyaShibata/86ada604efa2ad56d87bc567ede078f5 to your computer and use it in GitHub Desktop.
Android テンプレ for BPS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// java.util.Date をいい感じにフォーマットして表示するやつ | |
@BindingAdapter("date") | |
fun TextView.setDate(date: Date?): Unit { | |
if (createdAt == null) return | |
// android.text.format.DateFormat を使うよ | |
val formatedDate = DateFormat.format("yyyy.MM.dd kk:mm", date) | |
this.text = formatedDate | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BindingAdapter で画像を便利に表示するやつ with Picasso | |
@BindingAdapter(value = *arrayOf("imageUrl", "isCircle"), requireAll = false) | |
fun ImageView.setImageUrl(url: String?, isCircle: Boolean = false): Unit { | |
if (url.isNullOrBlank()) return | |
if (isCircle) { | |
Picasso.with(this.context).load(url).transform(CircleTransformation()).into(this) | |
return | |
} | |
Picasso.with(this.context).load(url).into(this) | |
} | |
// 参考:https://androidmads.blogspot.jp/2017/06/circle-image-view-using-glide-and.html | |
class CircleTransformation : Transformation { | |
override fun transform(source: Bitmap): Bitmap { | |
val size = Math.min(source.width, source.height) | |
val x = (source.width - size) / 2 | |
val y = (source.height - size) / 2 | |
val squaredBitmap = Bitmap.createBitmap(source, x, y, size, size) | |
if (squaredBitmap != source) { | |
source.recycle() | |
} | |
val bitmap = Bitmap.createBitmap(size, size, source.config) | |
val canvas = Canvas(bitmap) | |
val paint = Paint() | |
val shader = BitmapShader(squaredBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) | |
paint.setShader(shader) | |
paint.setAntiAlias(true) | |
val r = size / 2f | |
canvas.drawCircle(r, r, r, paint) | |
squaredBitmap.recycle() | |
return bitmap | |
} | |
override fun key(): String = "circle" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment