0

我正在尝试使用 为地图标记的群集图标设置自定义字体SquareTextView,这是android-maps-utils-amap扩展 TextView 的一类库。在DefaultClusterRenderer我使用此代码设置自定义字体但没有效果。所以请帮助我理解我需要做什么来改变 SquareTextView 的字体

private SquareTextView makeSquareTextView(Context context) {
    SquareTextView squareTextView = new SquareTextView(context);            
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "whatever.ttf");
    squareTextView.setTypeface(typeface);
}

这是源代码SquareTextView

package com.amap.api.maps2d.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;

public class SquareTextView extends TextView {
    private int mOffsetTop = 0;
    private int mOffsetLeft = 0;

    public SquareTextView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        int dimension = Math.max(width, height);
        if (width > height) {
            mOffsetTop = width - height;
            mOffsetLeft = 0;
        } else {
            mOffsetTop = 0;
            mOffsetLeft = height - width;
        }
        setMeasuredDimension(dimension, dimension);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.translate(mOffsetLeft / 2, mOffsetTop / 2);
        super.draw(canvas);
    }
}
4

2 回答 2

0

我有自己的 TextView 实现,我用它来设置如下字体,它对我有用:

public class MyNewTextView extends TextView {
private int mOffsetTop = 0;
private int mOffsetLeft = 0;

public MyNewTextView(Context context) {
    super(context);
}

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

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

@Override
public void setTypeface(Typeface tf, int style) {
     super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),   "fonts/whatever.ttf"), style);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();
    int dimension = Math.max(width, height);
    if (width > height) {
        mOffsetTop = width - height;
        mOffsetLeft = 0;
    } else {
        mOffsetTop = 0;
        mOffsetLeft = height - width;
    }
    setMeasuredDimension(dimension, dimension);
}

@Override
public void draw(Canvas canvas) {
    canvas.translate(mOffsetLeft / 2, mOffsetTop / 2);
    super.draw(canvas);
}
}

所以尝试为 SquareTextView 覆盖 setTypeface()

于 2016-03-27T14:37:31.617 回答
0

自己实现SquareTextView试试这个:

public class MySquareTextView extends SquareTextView {

    public MySquareTextView (Context context) {
        super(context);
        setTypeface();
    }

    public MySquareTextView (Context context, AttributeSet attrs) {
        super(context, attrs);
        setTypeface();
    }

    public MySquareTextView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTypeface();
    }


    public void setTypeface(){
        setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Italic.ttf"));
    }
}
于 2016-03-27T14:48:26.723 回答