Skip to content

Commit

Permalink
Add image span create function for image resource; Fix: #8
Browse files Browse the repository at this point in the history
  • Loading branch information
neworld committed Mar 28, 2018
1 parent aa53583 commit c3d8e8c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 0.5.0

- Add image from resource id support

### 0.4.0

- Change version naming by removing prefix `v`
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![](https://jitpack.io/v/neworld/spanner.svg)](https://jitpack.io/#neworld/spanner)
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Spanner-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/6271)
[![Build Status](https://travis-ci.org/neworld/spanner.svg?branch=master)](https://travis-ci.org/neworld/spanner)
[![Javadoc](https://img.shields.io/badge/javadoc-0.4.0-brightgreen.svg)](https://jitpack.io/com/github/neworld/spanner/0.4.0/javadoc/)
[![Javadoc](https://img.shields.io/badge/javadoc-0.5.0-brightgreen.svg)](https://jitpack.io/com/github/neworld/spanner/0.5.0/javadoc/)

This lib provides simple and fluent API for creating [Android Spannable](https://developer.android.com/reference/android/text/Spannable.html).
Features:
Expand Down Expand Up @@ -31,7 +31,7 @@ Spannable spannable = new Spanner()
.append("foreground\n", foreground(Color.RED))
.append("subscript\n", subscript())
.append("superscript\n", superscript())
.append(image(getResources().getDrawable(R.drawable.ic_android_16dp))).append("\n")
.append(image(context, R.drawable.ic_android_16dp)).append("\n")
.append("quite\n", quote())
.append("The quick brown fox jumps over the lazy dog\n", bold(), foreground(0xFF904f1c), Spans.quote())
.append("Custom\n", custom(new CustomSpan()))
Expand Down Expand Up @@ -111,7 +111,7 @@ Text manipulation:
}
dependencies {
compile 'lt.neworld:spanner:0.4.0'
compile 'lt.neworld:spanner:0.5.0'
}
```

Expand Down
28 changes: 28 additions & 0 deletions lib/src/main/java/lt/neworld/spanner/Spans.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.EmbossMaskFilter;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.LocaleList;
import android.support.annotation.ColorInt;
import android.support.annotation.Dimension;
import android.support.annotation.DrawableRes;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -185,6 +187,7 @@ public Object build() {
}

/**
* The drawable must already have bounds ({@link Drawable#setBounds(Rect)})
* @see android.text.style.ImageSpan#ImageSpan(Drawable)
*/
public static lt.neworld.spanner.ImageSpan image(@NonNull final Drawable drawable) {
Expand All @@ -197,6 +200,7 @@ public Object build() {
}

/**
* The drawable must already have bounds ({@link Drawable#setBounds(Rect)})
* @see android.text.style.ImageSpan#ImageSpan(Drawable, int)
*/
public static lt.neworld.spanner.ImageSpan image(@NonNull final Drawable drawable, final int verticalAlignment) {
Expand All @@ -208,6 +212,30 @@ public Object build() {
});
}

/**
* @see android.text.style.ImageSpan#ImageSpan(Context, int, int)
*/
public static lt.neworld.spanner.ImageSpan image(final Context context, @DrawableRes final int drawableId, final int verticalAlignment) {
return new lt.neworld.spanner.ImageSpan(new SpanBuilder() {
@Override
public Object build() {
return new ImageSpan(context, drawableId, verticalAlignment);
}
});
}

/**
* @see android.text.style.ImageSpan#ImageSpan(Context, int)
*/
public static lt.neworld.spanner.ImageSpan image(final Context context, @DrawableRes final int drawableId) {
return new lt.neworld.spanner.ImageSpan(new SpanBuilder() {
@Override
public Object build() {
return new ImageSpan(context, drawableId);
}
});
}

/**
* @see QuoteSpan#QuoteSpan()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void onClick(View view) {
}
};
Drawable drawable = getResources().getDrawable(R.drawable.ic_android_16dp);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.setBounds(0, 0, drawable.getIntrinsicWidth() * 2, drawable.getIntrinsicHeight() * 2);

// @formatter:off
Spannable spannable = new Spanner()
Expand All @@ -60,7 +60,8 @@ public void onClick(View view) {
.append("foreground\n", foreground(Color.RED))
.append("subscript\n", subscript())
.append("superscript\n", superscript())
.append(image(drawable)).append("\n")
.append("Image with custom bounds: ").append(image(drawable)).append("\n")
.append("Image from resources: ").append(image(this, R.drawable.ic_android_16dp)).append("\n")
.append("quite\n", quote())
.append("The quick brown fox jumps over the lazy dog\n", bold(), foreground(0xFF904f1c), Spans.quote())
.span("fox", foreground(Color.RED))
Expand Down

0 comments on commit c3d8e8c

Please sign in to comment.