[TOC]
速度表盘 For Android
<com.afra55.speedometer.SpeedometerDialog
android:id="@+id/test_speedometer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:meterBg="@drawable/dialog_bg_1"
app:meterCenterDesc="km/h"
app:meterCenterIc="@drawable/dialog_center_icon_1"
app:meterDividerAreaNumber="8"
app:meterMaskBg="@drawable/dialog_mask_bg_1"
app:meterNumberLimitTextColor="#E30808"
app:meterNumberMargin="89dp"
app:meterNumberSelectedTextColor="#FFFFFF"
app:meterCenterNumberTextSize="18sp"
app:meterCenterDescTextSize="18sp"
app:meterCenterTextColor="#FFFFFF"
app:meterCenterDescTextColor="#FFFFFF"
app:meterNumberTextColor="#4DFFFFFF"
app:meterNumberTextSize="20sp"
app:meterPointer="@drawable/dialog_pointer_1"
/>
用来进行图像混合处理。一共有18种混合模式。官方文档
Paint paint = new Paint();
// DST 图
canvas.drawBitmap(destinationImage, 0, 0, paint);
PorterDuff.Mode mode = // choose a mode
paint.setXfermode(new PorterDuffXfermode(mode));
// SRC 图
canvas.drawBitmap(sourceImage, 0, 0, paint);
获取文字的内容高度,这个高度是文字绘制的最高点到最低点的距离:
fun TextPaint.getTextBound(str: String): Rect {
val rect = Rect()
getTextBounds(str, 0, str.length, rect)
return rect
}
fun TextPaint.getCapHeight(): Int {
// 获得0-9数字内容的高度
return getTextBound("1234567890").height()
}
fun Resources.dp2Px(dip: Float): Float {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, this.displayMetrics)
}
class SquareFrameLayout : FrameLayout {
constructor(context: Context) : super(context) {
init(null, 0)
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init(attrs, 0)
}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
context,
attrs,
defStyle
) {
init(attrs, defStyle)
}
fun init(attrs: AttributeSet?, defStyle: Int) {
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val widthMode = MeasureSpec.getMode(widthMeasureSpec)
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
var width = MeasureSpec.getSize(widthMeasureSpec)
if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.EXACTLY) {
val height = MeasureSpec.getSize(heightMeasureSpec)
// 如果高是 match_parent 或者指定了特定值, 则取宽高较小的值为整个view的宽高
width = Math.min(width, height)
}
setMeasuredDimension(width, width)
}
}
Speedometer is available under the Apache-2.0 license. See the LICENSE file for more info.
Copyright 2020 Afra55
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.








