日常笔记
1.webView 相关:
webView onDestroy 中需要手动调用 webView.destroy(); 否则可能存在某些问题
2. 两int /long 型相除取两位小数结果
double result = Double.valueOf(new DecimalFormat("##.00").format((double) bytesWritten / totalSize * 100));
3 .window属性相关
private final int DIGITS = 2;
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
//两位小数
if ("".equals(source.toString())) {
return null;
}
String dValue = dest.toString();
String[] splitArray = dValue.split("\\.");
if (splitArray.length > 1) {
String dotValue = splitArray[1];
int diff = dotValue.length() + 1 - DIGITS;
if (diff > 0) {
return source.subSequence(start, end - diff);
}
}
//最大值
try {
int input = (int) Double.parseDouble(dest.toString() + source.toString());
if (isInRange(min, max, input))
return source;
} catch (NumberFormatException e) {
e.printStackTrace();
}
return "";
}
private boolean isInRange(int a, int b, int c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
6.intent filter相关
(1)自定义action时 category 必须有默认值,否则直接setAction 找不到对应的activity
7. 自定义permission:如果自己定义有permission。如果不设置android:exported = true 外边调用不到。如果设置外边调用的时候需要声明权限(可以防止adb调用)
8.canvas 画text
mTextPaint.getTextBounds(text, 0, text.length(), mTextBound);
canvas.drawText(text, (width - mTextBound.width()) / 2, (height + mTextBound.height()) / 2, mTextPaint);
9.canvas 画圆弧
第一个参数为圆弧的外框,注意为会出一半,所以位置要减去圆弧一半的宽度。第二个第三个参数为起点角度,终点角度,第四个参数为是否经过中心,如果为false 为空心,如果为true 为封闭圆弧。
RectF oval = new RectF(0 + outerCircleWidth / 2, 0 + outerCircleWidth / 2, width - outerCircleWidth / 2, height - outerCircleWidth / 2);
canvas.drawArc(oval, 0, progress * 360 / 100, false, mOuterCirclePaint);
10. 重写onMeasure().当是exctly时 为固定高度宽度,使用默认方法即可,其他情况需要自己测量。
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int with, height; if (widthMode == MeasureSpec.EXACTLY) { with = MeasureSpec.getSize(widthMeasureSpec); } else { with = getPaddingLeft() + (circlePadding + outerCircleWidth + innerCircleRadius) * 2 + getPaddingRight(); } if (heightMode == MeasureSpec.EXACTLY) { height = MeasureSpec.getSize(heightMeasureSpec); } else { height = getPaddingBottom() + (circlePadding + outerCircleWidth + innerCircleRadius) * 2 + getPaddingBottom(); } setMeasuredDimension(with, height); }
添加元素时:如果不主动设置子view的layoutParam,主动传入rootView 和 不传rootView 然后手动添加效果是一样的(如果需要单独设置子view的layoutParam 不要传rootView)
返回的View(***重要***):如果不传rootView ,返回的是生成的子view。如果传入rootView 返回的添加完子view的rootView.
所以重复添加的时候注意,如果使用第二种方式findviewById找到的永远是第一个元素,可以使用getChildAt()来处理,但是不建议这么做。