Skip to content

Instantly share code, notes, and snippets.

View glureau's full-sized avatar
🏡

Grégory Lureau glureau

🏡
View GitHub Profile
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
class MediumActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_medium)
tv3.text = "FOR <picto> DEVS"
val drawable = ContextCompat.getDrawable(this, R.drawable.medium_logo_lowres)!!.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}
/**********************************************************************************************************************
* Remapping auto-size functionalities from AppCompatTextViewAutoSizeHelper.
**********************************************************************************************************************/
fun AppCompatTextView.autoSizeTextAvailableSizes(): IntArray =
invokeAndReturnWithDefault(this, "getAutoSizeTextAvailableSizes", IntArray(0))
/**********************************************************************************************************************
* Copied from AppCompatTextViewAutoSizeHelper but adapted to AppCompatTextView.
private boolean suggestedSizeFitsInSpace(int suggestedSizeInPx, RectF availableSpace) {
CharSequence text = mTextView.getText();
TransformationMethod transformationMethod = mTextView.getTransformationMethod();
if (transformationMethod != null) {
CharSequence transformedText = transformationMethod.getTransformation(text, mTextView);
if (transformedText != null) {
text = transformedText;
}
}
interface DrawableHeightComputeMode {
fun computeHeight(textPaint: TextPaint, text: CharSequence): Int
}
fun TextView.alignImageToText(textPaint: TextPaint, drawableHeightComputer: DrawableHeightComputeMode) {
(text as? SpannedString)?.getSpans(0, text.length, DynamicDrawableSpan::class.java)?.forEach {
val drawable = it.drawable
val ratio = drawable.intrinsicWidth.toFloat() / drawable.intrinsicHeight
val fontHeight = drawableHeightComputer.computeHeight(textPaint, text)
val drawableWidth = truncate(fontHeight * ratio).toInt()
/**
* Method to adjust (auto-size) the text size AND the images height.
* This approach is based on width and lines instead of height, so you can use layout_heigt="wrap_content"
* and the TextView height will adapt to the best font size (instead of hardcoded heights).
* Idea of improvements: use layout_height value if hardcoded, and use current behaviour for match_parent or wrap_content.
*
* /!\ WARNING /!\
* You can define the min/max/granularity of autoSize with the standard appCompat definitions BUT you have to define the type to uniform.
* app:autoSizeTextType="uniform"
* app:autoSizeMaxTextSize="500sp"
/**********************************************************************************************************************
* Storage of AutoSizeConfiguration
*
* 1 - We cannot setTextSize without disabling autosize (or use Reflection but way more fragile...)
* 2 - Disabling autosize reset all values and attrs is not re-parsed so data is lost.
* Due to these 2 reasons, we need a way to keep track of the previous values to restore them when required.
*
* The solution is to store in a static map a weak reference of the view and an AutoSizeConfiguration data class.
* So if there is no data available (first usage or first view destroyed and recreated),
* we store the attributes in our data class, and if there is already a data class, we re-use them.
package glureau.frameradar
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import java.util.*
@glureau
glureau / ThemeColorPreview.kt
Last active September 13, 2021 10:41
Display
package com.glureau
import android.content.res.Configuration.UI_MODE_NIGHT_NO
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.lazy.GridCells
import androidx.compose.foundation.lazy.LazyVerticalGrid
import androidx.compose.material.MaterialTheme