Non empty bounds for AsyncDrawable when no dimensions available

This commit is contained in:
Dimitry Ivanov 2020-03-08 12:45:31 +03:00
parent db660d2a40
commit 12c7c8909b
8 changed files with 137 additions and 2 deletions

View File

@ -17,7 +17,9 @@
* add `SoftBreakAddsNewLinePlugin` plugin (`core` module)
* `LinkResolverDef` defaults to `https` when a link does not have scheme information ([#75])
* add `option` abstraction for `sample` module allowing switching of multiple cases in runtime via menu
* non-empty bounds for `AsyncDrawable` when no dimensions are not yet available ([#189])
[#189]: https://github.com/noties/Markwon/issues/189
[#75]: https://github.com/noties/Markwon/issues/75
[#204]: https://github.com/noties/Markwon/issues/204

View File

@ -223,6 +223,7 @@ public class AsyncDrawable extends Drawable {
}
this.result = result;
// this.result.setCallback(callback);
initBounds();
}
@ -250,6 +251,10 @@ public class AsyncDrawable extends Drawable {
if (canvasWidth == 0) {
// we still have no bounds - wait for them
waitingForDimensions = true;
// we cannot have empty bounds - otherwise in case if text contains
// a single AsyncDrawableSpan, it won't be displayed
setBounds(noDimensionsBounds(result));
return;
}
@ -268,6 +273,24 @@ public class AsyncDrawable extends Drawable {
invalidateSelf();
}
/**
* @since 4.3.0-SNAPSHOT
*/
@NonNull
private static Rect noDimensionsBounds(@Nullable Drawable result) {
if (result != null) {
final Rect bounds = result.getBounds();
if (!bounds.isEmpty()) {
return bounds;
}
final Rect intrinsicBounds = DrawableUtils.intrinsicBounds(result);
if (!intrinsicBounds.isEmpty()) {
return intrinsicBounds;
}
}
return new Rect(0, 0, 1, 1);
}
/**
* @since 1.0.1
*/

View File

@ -36,6 +36,7 @@
<activity android:name=".inlineparser.InlineParserActivity" />
<activity android:name=".htmldetails.HtmlDetailsActivity" />
<activity android:name=".tasklist.TaskListActivity" />
<activity android:name=".images.ImagesActivity" />
</application>

View File

@ -25,6 +25,7 @@ import io.noties.markwon.sample.customextension2.CustomExtensionActivity2;
import io.noties.markwon.sample.editor.EditorActivity;
import io.noties.markwon.sample.html.HtmlActivity;
import io.noties.markwon.sample.htmldetails.HtmlDetailsActivity;
import io.noties.markwon.sample.images.ImagesActivity;
import io.noties.markwon.sample.inlineparser.InlineParserActivity;
import io.noties.markwon.sample.latex.LatexActivity;
import io.noties.markwon.sample.precomputed.PrecomputedActivity;
@ -137,6 +138,10 @@ public class MainActivity extends Activity {
activity = TaskListActivity.class;
break;
case IMAGES:
activity = ImagesActivity.class;
break;
default:
throw new IllegalStateException("No Activity is associated with sample-item: " + item);
}

View File

@ -29,7 +29,9 @@ public enum Sample {
HTML_DETAILS(R.string.sample_html_details),
TASK_LIST(R.string.sample_task_list);
TASK_LIST(R.string.sample_task_list),
IMAGES(R.string.sample_images);
private final int textResId;

View File

@ -0,0 +1,85 @@
package io.noties.markwon.sample.images;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.target.Target;
import io.noties.markwon.Markwon;
import io.noties.markwon.image.AsyncDrawable;
import io.noties.markwon.image.glide.GlideImagesPlugin;
import io.noties.markwon.sample.ActivityWithMenuOptions;
import io.noties.markwon.sample.MenuOptions;
import io.noties.markwon.sample.R;
public class ImagesActivity extends ActivityWithMenuOptions {
private TextView textView;
@NonNull
@Override
public MenuOptions menuOptions() {
// todo: same for other plugins
return MenuOptions.create()
.add("glide-singleImage", this::glideSingleImage)
.add("glide-singleImageWithPlaceholder", this::glideSingleImageWithPlaceholder);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
textView = findViewById(R.id.text_view);
glideSingleImageWithPlaceholder();
}
private void glideSingleImage() {
final String md = "[![undefined](https://img.youtube.com/vi/gs1I8_m4AOM/0.jpg)](https://www.youtube.com/watch?v=gs1I8_m4AOM)";
final Markwon markwon = Markwon.builder(this)
.usePlugin(GlideImagesPlugin.create(this))
.build();
markwon.setMarkdown(textView, md);
}
// can be checked when used first, otherwise works as expected...
private void glideSingleImageWithPlaceholder() {
final String md = "[![undefined](https://img.youtube.com/vi/gs1I8_m4AOM/0.jpg)](https://www.youtube.com/watch?v=gs1I8_m4AOM)";
final Context context = this;
final Markwon markwon = Markwon.builder(context)
.usePlugin(GlideImagesPlugin.create(new GlideImagesPlugin.GlideStore() {
@NonNull
@Override
public RequestBuilder<Drawable> load(@NonNull AsyncDrawable drawable) {
// final Drawable placeholder = ContextCompat.getDrawable(context, R.drawable.ic_home_black_36dp);
// placeholder.setBounds(0, 0, 100, 100);
return Glide.with(context)
.load(drawable.getDestination())
// .placeholder(ContextCompat.getDrawable(context, R.drawable.ic_home_black_36dp));
// .placeholder(placeholder);
.placeholder(R.drawable.ic_home_black_36dp);
}
@Override
public void cancel(@NonNull Target<?> target) {
Glide.with(context)
.clear(target);
}
}))
.build();
markwon.setMarkdown(textView, md);
}
}

View File

@ -61,7 +61,8 @@ public class LatexActivity extends ActivityWithMenuOptions {
.add("boxes", this::boxes)
.add("insideBlockQuote", this::insideBlockQuote)
.add("error", this::error)
.add("legacy", this::legacy);
.add("legacy", this::legacy)
.add("textColor", this::textColor);
}
@Override
@ -136,6 +137,20 @@ public class LatexActivity extends ActivityWithMenuOptions {
markwon.setMarkdown(textView, md);
}
private void textColor() {
final String md = wrapLatexInSampleMarkdown(LATEX_LONG_DIVISION);
final Markwon markwon = Markwon.builder(this)
.usePlugin(MarkwonInlineParserPlugin.create())
.usePlugin(JLatexMathPlugin.create(textView.getTextSize(), new JLatexMathPlugin.BuilderConfigure() {
@Override
public void configureBuilder(@NonNull JLatexMathPlugin.Builder builder) {
}
}))
.build();
markwon.setMarkdown(textView, md);
}
@NonNull
private static String wrapLatexInSampleMarkdown(@NonNull String latex) {
return "" +

View File

@ -33,4 +33,6 @@
<string name="sample_task_list"># \# TaskList\n\nUsage of TaskListPlugin</string>
<string name="sample_images"># \# Images\n\nUsage of different images plugins</string>
</resources>