Android仿全局字体⼤⼩调整
⼀、前⾔
最近项⽬添加了⼀项调整应⽤字体⼤⼩功能,做完后空闲之余总结⼀下。本功能仿照应⽤“设置” - “通⽤” - “字体⼤⼩”功能,⼜有⼀点区别。据我所知,常见改变全局字体⼤⼩⽅法有两种,我把这两种分为可控和不可控,为什么这么分呢,当然不是为了⽅便记忆。那么简单说下两者⽅式的实现过程:
1、不可控:通过重写Actiivity的getResources()⽅法更新应⽤的字体倍数来调整全局字体⼤⼩
优点:实现简单
缺点:不可控制(所有字体放⼤倍数都是⼀致),⽆法跨进程(项⽬中另⼀个进程的字体不会改变,需要重新配置),只对sp字体单位有效果。....
2、可控:通过setTheme()⽅法,⼀开始就初始化设置不同风格的字体样式来更改全局字体⼤⼩。
优点:随意调节任意位置的字体⼤⼩
缺点:实现⿇烦
⽽本⽂正式采⽤了第⼀种⽅案,主要是中途添加该功能,时间也不充裕,抽取字体⼤⼩⼜太过耗时。
字体⼤⼩个⼈猜测使⽤第⼆种⽅案,后者是更好的实现⽅式也不⼀定。
⼆、效果预览
三、实现步骤
1、⾃定义字体调整控件
⽹上了⼀个相似的控件并加以完善,功能相对简单,就不做介绍了。/**
* TODO 仿字体⼤⼩调整
* ⾃定义属性:
* lineWidth        线条粗细
* lineColor        线条颜⾊
* totalCount      线条格数
* circleColor      球型颜⾊
* circleRadius    球型颜⾊半径
* textFontColor    ⽂字颜⾊
* smallSize        ⼩“A” 字体⼤⼩
* standerSize      “标准” 字体⼤⼩
* bigSize          ⼤“A” 字体⼤⼩
* defaultPosition  默认位置
*/
public class FontSizeView extends View {
private int defaultLineColor = b(33, 33, 33);
private int defaultLineWidth;
private int defaultMax = 5;
private int defaultCircleColor = Color.WHITE;
private int defaultCircleRadius;
recycle是什么意思
// 当前所在位置
private int currentProgress;
// 默认位置
private int defaultPosition = 1;
// ⼀共有多少格
private int max = 7;
/
/ 线条颜⾊
private int lineColor = Color.BLACK;
// 线条粗细
private int lineWidth;
//字体颜⾊
private int textColor = Color.BLACK;
//字体⼤⼩
private int smallSize = 14;
private int standerSize = 16;
private int bigSize = 28;
// 圆半径
private int circleRadius;
private int circleColor = Color.WHITE;
// ⼀段的宽度,根据总宽度和总格数计算得来
private int itemWidth;
// 控件的宽⾼
private int height;
private int width;
// 画笔
private Paint mLinePaint;
private Paint mTextPaint;
private Paint mText1Paint;
private Paint mText1Paint;
private Paint mText2Paint;
private Paint mCirclePaint;
// 滑动过程中x坐标
private float currentX = 0;
// 有效数据点
private List<Point> points = new ArrayList<>();
private float circleY;
private float textScaleX;
private float text1ScaleX;
private float text2ScaleX;
public FontSizeView(Context context) {
this(context, null);
}
public FontSizeView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
// initDefault
defaultLineWidth = DensityUtils.dp2px(context, 2);
defaultCircleRadius = DensityUtils.dp2px(context, 35);
lineWidth = DensityUtils.dp2px(context, 1);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FontSizeView);        final int N = IndexCount();
for (int i = 0; i < N; i++) {
Index(i), typedArray);
}
// 初始化画笔
mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mLinePaint.setColor(lineColor);
mLinePaint.setStyle(Paint.Style.FILL_AND_STROKE);
mLinePaint.setStrokeWidth(lineWidth);
//⽂字画笔
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(textColor);
mTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//        mTextPaint.setStrokeWidth(DensityUtils.dp2px(context, 1));
mTextPaint.setTextSize(DensityUtils.sp2px(context, smallSize));
textScaleX = asureText("A");
//⽂字画笔
mText1Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
mText1Paint.setColor(textColor);
mText1Paint.setStyle(Paint.Style.FILL_AND_STROKE);
mText1Paint.setTextSize(DensityUtils.sp2px(context, bigSize));
text1ScaleX = asureText("A");
//⽂字画笔
mText2Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
mText2Paint.setColor(textColor);
mText2Paint.setStyle(Paint.Style.FILL_AND_STROKE);
mText2Paint.setTextSize(DensityUtils.sp2px(context, standerSize));
text2ScaleX = asureText("标准");
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setColor(circleColor);
mCirclePaint.setStyle(Paint.Style.FILL);
/
/ 设置阴影效果
setLayerType(LAYER_TYPE_SOFTWARE, null);
mCirclePaint.setShadowLayer(2, 0, 0, b(33, 33, 33));
}
private void initCustomAttr(int attr, TypedArray typedArray) {
if (attr == R.styleable.FontSizeView_lineColor) {
lineColor = Color(attr, defaultLineColor);
} else if (attr == R.styleable.FontSizeView_circleColor) {
} else if (attr == R.styleable.FontSizeView_circleColor) {
circleColor = Color(attr, defaultCircleColor);
} else if (attr == R.styleable.FontSizeView_lineWidth) {
lineWidth = DimensionPixelSize(attr, defaultLineWidth);
} else if (attr == R.styleable.FontSizeView_circleRadius) {
circleRadius = DimensionPixelSize(attr, defaultCircleRadius);
} else if (attr == R.styleable.FontSizeView_totalCount) {
max = Integer(attr, defaultMax);
} else if (attr == R.styleable.FontSizeView_textFontColor) {
textColor = Color(attr, textColor);
} else if (attr == R.styleable.FontSizeView_smallSize) {
smallSize = Integer(attr, smallSize);
} else if (attr == R.styleable.FontSizeView_standerSize) {
standerSize = Integer(attr, standerSize);
} else if (attr == R.styleable.FontSizeView_bigSize) {
bigSize = Integer(attr, bigSize);
}else if (attr == R.styleable.FontSizeView_defaultPosition) {
defaultPosition = Integer(attr, defaultPosition);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
height = h;
width = w;
circleY = height / 2;
// 横线宽度是总宽度-2个圆的半径
itemWidth = (w - 2 * circleRadius) / max;
// 把可点击点保存起来
for (int i = 0; i <= max; i++) {
points.add(new Point(circleRadius + i * itemWidth, height / 2));
}
//初始刻度
currentX = (defaultPosition).x;
}
@Override
protected void onDraw(Canvas canvas) {
//画字
canvas.drawText("A", (0).x - textScaleX / 2, height / 2 - 50, mTextPaint);
//画字
canvas.drawText("标准", (1).x - text2ScaleX / 2, height / 2 - 50, mText2Paint);
//画字
canvas.drawText("A", (points.size() - 1).x - text1ScaleX / 2, height / 2 - 50, mText1Paint);        // 先画中间的横线
canvas.(0).x, height / 2, (points.size() - 1).x, height / 2, mLinePaint);        // 绘制刻度
for (Point point : points) {
canvas.drawLine(point.x + 1, height / 2 - 20, point.x + 1, height / 2 + 20, mLinePaint);
}
// 画圆
if (currentX < circleRadius) {
currentX = circleRadius;
}
if (currentX > width - circleRadius) {
currentX = width - circleRadius;
}
// 实体圆
canvas.drawCircle(currentX + 1, circleY, circleRadius, mCirclePaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
currentX = X();
switch (Action()) {
case MotionEvent.ACTION_DOWN: