Fix font fit text view

This commit is contained in:
Victor Shcherb 2012-09-16 20:34:22 +02:00
parent 49c8d80326
commit 7fd28b9d3f
6 changed files with 41 additions and 46 deletions

View file

@ -167,8 +167,6 @@ public class BinaryRoutePlanner {
// TODO TO-DO U-TURN
// TODO fastest/shortest way
/**
* Calculate route between start.segmentEnd and end.segmentStart (using A* algorithm)
* return list of segments

View file

@ -9,6 +9,7 @@
1. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
-->
<string name="ending_point_too_far">Starting point too far from nearest road.</string>
<string name="btn_add_tag">Add Tag</string>
<string name="btn_advanced_mode">Advanced Mode...</string>
<string name="poi_filter_parking">Parking</string>

View file

@ -36,7 +36,7 @@ public class CustomTitleBar {
return vidw;
}
public TextView getTitleView(){
public FontFitTextView getTitleView(){
return getView().getTextView();
}
@ -49,7 +49,7 @@ public class CustomTitleBar {
protected String titleString;
protected int titleImageRes;
protected OnClickListener click;
private TextView title;
private FontFitTextView title;
public CustomTitleBarView(String text, int img, OnClickListener cl) {
this.titleString = text;
this.titleImageRes = img;
@ -72,12 +72,12 @@ public class CustomTitleBar {
init(wnd.getDecorView());
}
public TextView getTextView(){
public FontFitTextView getTextView(){
return title;
}
public void init(View wnd) {
title = (TextView) wnd.findViewById(R.id.title_text);
title = (FontFitTextView) wnd.findViewById(R.id.title_text);
title.setText(titleString);
Button backButton = (Button) wnd.findViewById(R.id.back_button);
backButton.setContentDescription(wnd.getContext().getString(R.string.close));

View file

@ -1,90 +1,87 @@
package net.osmand.plus.activities;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
/* Based on
* from http://stackoverflow.com/questions/2617266/how-to-adjust-text-font-size-to-fit-textview
*/
public class FontFitTextView extends TextView {
public class FontFitTextView extends LinearLayout {
private static float MAX_TEXT_SIZE = 28;
private TextView tv;
public FontFitTextView(Context context) {
this(context, null);
tv = new TextView(context);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tv.setTextColor(Color.WHITE);
addView(tv, lp);
}
public FontFitTextView(Context context, AttributeSet attrs) {
super(context, attrs);
tv = new TextView(context);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tv.setTextColor(Color.WHITE);
addView(tv, lp);
}
// Default constructor override
public FontFitTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
// public FontFitTextView(Context context, AttributeSet attrs, int defStyle) {
// super(context, attrs, defStyle);
// }
private void refitText(String text, int textWidth, int textHeight, boolean layout) {
if (textWidth > 0) {
// Drawable left = getCompoundDrawables()[0];
// Drawable right = getCompoundDrawables()[2];
// float availableWidth = textWidth - this.getPaddingLeft()
// - this.getPaddingRight() - this.getCompoundDrawablePadding()
// - (left != null ? left.getMinimumWidth() : 0)
// - (right != null ? right.getMinimumWidth() : 0);
float availableWidth = textWidth;
TextPaint tp = getPaint();
TextPaint tp = new TextPaint();
tp.setTextSize(MAX_TEXT_SIZE);
int lines = text.length() / 25 + 1;
Rect rect = new Rect();
tp.getTextBounds(text, 0, text.length(), rect);
while (rect.width() > (availableWidth + 5) * lines || rect.height() * lines > textHeight) {
while ((rect.width() > (availableWidth + 5) * lines || rect.height() * lines > textHeight)
&& tp.getTextSize() > 8) {
tp.setTextSize(tp.getTextSize() - 1);
tp.getTextBounds(text, 0, text.length(), rect);
// setTextScaleX(availableWidth / size);
}
if (getLineCount() != lines) {
if (tv.getLineCount() != lines) {
if (lines == 1) {
setGravity(Gravity.CENTER_VERTICAL);
} else {
setGravity(Gravity.TOP);
}
setLines(lines);
setMaxLines(lines);
requestLayout();
tv.setLines(lines);
tv.setMaxLines(lines);
}
if (getTextSize() != tp.getTextSize()) {
setTextSize(TypedValue.COMPLEX_UNIT_PX, tp.getTextSize());
if(tv.getTextSize() != tp.getTextSize()) {
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, tp.getTextSize());
tv.requestLayout();
}
}
}
public void setText(String text){
tv.setText(text);
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
refitText(this.getText().toString(), parentWidth, parentHeight, false);
this.setMeasuredDimension(parentWidth, parentHeight);
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if(changed) {
refitText(tv.getText().toString(), r - l, b - t, true);
}
}
@Override
protected void onTextChanged(final CharSequence text, final int start,
final int before, final int after) {
refitText(text.toString(), this.getWidth(), this.getHeight(), true);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w != oldw || h != oldh) {
refitText(this.getText().toString(), w, h, true);
}
}
}

View file

@ -146,8 +146,7 @@ public abstract class SearchByNameAbstractActivity<T> extends OsmandListActivity
}
public void setLabelText(int res) {
titleBar.getTitleView().setText(res);
//((TextView)findViewById(R.id.Label)).setText(res);
titleBar.getTitleView().setText(getString(res));
}
protected int getZoomToDisplay(T item){

View file

@ -359,7 +359,7 @@ public class RouteProvider {
}
RouteSegment en = router.findRouteSegment(end.getLatitude(), end.getLongitude(), ctx);
if (en == null) {
return new RouteCalculationResult("End point is far from allowed road.");
return new RouteCalculationResult(app.getString(R.string.ending_point_too_far));
}
try {
List<RouteSegmentResult> result = router.searchRoute(ctx, st, en, leftSide);