-
-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from noties/v4.3.1
V4.3.1
- Loading branch information
Showing
30 changed files
with
1,032 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/src/debug/java/io/noties/markwon/debug/ColorBlendView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.noties.markwon.debug; | ||
|
||
import android.content.Context; | ||
import android.content.res.TypedArray; | ||
import android.graphics.Canvas; | ||
import android.graphics.Paint; | ||
import android.graphics.Rect; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import io.noties.markwon.app.R; | ||
import io.noties.markwon.utils.ColorUtils; | ||
|
||
public class ColorBlendView extends View { | ||
|
||
private final Rect rect = new Rect(); | ||
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); | ||
|
||
private int background; | ||
private int foreground; | ||
|
||
public ColorBlendView(Context context, @Nullable AttributeSet attrs) { | ||
super(context, attrs); | ||
|
||
if (attrs != null) { | ||
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ColorBlendView); | ||
try { | ||
background = array.getColor(R.styleable.ColorBlendView_cbv_background, 0); | ||
foreground = array.getColor(R.styleable.ColorBlendView_cbv_foreground, 0); | ||
} finally { | ||
array.recycle(); | ||
} | ||
} | ||
|
||
paint.setStyle(Paint.Style.FILL); | ||
|
||
setWillNotDraw(false); | ||
} | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
super.onDraw(canvas); | ||
|
||
final int side = getWidth() / 11; | ||
|
||
rect.set(0, 0, side, getHeight()); | ||
|
||
canvas.translate(getPaddingLeft(), 0F); | ||
|
||
for (int i = 0; i < 11; i++) { | ||
final float alpha = i / 10F; | ||
paint.setColor(ColorUtils.blend(foreground, background, alpha)); | ||
canvas.drawRect(rect, paint); | ||
canvas.translate(side, 0F); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout 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" | ||
android:background="#f00"> | ||
|
||
<io.noties.markwon.debug.ColorBlendView | ||
android:layout_width="match_parent" | ||
android:layout_height="64dip" | ||
app:cbv_background="#fff" | ||
app:cbv_foreground="#f0f"/> | ||
|
||
<io.noties.markwon.debug.ColorBlendView | ||
android:layout_width="match_parent" | ||
android:layout_height="64dip" | ||
app:cbv_background="#000" | ||
app:cbv_foreground="#f0f"/> | ||
|
||
<io.noties.markwon.debug.ColorBlendView | ||
android:layout_width="match_parent" | ||
android:layout_height="64dip" | ||
app:cbv_background="#fff" | ||
app:cbv_foreground="#00f"/> | ||
|
||
<io.noties.markwon.debug.ColorBlendView | ||
android:layout_width="match_parent" | ||
android:layout_height="64dip" | ||
app:cbv_background="#000" | ||
app:cbv_foreground="#00f"/> | ||
|
||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
markwon-core/src/main/java/io/noties/markwon/PrecomputedFutureTextSetterCompat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.noties.markwon; | ||
|
||
import android.text.Spanned; | ||
import android.widget.TextView; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.appcompat.widget.AppCompatTextView; | ||
import androidx.core.text.PrecomputedTextCompat; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Future; | ||
|
||
/** | ||
* Please note this class requires `androidx.core:core` artifact being explicitly added to your dependencies. | ||
* This is intended to be used in a RecyclerView. | ||
* | ||
* @see io.noties.markwon.Markwon.TextSetter | ||
* @since 4.3.1 | ||
*/ | ||
public class PrecomputedFutureTextSetterCompat implements Markwon.TextSetter { | ||
|
||
/** | ||
* @param executor for background execution of text pre-computation, | ||
* if not provided the standard, single threaded one will be used. | ||
*/ | ||
@NonNull | ||
public static PrecomputedFutureTextSetterCompat create(@Nullable Executor executor) { | ||
return new PrecomputedFutureTextSetterCompat(executor); | ||
} | ||
|
||
@NonNull | ||
public static PrecomputedFutureTextSetterCompat create() { | ||
return new PrecomputedFutureTextSetterCompat(null); | ||
} | ||
|
||
@Nullable | ||
private final Executor executor; | ||
|
||
@SuppressWarnings("WeakerAccess") | ||
PrecomputedFutureTextSetterCompat(@Nullable Executor executor) { | ||
this.executor = executor; | ||
} | ||
|
||
@Override | ||
public void setText( | ||
@NonNull TextView textView, | ||
@NonNull Spanned markdown, | ||
@NonNull TextView.BufferType bufferType, | ||
@NonNull Runnable onComplete) { | ||
if (textView instanceof AppCompatTextView) { | ||
final AppCompatTextView appCompatTextView = (AppCompatTextView) textView; | ||
final Future<PrecomputedTextCompat> future = PrecomputedTextCompat.getTextFuture( | ||
markdown, | ||
appCompatTextView.getTextMetricsParamsCompat(), | ||
executor); | ||
appCompatTextView.setTextFuture(future); | ||
// `setTextFuture` is actually a synchronous call, so we should call onComplete now | ||
onComplete.run(); | ||
} else { | ||
throw new IllegalStateException("TextView provided is not an instance of AppCompatTextView, " + | ||
"cannot call setTextFuture(), textView: " + textView); | ||
} | ||
} | ||
} |
24 changes: 23 additions & 1 deletion
24
markwon-core/src/main/java/io/noties/markwon/utils/ColorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,33 @@ | ||
package io.noties.markwon.utils; | ||
|
||
import android.graphics.Color; | ||
|
||
import androidx.annotation.ColorInt; | ||
import androidx.annotation.FloatRange; | ||
import androidx.annotation.IntRange; | ||
|
||
public abstract class ColorUtils { | ||
|
||
public static int applyAlpha(int color, int alpha) { | ||
@ColorInt | ||
public static int applyAlpha( | ||
@ColorInt int color, | ||
@IntRange(from = 0, to = 255) int alpha) { | ||
return (color & 0x00FFFFFF) | (alpha << 24); | ||
} | ||
|
||
// blend two colors w/ specified ratio, resulting color won't have alpha channel | ||
@ColorInt | ||
public static int blend( | ||
@ColorInt int foreground, | ||
@ColorInt int background, | ||
@FloatRange(from = 0.0F, to = 1.0F) float ratio) { | ||
return Color.rgb( | ||
(int) (((1F - ratio) * Color.red(foreground)) + (ratio * Color.red(background))), | ||
(int) (((1F - ratio) * Color.green(foreground)) + (ratio * Color.green(background))), | ||
(int) (((1F - ratio) * Color.blue(foreground)) + (ratio * Color.blue(background))) | ||
); | ||
} | ||
|
||
private ColorUtils() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.