Set text in textview very blurry - android

I'm looking into methods of completely blurring a text in a textview. Read, completely unreadable (pixelate). The methods i'm using now are using shadows. But this seems very inefficient. Anyone who has a better solution?

I did not like the Paint method used in the other answers. The below code is working for me with the radius calculated depending on the font size and applying it directly on the TextView.
if (Build.VERSION.SDK_INT >= 11) {
yourTextView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
float radius = yourTextView.getTextSize() / 3;
BlurMaskFilter filter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL);
yourTextView.getPaint().setMaskFilter(filter);

You should try to use canvas instead of TextView and draw a text with blur.
There is a lot of method using BlurMaskFilter and you can use this like that :
paint.setMaskFilter(new BlurMaskFilter(float radius, BlurMaskFilter.Blur style));

You may try below methods:
Whenever you want to show blurry text, use a specialized font for
your TextView, added to asset folder of your project.
Try setting a transparent color[#00ffffff] to your text and give some shadow to it.
I hope one of the above method helps.

If you are looking to obfuscate the text you can try using TransformationMethod of TextView.
Here you have an example, is not a blurring effect but may help: https://gist.github.com/RicardAparicio/bdd6cd30410f9431e1da238202a1dfa9

Related

How to create a date drawable like google calendar app

I don't know how to create a drawable and add it to toolbar like the image above (red circle). The number is corresponding to today's date.
If you have any ideas, please help me.
UPDATE:
Thanks for your ideas. This what I come up with:
Create my custom Drawable (eg: TextDrawable extends Drawable)
Override the draw(Canvas canvas) method
Programmatically add my custom Drawable to my Toolbar:
inside: onCreateOptionsMenu(Menu menu)
menu.add(0, 0, 1, getString(R.string.jump_to_today)).setIcon(new TextDrawable("22")).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
For now, it's display a text date like I want (haven't has the calendar background). I think I will have to write it on my draw method.
Thanks for your help!
Trying writing text on an image...
Firstly, you need a calendar image without text ("22" in this case). Then, you write text "22" on the image. Something like this:
Bitmap calendarBitmap = ... // Load your calendar image here
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText("22", x, y, paint);
Which x, y are the coordinates of the text's position on the image. You also need to change Color.BLACK to the image's color for consistency.
Finally, you get calendarBitmap as a result.
I think the best way is to get the image you want as the calendar (maybe you can find it inside android resources). Then transform it into a nine patch and use it as the background drawable of your TextView.
You can read google documentation about creating nine patch. You will be able to define in wich region the text should be by defining the content area.

RemoteViews (Widget) has fuzzy text when using a Bitmap

