Created
September 13, 2016 11:13
-
-
Save KalpeshTalkar/b4a7ca55f12845746931afaed2bb5ef2 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
import android.app.Activity; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.Canvas; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.text.Spannable; | |
import android.text.SpannableStringBuilder; | |
import android.text.style.ImageSpan; | |
import android.util.AttributeSet; | |
import android.view.LayoutInflater; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
/** | |
* Created by Kalpesh Talkar on 20/05/16. | |
*/ | |
public class KBubblesView extends EditText { | |
private Context context; | |
// Constructors | |
public KBubblesView(Context context) { | |
super(context); | |
init(context); | |
} | |
public KBubblesView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context); | |
} | |
public KBubblesView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(context); | |
} | |
// Init method | |
void init(Context context) { | |
// Initialization codegoes here | |
this.context = context; | |
} | |
// Set bubbles | |
public void setBubbles(ArrayList<TimeOfOperation> bubbles) { | |
if (bubbles== null|| bubbles.size() == 0) { | |
TimeOfOperation noData = new TimeOfOperation(); | |
noData.setStartTime("No Data Available"); | |
bubbles = new ArrayList<>(); | |
bubbles.add(noData); | |
setBubbles(bubbles); | |
} | |
if (bubbles.size() > 0) { | |
// Get coma separated string from the array | |
StringBuffer sbf = new StringBuffer(); | |
for (int i = 0; i < bubbles.size(); i++) { | |
String data = bubbles.get(i).getStartTime()+" to "+bubbles.get(i).getEndTime(); | |
sbf.append(data); | |
//sbf.append(bubbles.get(i)); | |
// Append separator | |
sbf.append("_"); | |
} | |
// Set text | |
setText(sbf.toString()); | |
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getText()); | |
int x = 0; | |
// Loop will generate ImageSpan for every country name separated by comma | |
for (String bubble : bubbles) { | |
// inflate bubbles layout | |
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); | |
TextView bubbleLabel = (TextView) layoutInflater.inflate(R.layout.bubble_layout, null); | |
// Set text | |
bubbleLabel.setText(bubble); | |
bubbleLabel.setBackgroundColor(Color.parseColor("#808080")); | |
// capture bitmapt of genreated textview | |
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); | |
bubbleLabel.measure(spec, spec); | |
bubbleLabel.layout(0, 0, bubbleLabel.getMeasuredWidth(), bubbleLabel.getMeasuredHeight()); | |
Bitmap bitmap = Bitmap.createBitmap(bubbleLabel.getWidth(), bubbleLabel.getHeight(), Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(bitmap); | |
canvas.translate(-bubbleLabel.getScrollX(), -bubbleLabel.getScrollY()); | |
bubbleLabel.draw(canvas); | |
bubbleLabel.setDrawingCacheEnabled(true); | |
Bitmap cacheBmp = bubbleLabel.getDrawingCache(); | |
Bitmap viewBitmap = cacheBmp.copy(Bitmap.Config.ARGB_8888, true); | |
// Destroy drawable | |
bubbleLabel.destroyDrawingCache(); | |
// Create bitmap drawable for imagespan | |
BitmapDrawable bmpDrawable = new BitmapDrawable(context.getResources(), viewBitmap); | |
bmpDrawable.setBounds(0, 0, bmpDrawable.getIntrinsicWidth(), bmpDrawable.getIntrinsicHeight()); | |
// create and set imagespan | |
spannableStringBuilder.setSpan(new ImageSpan(bmpDrawable), x, x + c.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); | |
x = x + c.length() + 1; | |
} | |
// set bubbles span | |
setText(spannableStringBuilder); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment