Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TextMessage extends SurfaceView implements SurfaceHolder.Callback {
- private SurfaceHolder surfaceHolder;
- private int padding = 32; // For one side
- private int radius = 25;
- private int textsize = 30;
- private int typelinegap = (int)(8 * getContext().getResources().getDisplayMetrics().density);
- private int viewWidth;
- private int viewHeight;
- private int maxTextWidth;
- private int textColor = Color.WHITE;
- private int windowColor;
- private String message;
- private Integer boxColor = Color.BLUE;
- private String platform = "samsung";
- private Paint paint;
- public TextMessage(Context context, AttributeSet attrs) {
- super(context, attrs);
- TypedArray a = context.getTheme().obtainStyledAttributes(
- attrs,
- R.styleable.TextMessage,
- 0, 0);
- try {
- textColor = a.getColor(R.styleable.TextMessage_textColor, Color.WHITE);
- boxColor = a.getColor(R.styleable.TextMessage_msgBoxColor, Integer.MIN_VALUE);
- boxColor = (boxColor.intValue() == Integer.MIN_VALUE) ? null : boxColor;
- textsize = a.getDimensionPixelSize(R.styleable.TextMessage_textSize, 30);
- platform = a.getString(R.styleable.TextMessage_platform).toLowerCase();
- message = a.getString(R.styleable.TextMessage_text);
- message = (message == null) ? "\uD83D\uDE01\uD83D\uDE01\uD83D\uDE01\uD83D\uDE01\uD83D\uDE01" : message;
- init();
- }
- finally {
- a.recycle();
- }
- }
- public TextMessage(Context context, String message, int viewWidth, String platform, int textColor,
- Integer boxColor, int textsize) {
- super(context);
- this.message = message;
- this.viewWidth = viewWidth;
- this.platform = platform;
- this.textColor = textColor;
- this.boxColor = boxColor;
- this.textsize = textsize;
- init();
- }
- private void init() {
- paint = new Paint();
- this.setWillNotDraw(false);
- this.setZOrderMediaOverlay(true);
- surfaceHolder = getHolder();
- surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
- surfaceHolder.addCallback(this);
- TypedArray array = getContext().obtainStyledAttributes(new int[] {
- android.R.attr.colorBackground,
- android.R.attr.textColorPrimary,
- });
- try {
- windowColor = array.getColor(0, 0xFF00FF);
- }
- finally {
- array.recycle();
- }
- }
- @Override
- public void surfaceCreated(SurfaceHolder surfaceHolder) {
- new DrawTextTask().execute(this);
- }
- @Override
- public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
- // TODO
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
- // TODO
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- viewWidth = MeasureSpec.getSize(widthMeasureSpec);
- this.maxTextWidth = viewWidth - (2 * (padding + radius));
- setMeasuredDimension(viewWidth, measureViewHeight());
- }
- // Reduced copied code, used in determining message Height and actually drawing the text
- public int formatMessage(@Nullable Canvas canvas) {
- int line = 1;
- boolean draw = canvas != null;
- paint.setColor(textColor);
- paint.setTextSize(textsize);
- StringBuilder stringBuilder = new StringBuilder("");
- String[] words = message.split(" ");
- Log.d("Emoji", "Words:" + words.length);
- for (int n = 0; n < words.length; n++) {
- if (stringBuilder.length() != 0)
- stringBuilder.append(" ");
- stringBuilder.append(words[n]);
- if (paint.measureText(stringBuilder.toString()) > maxTextWidth) {
- if ((paint.measureText(words[n]) / (double) maxTextWidth) > 0.25) {
- int x = stringBuilder.length() - 1;
- char c = stringBuilder.charAt(x);
- if ((c >= 55296 && c <= 56319) || (c >= 56320 && c <= 57343))
- x--;
- while(paint.measureText(stringBuilder.substring(0, x)) > maxTextWidth) {
- x--;
- char ch = stringBuilder.charAt(x);
- if ((ch >= 55296 && ch <= 56319) || (ch >= 56320 && ch <= 57343))
- x--;
- }
- String halfWord = stringBuilder.substring(x);
- ArrayList<String> breakWords = new ArrayList<>();
- int b = 0;
- for (int a = 1; a <= halfWord.length(); a++) {
- char ch = stringBuilder.charAt(a);
- if ((ch >= 55296 && ch <= 56319) || (ch >= 56320 && ch <= 57343))
- a++;
- String substring = halfWord.substring(b, a);
- if (paint.measureText(substring) > maxTextWidth) {
- breakWords.add(substring);
- b = a;
- }
- }
- breakWords.add(halfWord.substring(b));
- if (draw)
- writeText(canvas, stringBuilder.substring(0, x), line);
- line += 1;
- stringBuilder.delete(0, stringBuilder.length());
- for (int a = 0; a < breakWords.size() - 1; a++) {
- if (draw)
- writeText(canvas, breakWords.get(a), line);
- line += 1;
- }
- stringBuilder.append(breakWords.get(breakWords.size() - 1));
- } else {
- stringBuilder.delete(stringBuilder.length() - words[n].length(), stringBuilder.length());
- if (draw)
- writeText(canvas, stringBuilder.toString(), line);
- line += 1;
- n--;
- stringBuilder.delete(0, stringBuilder.length());
- }
- }
- }
- if (draw)
- writeText(canvas, stringBuilder.toString(), line);
- return line;
- }
- private void writeText(Canvas canvas, String msg, final int line) {
- int b = 0, offsetX = 0;
- for (int x = 0; x < msg.length(); x++) {
- final char c = msg.charAt(x);
- if (c >= 55296 && c <= 56319) {
- final char nextC = msg.charAt(x + 1);
- if (nextC >= 56320 && nextC <= 57343) {
- canvas.drawText(msg.substring(b, x), padding + radius + offsetX, (line * textsize) + ((line - 1) *
- ((line > 1) ? typelinegap : 0)) + padding + radius, paint);
- offsetX += paint.measureText(msg.substring(b, x));
- final int currentOffsetX = offsetX;
- String emojiCode = Integer.toHexString(Character.toCodePoint(c, nextC));
- Bitmap emoji = getBitmapFromURL("https://raw.githubusercontent.com/" +
- "myrepository/master/" + emojiCode + "-" + platform + ".png");
- final double heightMultiplier = 1.5;
- int emojiWidth = ((int) ((((double)emoji.getWidth()) / emoji.getHeight()) * textsize * heightMultiplier));
- int emojiHeightIncrease = (int) ((textsize * (heightMultiplier - 1)) / 2);
- Rect emojiRectangle = new Rect(padding + radius + currentOffsetX, (line * textsize) + ((line - 1) *
- ((line > 1) ? typelinegap : 0)) + padding + radius - textsize - emojiHeightIncrease,
- padding + radius + currentOffsetX + emojiWidth,
- (line * textsize) + ((line - 1) *
- ((line > 1) ? typelinegap : 0)) + padding + radius + emojiHeightIncrease);
- canvas.drawBitmap(emoji, null, emojiRectangle, paint);
- offsetX += emojiWidth;
- b = x + 2;
- x++;
- }
- }
- }
- canvas.drawText(msg.substring(b), padding + radius + offsetX, (line * textsize) + ((line - 1) *
- ((line > 1) ? typelinegap : 0)) + padding + radius, paint);
- }
- private void drawBox(Canvas canvas) {
- paint.setColor(boxColor);
- Path p = new Path();
- p.moveTo(padding + radius, padding + radius);
- for (int n = 180; n <= 270; n++)
- p.lineTo(padding + radius + (float) (radius * Math.cos(Math.toRadians(n))),
- padding + radius + (float) (radius * Math.sin(Math.toRadians(n))));
- p.rLineTo(viewWidth - padding - radius, 0);
- for (int n = 270; n <= 360; n++)
- p.lineTo(viewWidth - padding - radius + (float) (radius * Math.cos(Math.toRadians(n))),
- padding + radius + (float) (radius * Math.sin(Math.toRadians(n))));
- p.lineTo(viewWidth - padding, viewHeight - padding);
- p.lineTo(padding + radius, viewHeight - padding);
- for (int n = 90; n <= 180; n++)
- p.lineTo(padding + radius + (float) (radius * Math.cos(Math.toRadians(n))),
- viewHeight - radius - padding + (float) (radius * Math.sin(Math.toRadians(n))));
- p.lineTo(padding, radius + padding);
- canvas.drawPath(p, paint);
- }
- public void setMessage(String message) {
- this.message = message;
- requestLayout();
- new DrawTextTask().execute(this);
- }
- protected int measureViewHeight() {
- int line = formatMessage(null);
- this.viewHeight = (textsize * line) + (2 * (radius + padding)) + (typelinegap * line - 1);
- return viewHeight;
- }
- public void doDraw(Canvas canvas) {
- Bitmap canvasBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888);
- Canvas c = new Canvas(canvasBitmap);
- c.drawColor(windowColor);
- if (boxColor != null)
- drawBox(c);
- formatMessage(c);
- canvas.drawBitmap(canvasBitmap, 0,0, null);
- }
- public static Bitmap getBitmapFromURL(String src) {
- try {
- URL url = new URL(src);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setDoInput(true);
- connection.connect();
- InputStream input = connection.getInputStream();
- Bitmap myBitmap = BitmapFactory.decodeStream(input);
- return myBitmap;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- class DrawTextTask extends AsyncTask<TextMessage, Void, Void> {
- @Override
- protected Void doInBackground(TextMessage[] param) {
- TextMessage msg = param[0];
- SurfaceHolder holder = msg.getHolder();
- Canvas c = null;
- try {
- c = holder.lockCanvas();
- synchronized (holder) {
- Log.d("Emoji", "Doing draw");
- msg.doDraw(c);
- }
- } finally {
- if (c != null) {
- holder.unlockCanvasAndPost(c);
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement