Created
February 11, 2017 16:09
-
-
Save suwhs/78d77da059d8cb63dcde34e903ab4d51 to your computer and use it in GitHub Desktop.
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
package su.whs.watl_issue_029; | |
import android.graphics.Point; | |
import android.graphics.Rect; | |
import android.graphics.drawable.Drawable; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.text.Spannable; | |
import android.text.SpannableStringBuilder; | |
import android.text.Spanned; | |
import android.text.style.DynamicDrawableSpan; | |
import android.text.style.ImageSpan; | |
import su.whs.watl.text.ContentView; | |
import su.whs.watl.text.ImagePlacementHandler; | |
import su.whs.watl.ui.TextViewEx; | |
public class MainActivity extends AppCompatActivity { | |
private static final String SAMPLE_STRING = | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
TextViewEx tv = (TextViewEx) findViewById(R.id.textView); | |
ImageSpan span = new ImageSpan(this,R.drawable.letter_l,ImageSpan.ALIGN_BASELINE); //letter_l - pre-rendered capital 'L' | |
SpannableStringBuilder text = new SpannableStringBuilder(SAMPLE_STRING); | |
// mark place for image with UNICODE REPLACEMENT CHARACTER (Html.fromHtml() does it automatically) | |
text.replace(0,1,"\uFFFC"); | |
text.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); | |
tv.getOptions().setDrawablePaddings(0,0,0,0); // reset default images paddings | |
//.setImagePlacementHandler(mImagePlacementHandler); | |
span = new ImageSpan(this,R.drawable.letter_c, ImageSpan.ALIGN_BASELINE); //letter_c - pre-rendered capital 'C' | |
// break text before Initial and mark place for image | |
text.replace(28,29,"\n\uFFFC"); | |
text.setSpan(span,29,30, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); | |
tv.setText(text); | |
} | |
// sample image placement handler | |
private ImagePlacementHandler mImagePlacementHandler = new ImagePlacementHandler() { | |
@Override | |
public int place(DynamicDrawableSpan drawable, int height, int viewHeight, int width, int viewWidth, int offset, Point scale, ContentView.Options options, boolean allowDefer) { | |
Drawable dr = drawable.getDrawable(); | |
Rect paddings = options.getDrawablePaddings(); | |
int sW = paddings.left + paddings.top; | |
int sH = paddings.top + paddings.bottom; | |
// set target size for drawable (for example - reduce height) | |
scale.set(dr.getIntrinsicWidth() + sW, (int) (dr.getIntrinsicHeight() * .8f) + sH); | |
// 0x8 - WRAP_TEXTflag (magic cause i forgot makes it public ) | |
return 0x8 | ImagePlacementHandler.ALIGN_START; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment