BIEN meilleur qualite de texte + patch fps

This commit is contained in:
mmarc471@gmail.com
2020-09-10 20:05:41 +02:00
parent c47e61438d
commit c368f10e9d
7 changed files with 195 additions and 63 deletions
@@ -4,6 +4,7 @@ import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import androidx.annotation.NonNull;
@@ -51,10 +52,6 @@ public class Drawable {
float x_scaled_size = x_size* GameActivity.screenWidth/100;
float y_scaled_size = y_size* GameActivity.screenHeight/100;
System.out.println(x_scaled_size + " y: " + y_scaled_size);
System.out.println(y_size);
if(x_scaled_size <=0 || y_scaled_size <=0)
{
Log.e("TEXTURE :","ERREUR DE MISE A L'ECHELLE");
@@ -76,32 +73,75 @@ public class Drawable {
public Drawable(String text,float x_pos,float y_pos,String name, float x_size,float y_size,float textSize)
{
this(x_pos,y_pos,name);
//this ratio is used to dial the quality of the text;
List<String> linesOfText = cutText(20,text);
Log.i("bitmap"," : h:" + 40*linesOfText.size() + " w: "+text.length()*textSize / linesOfText.size());
bitmap = Bitmap.createBitmap((int)(text.length()*textSize / linesOfText.size()), (int) 40*linesOfText.size(), Bitmap.Config.ARGB_8888);
int x_scaled_size = (int)x_size* GameActivity.screenWidth/100;
int y_scaled_size = (int)y_size* GameActivity.screenHeight/100;
checkPaint();
p.setTextSize(textSize*2);
p.setColor(Color.BLACK);
List<String> linesOfText = cutText(20,text);
p.setTextSize(textSize);
/*float size = 0;
for(String str : linesOfText)
{
float i;
if((i = getTextSizeForWidth(p,x_scaled_size,y_scaled_size,str)) > size)
{
size = i;
}
}
p.setTextSize(size);*/
//+10 permet d'e prendre en compte les p,q,j qui dessende plus bas que les autres
bitmap = Bitmap.createBitmap(x_scaled_size, y_scaled_size+10, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
/*List<String> linesOfText = cutText(20,text);
bitmap = Bitmap.createBitmap((int)(textSize * 9), (int) 40*linesOfText.size(), Bitmap.Config.ARGB_8888);
checkPaint();
p.setTextSize(textSize);
p.setColor(Color.BLACK);
Canvas c = new Canvas(bitmap);
*/
int index = 1;
for(String line : linesOfText)
{
c.drawText(line ,0,textSize*2*index,p);
c.drawText(line ,0,index*y_scaled_size/linesOfText.size(),p);
index++;
}
/*
float x_scaled_size = x_size* GameActivity.screenWidth/100;
float y_scaled_size = y_size* GameActivity.screenHeight/100;
bitmap = Bitmap.createScaledBitmap(bitmap, (int)x_scaled_size, (int)y_scaled_size, GameActivity.bilinearFiltering);
bitmap = Bitmap.createScaledBitmap(bitmap, (int)x_scaled_size, (int)y_scaled_size, GameActivity.bilinearFiltering);*/
}
private String arrangeText(int charPerLines, String text) {
//TODO cut text in lines
StringBuilder nextLine = new StringBuilder(text);
int index = 0;
int size=0;
for (String word : text.split(" ")) {
if(word.length() < charPerLines)
{
if(word.length() + size +1 < charPerLines)
{
index +=word.length();
size += word.length();
}else{
size=0;
nextLine.insert(index++,'\n');
}
}else{
Log.e("TextRender","ERROR WORD TOO LONG :" + word);
}
}
return nextLine.toString();
}
/**
@@ -152,9 +192,10 @@ public class Drawable {
* @param c canevas sur le quel dessiner
* @param p pinceau
*/
public void drawOn(Canvas c, Paint p)
public void drawOn(@NonNull Canvas c,@NonNull Paint p)
{
//drawings
if(c != null)
c.drawBitmap(getBitmap(),x* GameActivity.screenWidth/100,y* GameActivity.screenHeight/100,p);
}
@@ -223,22 +264,61 @@ public class Drawable {
{
List<String> output = new ArrayList<>();
//TODO cut text in lines
String nextLine = "";
StringBuilder nextLine = new StringBuilder(charPerLines);
for (String word : text.split(" ")) {
if(word.length() < charPerLines)
{
if(word.length() + nextLine.length() +1 < charPerLines)
if(word.length() + nextLine.toString().length() +1 < charPerLines)
{
nextLine += " " + word;
nextLine.append(" " + word);
}else{
output.add(nextLine);
nextLine="";
//fillWithSpace(nextLine,charPerLines);
output.add(nextLine.toString());
nextLine.delete(0,charPerLines-1);
}
}else{
Log.e("TextRender","ERROR WORD TOO LONG :" + word);
}
}
if(!nextLine.equals(""))output.add(nextLine);
if(!nextLine.toString().equals(""))
{
//fillWithSpace(nextLine,charPerLines);
output.add(nextLine.toString());
}
return output;
}
private void fillWithSpace(StringBuilder nextLine, int charPerLines) {
for(int i=0 ; i < charPerLines-nextLine.length();i++)
{
nextLine.append(' ');
}
}
/**
* https://stackoverflow.com/questions/12166476/android-canvas-drawtext-set-font-size-from-width
* @param paint
* @param desiredWidth
* @param text
*/
private static float getTextSizeForWidth(Paint paint, float desiredWidth,
String text) {
// Pick a reasonably large value for the test. Larger values produce
// more accurate results, but may cause problems with hardware
// acceleration. But there are workarounds for that, too; refer to
// http://stackoverflow.com/questions/6253528/font-size-too-large-to-fit-in-cache
final float testTextSize = 48f;
// Get the bounds of the text, using our testTextSize.
paint.setTextSize(testTextSize);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
// Calculate the desired size as a proportion of our testTextSize.
float desiredTextWidth = testTextSize * desiredWidth / bounds.width();
// Set the paint for that size.
return desiredTextWidth;
}
}