Skip to content

Instantly share code, notes, and snippets.

Greg glureau

  • Betclic
  • Bordeaux, France
Block or report user

Report or block glureau

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View medium_atvasis_providesAutoSizeConfiguration.kt
/**********************************************************************************************************************
* 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.
View medium_atvasis_adjustSizeToFit.kt
/**
* 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"
View medium_atvasis_alignImageToText.kt
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()
View medium_atvasis_suggestedSizeFitsInSpace.java
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;
}
}
View medium_atvasis_reflection
/**********************************************************************************************************************
* Remapping auto-size functionalities from AppCompatTextViewAutoSizeHelper.
**********************************************************************************************************************/
fun AppCompatTextView.autoSizeTextAvailableSizes(): IntArray =
invokeAndReturnWithDefault(this, "getAutoSizeTextAvailableSizes", IntArray(0))
/**********************************************************************************************************************
* Copied from AppCompatTextViewAutoSizeHelper but adapted to AppCompatTextView.
View medium_atvasis_imagespan.kt
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)
}
View medium_atvasis.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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
You can’t perform that action at this time.