Scale images when scaling text

This commit is contained in:
M66B 2020-09-05 21:06:39 +02:00
parent d9473c2e7b
commit 4664eadb72
2 changed files with 56 additions and 4 deletions

View File

@ -475,6 +475,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private TwoStateOwner powner = new TwoStateOwner(owner, "MessagePopup"); private TwoStateOwner powner = new TwoStateOwner(owner, "MessagePopup");
private ScaleGestureDetector gestureDetector; private ScaleGestureDetector gestureDetector;
private Map<Drawable, Pair<Integer, Integer>> drawableSize = new HashMap<>();
private SimpleTask taskContactInfo; private SimpleTask taskContactInfo;
@ -757,31 +758,52 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
btnCalendarMaybe.setOnLongClickListener(this); btnCalendarMaybe.setOnLongClickListener(this);
gestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener() { gestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
private float scale = 1.0f;
private Toast toast = null; private Toast toast = null;
@Override @Override
public boolean onScale(ScaleGestureDetector detector) { public boolean onScale(ScaleGestureDetector detector) {
TupleMessageEx message = getMessage(); TupleMessageEx message = getMessage();
if (message != null) { if (message != null) {
// Scale factor
float factor = detector.getScaleFactor(); float factor = detector.getScaleFactor();
float size = tvBody.getTextSize() * factor; float size = tvBody.getTextSize() * factor;
float scale = (textSize == 0 ? 1.0f : size / (textSize * message_zoom / 100f));
// Text size
properties.setSize(message.id, size); properties.setSize(message.id, size);
tvBody.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); tvBody.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
scale = scale * factor; // Image size
String perc = Math.round(scale * message_zoom) + " %"; Spanned spanned = (Spanned) tvBody.getText();
int bw = tvBody.getWidth() - tvBody.getPaddingStart() - tvBody.getPaddingEnd();
if (bw != 0)
for (ImageSpan img : spanned.getSpans(0, spanned.length(), ImageSpan.class)) {
Drawable d = img.getDrawable();
Pair<Integer, Integer> p = drawableSize.get(d);
if (p == null || p.first == 0)
continue;
float s = Math.min(bw / (float) p.first, scale);
properties.setScale(message.id, s);
int w = Math.round(p.first * s);
int h = Math.round(p.second * s);
d.setBounds(0, 0, w, h);
}
// Feedback
String perc = Math.round(scale) + " %";
if (toast != null) if (toast != null)
toast.cancel(); toast.cancel();
toast = ToastEx.makeText(context, perc, Toast.LENGTH_SHORT); toast = ToastEx.makeText(context, perc, Toast.LENGTH_SHORT);
toast.show(); toast.show();
} }
return true; return true;
} }
}); });
} }
if (accessibility) { if (accessibility) {
view.setAccessibilityDelegate(accessibilityDelegateHeader); view.setAccessibilityDelegate(accessibilityDelegateHeader);
header.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO); header.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
@ -1948,6 +1970,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
args.putBoolean("show_quotes", show_quotes); args.putBoolean("show_quotes", show_quotes);
args.putInt("zoom", zoom); args.putInt("zoom", zoom);
args.putFloat("scale", properties.getScale(message.id, 1.0f));
new SimpleTask<Object>() { new SimpleTask<Object>() {
@Override @Override
protected void onPreExecute(Bundle args) { protected void onPreExecute(Bundle args) {
@ -1966,6 +1990,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
final boolean show_images = args.getBoolean("show_images"); final boolean show_images = args.getBoolean("show_images");
final boolean show_quotes = args.getBoolean("show_quotes"); final boolean show_quotes = args.getBoolean("show_quotes");
final int zoom = args.getInt("zoom"); final int zoom = args.getInt("zoom");
final float scale = args.getFloat("scale");
if (message == null || !message.content) if (message == null || !message.content)
return null; return null;
@ -2120,10 +2145,17 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
// Draw images // Draw images
Map<Drawable, Pair<Integer, Integer>> map = new HashMap<>();
SpannableStringBuilder ssb = HtmlHelper.fromDocument(context, document, true, new Html.ImageGetter() { SpannableStringBuilder ssb = HtmlHelper.fromDocument(context, document, true, new Html.ImageGetter() {
@Override @Override
public Drawable getDrawable(String source) { public Drawable getDrawable(String source) {
Drawable drawable = ImageHelper.decodeImage(context, message.id, source, show_images, zoom, tvBody); Drawable drawable = ImageHelper.decodeImage(context, message.id, source, show_images, zoom, tvBody);
Rect bounds = drawable.getBounds();
map.put(drawable, new Pair<>(bounds.right, bounds.bottom));
bounds.right = Math.round(bounds.right * scale);
bounds.bottom = Math.round(bounds.bottom * scale);
drawable.setBounds(bounds);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (drawable instanceof AnimatedImageDrawable) if (drawable instanceof AnimatedImageDrawable)
@ -2133,6 +2165,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
return drawable; return drawable;
} }
}, null); }, null);
drawableSize = map;
if (show_quotes) if (show_quotes)
return ssb; return ssb;
@ -5773,6 +5806,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
void setExpanded(TupleMessageEx message, boolean expanded); void setExpanded(TupleMessageEx message, boolean expanded);
void setScale(long id, Float size);
float getScale(long id, float defaultSize);
void setSize(long id, Float size); void setSize(long id, Float size);
float getSize(long id, float defaultSize); float getSize(long id, float defaultSize);

View File

@ -309,6 +309,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
final private Map<String, String> kv = new HashMap<>(); final private Map<String, String> kv = new HashMap<>();
final private Map<String, List<Long>> values = new HashMap<>(); final private Map<String, List<Long>> values = new HashMap<>();
final private LongSparseArray<Float> scales = new LongSparseArray<>();
final private LongSparseArray<Float> sizes = new LongSparseArray<>(); final private LongSparseArray<Float> sizes = new LongSparseArray<>();
final private LongSparseArray<Integer> heights = new LongSparseArray<>(); final private LongSparseArray<Integer> heights = new LongSparseArray<>();
final private LongSparseArray<Pair<Integer, Integer>> positions = new LongSparseArray<>(); final private LongSparseArray<Pair<Integer, Integer>> positions = new LongSparseArray<>();
@ -1645,6 +1646,19 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
handleExpand(message.id); handleExpand(message.id);
} }
@Override
public void setScale(long id, Float size) {
if (size == null)
scales.remove(id);
else
scales.put(id, size);
}
@Override
public float getScale(long id, float defaultSize) {
return scales.get(id, defaultSize);
}
@Override @Override
public void setSize(long id, Float size) { public void setSize(long id, Float size) {
if (size == null) if (size == null)
@ -3513,6 +3527,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
Log.i("Hidden id=" + id); Log.i("Hidden id=" + id);
for (String key : values.keySet()) for (String key : values.keySet())
values.get(key).remove(id); values.get(key).remove(id);
scales.remove(id);
sizes.remove(id); sizes.remove(id);
heights.remove(id); heights.remove(id);
positions.remove(id); positions.remove(id);