To place a custom font on my widget I am creating a bitmap with the font inside of it, and then placing that bitmap into my RemoteViews. However the text on the bitmap is pretty fuzzy, and looks really pixelated compared to how crystal clear it is within an Activity.
There are quite a few apps already that use custom fonts on a widget. I haven't found a solid way to do this yet though. If anyone has a better solution to this problem I would love to hear it!
Right now this is what I am using to create the bitmap and place it on the widget:
RemoteViews widgetView = new RemoteViews(this.getPackageName(), R.layout.widget);
widgetView.setImageViewBitmap(R.id.widgetImage, buildBitmap());
with
public Bitmap buildBitmap() {
Bitmap bitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
TextPaint textPaint = new TextPaint(TextPaint.LINEAR_TEXT_FLAG | TextPaint.ANTI_ALIAS_FLAG);
textPaint.setTypeface(getFont());
textPaint.setStyle(Style.FILL);
textPaint.setColor(fontColor);
textPaint.setTextSize(fontSize);
StaticLayout staticLayout = new StaticLayout(textString, textPaint, bitmap.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
staticLayout.draw(canvas);
return bitmap;
}
The culprit Widgets! The one on the top is with the custom font, the middle image is with the default Sans font on a bitmap, to show that the text is fuzzy no matter what font is used.
The Last image is what the text looks like when using the default remoteView.setTextViewText("Text"); instead of a bitmap.
Any assistance on this problem would be greatly appreciated!
You can't assume 160x84. That's a very small size. When scaled it will likely be fuzzy as you can see. Instead, you'll want to actually measure out the bitmap size based on measurements of your text at a given sp. Paint.measureText is useful sometimes for these kinds of things to get the width, but you'll need height as well to do it right, so getTextBounds might be more useful. You'll probably need to add some padding too, but this should get you started. As you can see in your pictures, the clarity is not the only problem you have to deal with. You'll have to figure out word wrapping too (perhaps here is where Paint.measureText will come in handy).
The ImageView you are using requires android:scaleType="matrix" attribute to be added. Edit your widget layout xml and add that attribute where needed. The ImageView will then not try and scale your image to fit the view.
As previous posts have said your pixel dimensions are wrong. You should be maintaining a record of the size of each of your widgets so you can later use it for creating the bitmap.
If you want help working this bit out I can post some code. Accept this answer and post another about that question specifically and I will answer there with the code.

How can I anchor my DynamicLayout to the bottom of my canvas?

I am working on a custom View that includes frequently updated text that I want to anchor to the bottom of the canvas.
The text length changes as well, and I'd like it to wrap, moving up the screen as more lines are needed. (DynamicLayout thus seems like a solid choice for automating this)
However, I don't see any options in the docs about specifying where on my canvas the text is drawn or in which direction it should "grow".
Here is my initialization:
TextPaint subtleTextPaint = new TextPaint();
DynamicLayout dl = new DynamicLayout(text,subtleTextPaint,getWidth()
,Layout.Alignment.ALIGN_CENTER,1,0,true);
And in onDraw(), I simply pass the canvas to the DynamicLayout object like so:
dl.draw(c);
Right now, the text is drawn at the very top of the screen and word-wraps downwards as the text gets longer.
After quite a bit of searching, I found nothing that did what I wanted. So, in true hacker style, I created my own solution. By extending the DynamicLayout class and overwriting the getLineTop() function, I was able to achieve the functionality I was looking for.
I've posted the source code here.
What i think 1st you get the Width of the screen and then set the Text with the X y according to your screen dimension
that will work
snippet is following
width=canvas.getWidth();
if(width==720)
{
canvas.drawText(String, x , y, drawText);
}
else if(width==480)
{
canvas.drawText(String, x , (y, drawText);
}
else
{
canvas.drawText(String, x , (y, drawText);
}

How to draw BIG thin text in android

I need to draw single character on my view as BIG thin text as shown in image below (text as just simple lines)
But my code below gives me following image (text as shape)
Paint dPaint = new Paint();
dPaint.setAntiAlias(true);
dPaint.setDither(true);
dPaint.setColor(Color.RED);
dPaint.setTextSize(getWidth()*.8F);
dPaint.setStyle(Paint.Style.STROKE);
dPaint.setStrokeWidth(1);
canvas.drawText("A",getWidth()*.3F, getHeight()*.6F, dPaint);
I tried various paint and stroke properties but none worked out. Also I read the in java 2D text is drawn as shape.
Is there a way to draw text (character in particular) as lines rather then shape. Also text down should be BIG in size.
I think only two ways to achieve your requirement.
Use a custom font-type with your font style and use it in your application as below
TextView text = (TextView) findViewById(R.id.textView);
Typeface customFont = Typeface.createFromAsset(getAssets(), "ThinText.ttf");
text.setTypeface(customFont);
Another technique is you have to draw the shape of give character as lines by calculating the coordinates required in the path to draw that alphabet or digit.
I hope it may help you.
You increase strokewidth, then thin is increased like so:
dPaint.setStrokeWidth(5);

How to know the size of a text when using Canvas.drawText()

I'm using the Canvas.drawText() to write text on a custom view.
I want to add a background 9-patch drawable behind the text, so I need to know the boundaries of the text drawn.
The text is given by the user, so it's not a fixed size.
For now I'm using a monospace font and I managed to build a size which looks not so bad, but in the hand i'd rather use another font.
You can use public void getTextBounds(String text, int start, int end, Rect bounds) method of Paint to get a rectangle that encloses all your text. Take a look at getTextBounds.
Hope this helps!
You also may use measureText
mTextTimeColorPaint.measureText(timeText)
Like in this post Center Text on Watch
I think getTextBounds(String text, int start, int end, Rect bounds) can do it for you.
This method belongs to Paint class:
Refer to this link

Categories

Resources