Skip to content

Commit

Permalink
Autosize text inside Element
Browse files Browse the repository at this point in the history
Code used to manually update font size has been replaced by a call to
`setAutoSizeTextTypeUniformWithConfiguration`.
Fixes SecUSo#2, SecUSo#6 and SecUSo#12.
  • Loading branch information
p-davide committed Nov 4, 2022
1 parent b068d50 commit 352e513
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package org.secuso.privacyfriendly2048.activities;

import static android.util.TypedValue.COMPLEX_UNIT_SP;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -49,17 +52,22 @@ public class Element extends android.support.v7.widget.AppCompatButton {
public int dPosY = 0;
public boolean activated;
public boolean animateMoving = false;
public float textSize = 24;
Context context;
int color;


@SuppressLint("RestrictedApi")
public Element(Context c)
{
super(c);
context = c;
setAllCaps(false);
setTextSize(textSize);
// see https://developer.android.com/develop/ui/views/text-and-emoji/autosizing-textview#default
setAutoSizeTextTypeUniformWithConfiguration(
6,
24,
1,
COMPLEX_UNIT_SP);
setBackgroundResource(R.drawable.game_brick);
if(PreferenceManager.getDefaultSharedPreferences(context).getString("pref_color","1").equals("1"))
setColor(context.getResources().getColor(R.color.button_empty));
Expand Down Expand Up @@ -143,14 +151,10 @@ public void drawItem() {
case 16384:
setColor(ContextCompat.getColor(context,R.color.button16384));
setTextColor(ContextCompat.getColor(context,R.color.white));
textSize = textSize * 0.8f;
setTextSize(textSize);
break;
case 32768:
setColor(ContextCompat.getColor(context,R.color.button32768));
setTextColor(ContextCompat.getColor(context,R.color.white));
textSize = textSize * 0.8f;
setTextSize(textSize);
break;
}
}
Expand Down Expand Up @@ -216,14 +220,10 @@ public void drawItem() {
case 16384:
setColor(ContextCompat.getColor(context,R.color.button16384_2));
setTextColor(ContextCompat.getColor(context,R.color.white));
textSize = textSize * 0.8f;
setTextSize(textSize);
break;
case 32768:
setColor(ContextCompat.getColor(context,R.color.button32768_2));
setTextColor(ContextCompat.getColor(context,R.color.white));
textSize = textSize * 0.8f;
setTextSize(textSize);
break;
}
}
Expand Down Expand Up @@ -273,10 +273,7 @@ public int getPosX() {
public int getPosY() {
return posY;
}
public void updateFontSize(){
textSize=(float)(getLayoutParams().width/7.0);
setTextSize(textSize);
}

public Element copy()
{
Element temp = new Element(context);
Expand All @@ -288,9 +285,7 @@ public Element copy()
temp.dPosY = dPosY;
temp.activated = activated;
temp.animateMoving = animateMoving;
temp.textSize = textSize;
temp.color = color;
temp.setTextSize(textSize);
//temp.setBackgroundResource(backGroundResource);
temp.setColor(color);
temp.setVisibility(getVisibility());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ record = gameStatistics.getRecord();
elements[i][j].setDPosition(lp.getMarginStart(),lp.topMargin);
elements[i][j].setLayoutParams(lp);
backgroundElements[i][j].setLayoutParams(lp);
elements[i][j].updateFontSize();
backgroundElements[i][j].setLayoutParams(lp);
backgroundElements[i][j].setOnTouchListener(swipeListener);
elements[i][j].setOnTouchListener(swipeListener);
Expand Down

0 comments on commit 352e513

Please sign in to comment.