Skip to content

Commit a153c57

Browse files
author
ChenRui
committed
字体刻度条
1 parent fe46695 commit a153c57

File tree

6 files changed

+142
-9
lines changed

6 files changed

+142
-9
lines changed

app/src/main/java/com/rae/cnblogs/activity/FontSettingActivity.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.os.Bundle;
44
import android.support.annotation.Nullable;
5-
import android.util.Log;
65
import android.widget.SeekBar;
76
import android.widget.TextView;
87

@@ -48,10 +47,6 @@ public void onProgressChanged(SeekBar seekBar, int value, boolean b) {
4847
break;
4948
}
5049

51-
int width = mSeekBar.getWidth() - mSeekBar.getPaddingLeft() - mSeekBar.getPaddingRight();
52-
int thumbPos = mSeekBar.getPaddingLeft() + width * mSeekBar.getProgress() / mSeekBar.getMax();
53-
54-
Log.i("rae", "大小:" + thumbPos);
5550
}
5651

5752
@Override
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.rae.cnblogs.widget;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Paint;
6+
import android.graphics.Rect;
7+
import android.support.v4.content.ContextCompat;
8+
import android.support.v7.widget.AppCompatSeekBar;
9+
import android.text.TextPaint;
10+
import android.util.AttributeSet;
11+
import android.util.TypedValue;
12+
13+
import com.rae.cnblogs.R;
14+
15+
/**
16+
* 带文本的滑杆
17+
* Created by ChenRui on 2017/10/13 0013 12:50.
18+
*/
19+
public class RaeSeekBar extends AppCompatSeekBar {
20+
21+
private String[] mTickMarkTitles = new String[]{
22+
"小号",
23+
"标准",
24+
"中号",
25+
"大号",
26+
"特大",
27+
28+
};
29+
30+
private final Paint mTickMarkTitlePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
31+
private float mTickMarkTitleTextSize = 18;
32+
private float mOffsetY = 20;
33+
private final Rect mRect = new Rect();
34+
35+
public RaeSeekBar(Context context) {
36+
super(context);
37+
init();
38+
}
39+
40+
public RaeSeekBar(Context context, AttributeSet attrs) {
41+
super(context, attrs);
42+
init();
43+
}
44+
45+
public RaeSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
46+
super(context, attrs, defStyleAttr);
47+
init();
48+
}
49+
50+
protected void init() {
51+
mTickMarkTitleTextSize = getSize(mTickMarkTitleTextSize);
52+
mOffsetY = getSize(mOffsetY);
53+
}
54+
55+
@Override
56+
protected void onDraw(Canvas canvas) {
57+
// canvas.translate(0, mTickMarkTitleTextSize + mOffsetY);
58+
// canvas.save();
59+
super.onDraw(canvas);
60+
61+
// draw line
62+
63+
int width = canvas.getWidth();
64+
int height = canvas.getHeight();
65+
int h2 = height / 2;
66+
67+
68+
int max = getMax();
69+
int thumbStart = getPaddingLeft();
70+
int thumbEnd = getPaddingLeft() + width * max / max;
71+
72+
mRect.left = getPaddingLeft();
73+
mRect.right = thumbEnd;
74+
mRect.top = h2 - ((int) getSize(1));
75+
mRect.bottom = (mRect.top + (int) getSize(2));
76+
canvas.drawRect(mRect, mTickMarkTitlePaint);
77+
78+
79+
// canvas.save();
80+
81+
// // 总宽度
82+
// int width = getWidth() - getPaddingLeft() - getPaddingRight();
83+
//
84+
//
85+
// canvas.translate(0, -mTickMarkTitleTextSize);
86+
//
87+
//
88+
mTickMarkTitlePaint.setTextSize(mTickMarkTitleTextSize);
89+
mTickMarkTitlePaint.setTextAlign(Paint.Align.CENTER);
90+
mTickMarkTitlePaint.setColor(ContextCompat.getColor(getContext(), R.color.ph1));
91+
92+
for (int i = 0; i <= getMax(); i++) {
93+
String title = mTickMarkTitles[i % mTickMarkTitles.length];
94+
// 每个间隔的大小
95+
int thumbPos = getPaddingLeft() + width * i / getMax();
96+
mTickMarkTitlePaint.getTextBounds(title, 0, title.length(), mRect);
97+
// canvas.drawText(title, thumbPos, mRect.height(), mTickMarkTitlePaint);
98+
}
99+
100+
}
101+
102+
@Override
103+
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
104+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
105+
106+
// 加上字体大小
107+
int wm = MeasureSpec.getMode(widthMeasureSpec);
108+
int hm = MeasureSpec.getMode(heightMeasureSpec);
109+
int w = getMeasuredWidth();
110+
int h = getMeasuredHeight();
111+
h += mTickMarkTitleTextSize;
112+
h += mOffsetY;
113+
// 保存
114+
// setMeasuredDimension(MeasureSpec.makeMeasureSpec(w, wm), MeasureSpec.makeMeasureSpec(h, hm));
115+
}
116+
117+
protected float getSize(float size) {
118+
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, getResources().getDisplayMetrics());
119+
}
120+
121+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item
4+
android:id="@android:id/background"
5+
android:gravity="center_vertical|fill_horizontal">
6+
<shape android:shape="line">
7+
<solid android:color="@color/colorPrimary"/>
8+
</shape>
9+
</item>
10+
</layer-list>

app/src/main/res/drawable/seekbar_thumb_material_anim_font_setting.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<item>
3232
<shape android:shape="oval">
3333
<solid android:color="@color/badge_color"/>
34-
<size android:width="18dp" android:height="18dp"/>
34+
<size android:width="24dp" android:height="24dp"/>
3535
</shape>
3636
</item>
3737
</selector>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:shape="oval">
4+
<size
5+
android:width="4dp"
6+
android:height="4dp"/>
7+
<solid android:color="@android:color/white"/>
8+
</shape>

app/src/main/res/layout/activity_font_setting.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
android:textColor="@color/ph1"
3636
android:textSize="16sp"/>
3737

38-
<SeekBar
38+
<com.rae.cnblogs.widget.RaeSeekBar
3939
android:id="@+id/seekBar"
4040
android:layout_width="match_parent"
4141
android:layout_height="wrap_content"
@@ -45,10 +45,9 @@
4545
android:layout_marginLeft="36dp"
4646
android:layout_marginRight="36dp"
4747
android:layout_marginTop="60dp"
48-
android:indeterminateDrawable="@color/transparent"
4948
android:max="4"
5049
android:progress="1"
51-
android:progressDrawable="@color/transparent"
50+
android:progressDrawable="@android:color/transparent"
5251
android:theme="@style/Widget.AppCompat.SeekBar.Discrete"
5352
android:thumb="@drawable/seekbar_thumb_material_anim_font_setting"/>
5453

0 commit comments

Comments
 (0)