1.Android 中Stream 的使用(向下兼容)
step1:implementation 'com.annimon:stream:1.2.1'
step2:用法
ArrayList<User> arrayList = new ArrayList<>();
//从集合ArrayList中获取height>170 的数据集
List<User> users= Stream.of(arrayList).
filter(user-> user.getHeight() > 170).
collect(Collectors.toList());
//从ArrayList里获取姓名的集合
List<String> names = Stream.of(arrayList)
.map(User::getName)
.collect(Collectors.toList());
//取出User类中name重新构建成Customer的结合
List<Customer> userList = Stream.of(arrayList).
map(user-> new Customer(user.getName())).
collect(Collectors.toList());
//Sream返回的结果是List集合,如果你需要ArrayList集合,只需要强转类型即可
ArrayList<String> names = (ArrayList<String>) Stream.of(arrayList)
.map(User::getName)
.collect(Collectors.toList());
2.两个list进行“交集,并集,差集,去重复并集”的操作
//交集
addAll();
retainAll();
//并集
addAll();
//差集
removeAll();
//去重复并集
removeAll();
addAll();
3.RecyclerView为网格布局添加间距
RecyclerView.ItemDecoration gridItemDecoration = new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
int spanIndex = layoutParams.getSpanIndex();
outRect.top = xx;
if (spanIndex == 0) {
outRect.right = xx;
} else {
outRect.left = xx;
}
}
};
recyclerView.addItemDecoration(gridItemDecoration);
4.对一段文字进行拆分设置大小和加粗
public static void setGroupTextStyle(TextView textView, String frontText, int frontTextSize,
boolean frontIsBold,String laterText, int laterTextSize, boolean laterIsBold) {
if (frontText == null) {
return;
}
String text = frontText + laterText;
Spannable wordtoSpan = new SpannableString(text);
wordtoSpan.setSpan(new AbsoluteSizeSpan(ScreenUtils.sp2px(BaseApplication.getContext(), frontTextSize)),
0, frontText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (frontIsBold) {
wordtoSpan.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
0, frontText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
wordtoSpan.setSpan(new AbsoluteSizeSpan(ScreenUtils.sp2px(BaseApplication.getContext(), laterTextSize)),
frontText.length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (laterIsBold) {
wordtoSpan.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
frontText.length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.setText(wordtoSpan);
}
5.设置字体大小动态化
public static float getAdjustTvTextSize(TextView tv, int maxWidth, String text) {
int avaiWidth = maxWidth - tv.getPaddingLeft() - tv.getPaddingRight();
if (avaiWidth <= 0) {
return 0f;
}
TextPaint textPaintClone = new TextPaint(tv.getPaint());
// note that Paint text size works in px not sp
float trySize = textPaintClone.getTextSize();
while (textPaintClone.measureText(text) > avaiWidth) {
trySize--;
textPaintClone.setTextSize(trySize);
}
if (trySize > 0) {
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
}
return trySize;
}
6.判断是否快速点击了,时间自定义
private static final int MIN_DELAY_TIME = 800; //两次点击间隔不能少于800ms
private static long lastClickTime;
public static boolean isFastClick() {
boolean flag = true;
long currentClickTime = System.currentTimeMillis();
if ((currentClickTime - lastClickTime) >= MIN_DELAY_TIME) {
flag = false;
}
lastClickTime = currentClickTime;
return flag;
}
7.app整体置灰
第一种:基础封装类中重载此方法(类似BaseActivity类),替换根布局
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
try {
if ("FrameLayout".equals(name)) {
int count = attrs.getAttributeCount();
for (int i = 0; i < count; i++) {
String attributeName = attrs.getAttributeName(i);
String attributeValue = attrs.getAttributeValue(i);
if (attributeName.equals("id")) {
int id = Integer.parseInt(attributeValue.substring(1));
String idVal = getResources().getResourceName(id);
if ("android:id/content".equals(idVal)) {
GrayFrameLayout grayFrameLayout = new GrayFrameLayout(context, attrs);
return grayFrameLayout;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return super.onCreateView(name, context, attrs);
}
GrayFrameLayout :
public class GrayFrameLayout extends FrameLayout {
private Paint mPaint = new Paint();
public GrayFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
}
@Override
protected void dispatchDraw(Canvas canvas) {
canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);
super.dispatchDraw(canvas);
canvas.restore();
}
@Override
public void draw(Canvas canvas) {
canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);
super.draw(canvas);
canvas.restore();
}
}
第二种:基础封装类中重载此方法,设置软硬件加速
@Override
protected void onStart() {
super.onStart();
View view = findViewById(android.R.id.content);
if(view!=null){
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
Paint mPaint = new Paint();
mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
//setLayerType 软硬件加速看实际页面需要
view.setLayerType(View.LAYER_TYPE_HARDWARE, mPaint);
}
}
8.app性能检测
第一种:
"Debug.startMethodTracing("App");"
---检测代码在这两个方法之间被包裹---
"Debug.stopMethodTracing();"
执行之后到android studio右下角的"Device File Explorer"中的
"sdcard/Android/data/项目包名/files"下的"App.trace"文件双击打开就行
第二种:
利用android studio的"profile插件"运行,利用"record和stop"记录分析文件