How to Draw Text with a Border on a Mapview in Android

How do you draw text with a border on a MapView in Android?

The easiest way to do this is with a Stroke... something like this:

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Paint strokePaint = new Paint();
strokePaint.setARGB(255, 0, 0, 0);
strokePaint.setTextAlign(Paint.Align.CENTER);
strokePaint.setTextSize(16);
strokePaint.setTypeface(Typeface.DEFAULT_BOLD);
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(2);

Paint textPaint = new Paint();
textPaint.setARGB(255, 255, 255, 255);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(16);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);

canvas.drawText("Some Text", 100, 100, strokePaint);
canvas.drawText("Some Text", 100, 100, textPaint);

super.draw(canvas, mapView, shadow);
}

This will draw a border of 2 pixels around the outside of the text then draw the text over the top of it, giving you the illusion of an outline.

Also, it may be worth setting the Paints up in the constructor then just reusing them.

How to put a border around an Android TextView?

You can set a shape drawable (a rectangle) as background for the view.

<TextView android:text="Some text" android:background="@drawable/back"/>

And rectangle drawable back.xml (put into res/drawable folder):

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/white" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

You can use @android:color/transparent for the solid color to have a transparent background.
You can also use padding to separate the text from the border.
for more information see: http://developer.android.com/guide/topics/resources/drawable-resource.html

How to make border for text in TextView?

public class CoustomTextView extends TextView {

private float strokeWidth;
private Integer strokeColor;
private Paint.Join strokeJoin;
private float strokeMiter;

public CoustomTextView(Context context) {
super(context);
init(null);
}

public CoustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}

public CoustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}

public void init(AttributeSet attrs) {

if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CoustomTextView);

if (a.hasValue(R.styleable.CoustomTextView_strokeColor)) {
float strokeWidth = a.getDimensionPixelSize(R.styleable.CoustomTextView_strokeWidth, 1);
int strokeColor = a.getColor(R.styleable.CoustomTextView_strokeColor, 0xff000000);
float strokeMiter = a.getDimensionPixelSize(R.styleable.CoustomTextView_strokeMiter, 10);
Paint.Join strokeJoin = null;
switch (a.getInt(R.styleable.CoustomTextView_strokeJoinStyle, 0)) {
case (0):
strokeJoin = Paint.Join.MITER;
break;
case (1):
strokeJoin = Paint.Join.BEVEL;
break;
case (2):
strokeJoin = Paint.Join.ROUND;
break;
}
this.setStroke(strokeWidth, strokeColor, strokeJoin, strokeMiter);
}
}
}

public void setStroke(float width, int color, Paint.Join join, float miter) {
strokeWidth = width;
strokeColor = color;
strokeJoin = join;
strokeMiter = miter;
}

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);

int restoreColor = this.getCurrentTextColor();
if (strokeColor != null) {
TextPaint paint = this.getPaint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(strokeJoin);
paint.setStrokeMiter(strokeMiter);
this.setTextColor(strokeColor);
paint.setStrokeWidth(strokeWidth);
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
this.setTextColor(restoreColor);
}
}
}

Usage:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

CoustomTextView coustomTextView = (CoustomTextView) findViewById(R.id.pager_title);
}
}

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@mipmap/background">

<pk.sohail.gallerytest.activity.CoustomTextView
android:id="@+id/pager_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="@string/txt_title_photo_gallery"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="bold"
app:outerShadowRadius="10dp"
app:strokeColor="@color/title_text_color"
app:strokeJoinStyle="miter"
app:strokeWidth="2dp" />

</RelativeLayout>

attars:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="CoustomTextView">

<attr name="outerShadowRadius" format="dimension" />
<attr name="strokeWidth" format="dimension" />
<attr name="strokeMiter" format="dimension" />
<attr name="strokeColor" format="color" />
<attr name="strokeJoinStyle">
<enum name="miter" value="0" />
<enum name="bevel" value="1" />
<enum name="round" value="2" />
</attr>
</declare-styleable>

</resources>

Programmatically:

CoustomTextView mtxt_name = (CoustomTextView) findViewById(R.id.pager_title);

Use setStroke(); method before calling setText();

Android textview outline text

You can put a shadow behind the text, which can often help readability. Try experimenting with 50% translucent black shadows on your green text. Details on how to do this are over here: Android - shadow on text?

To really add a stroke around the text, you need to do something a bit more involved, like this:
How do you draw text with a border on a MapView in Android?

Android - Draw text with solid background onto canvas to be used as a bitmap

Instead of this:

canvas.drawText(text, 150, 50, mTextOutline);

you should draw a Rectagle with one of the drawRect functions from Canvas.

Beware that

mTextOutline.setStyle(Paint.Style.STROKE);

Means only paint the border (stroke), while

mTextOutline.setStyle(Paint.Style.FILL);

Should fill the rectangle.

You can measure the size of the text by using Paint.getTextBounds, and maybe increase it a little so you have some borders.

Of course, paint the border before the text, or you will hide it.

Image with border in map overlay using android

here is some code that draws a circle:

private class ProximityOverlay extends Overlay {

public void draw(Canvas canvas, MapView mapview, boolean b) {
// draw some stuff in here, like
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2.0f);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Projection projection = mapView.getProjection();
GeoPoint leftGeo = new GeoPoint((int) (latitude * 1e6),
(int) (longitude * 1e6));
Point left = new Point();
projection.toPixels(leftGeo, left);

paint.setColor(Color.parseColor("#00CCFF"));
paint.setStyle(Style.FILL);
canvas.drawCircle(left.x, left.y, 9, paint);
paint.setColor(Color.parseColor("#003399"));
paint.setStyle(Style.STROKE);
canvas.drawCircle(left.x, left.y, 10, paint);

}

you can modify it to draw a black rectangle:

canvas.drawRect(left, top, right, bottom, paint);


Related Topics



Leave a reply



Submit