1.Android 中Stream 的使用(向下兼容)
step1:implementation 'com.annimon:stream:1.2.1'
step2:用法
ArrayList<User> arrayList = new ArrayList<>();
//從集合ArrayList中獲取height>170 的數(shù)據(jù)集
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重新構(gòu)建成Customer的結(jié)合
List<Customer> userList = Stream.of(arrayList).
map(user-> new Customer(user.getName())).
collect(Collectors.toList());
//Sream返回的結(jié)果是List集合丈莺,如果你需要ArrayList集合敷钾,只需要強(qiáng)轉(zhuǎn)類型即可
ArrayList<String> names = (ArrayList<String>) Stream.of(arrayList)
.map(User::getName)
.collect(Collectors.toList());
2.兩個(gè)list進(jìn)行“交集,并集峻呕,差集瞧挤,去重復(fù)并集”的操作
//交集
addAll();
retainAll()官紫;
//并集
addAll();
//差集
removeAll();
//去重復(fù)并集
removeAll();
addAll();
3.RecyclerView為網(wǎng)格布局添加間距
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.對(duì)一段文字進(jìn)行拆分設(shè)置大小和加粗
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.設(shè)置字體大小動(dòng)態(tài)化
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.判斷是否快速點(diǎn)擊了畸颅,時(shí)間自定義
private static final int MIN_DELAY_TIME = 800; //兩次點(diǎn)擊間隔不能少于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整體置灰
第一種:基礎(chǔ)封裝類中重載此方法(類似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();
}
}
第二種:基礎(chǔ)封裝類中重載此方法,設(shè)置軟硬件加速
@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 軟硬件加速看實(shí)際頁面需要
view.setLayerType(View.LAYER_TYPE_HARDWARE, mPaint);
}
}
8.app性能檢測(cè)
第一種:
"Debug.startMethodTracing("App");"
---檢測(cè)代碼在這兩個(gè)方法之間被包裹---
"Debug.stopMethodTracing();"
執(zhí)行之后到android studio右下角的"Device File Explorer"中的
"sdcard/Android/data/項(xiàng)目包名/files"下的"App.trace"文件雙擊打開就行
第二種:
利用android studio的"profile插件"運(yùn)行杏节,利用"record和stop"記錄分析文件