隨手記之好方法

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"記錄分析文件
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末唬渗,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子奋渔,更是在濱河造成了極大的恐慌镊逝,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嫉鲸,死亡現(xiàn)場(chǎng)離奇詭異撑蒜,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門座菠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來狸眼,“玉大人,你說我怎么就攤上這事浴滴⊥孛龋” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵升略,是天一觀的道長(zhǎng)微王。 經(jīng)常有香客問我,道長(zhǎng)品嚣,這世上最難降的妖魔是什么炕倘? 我笑而不...
    開封第一講書人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮翰撑,結(jié)果婚禮上罩旋,老公的妹妹穿的比我還像新娘。我一直安慰自己眶诈,他們只是感情好瘸恼,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著册养,像睡著了一般东帅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上球拦,一...
    開封第一講書人閱讀 51,365評(píng)論 1 302
  • 那天靠闭,我揣著相機(jī)與錄音,去河邊找鬼坎炼。 笑死愧膀,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的谣光。 我是一名探鬼主播檩淋,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼萄金!你這毒婦竟也來了蟀悦?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤氧敢,失蹤者是張志新(化名)和其女友劉穎日戈,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體孙乖,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡浙炼,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年份氧,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片弯屈。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蜗帜,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出资厉,到底是詐尸還是另有隱情厅缺,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布酌住,位于F島的核電站店归,受9級(jí)特大地震影響阎抒,放射性物質(zhì)發(fā)生泄漏酪我。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一且叁、第九天 我趴在偏房一處隱蔽的房頂上張望都哭。 院中可真熱鬧,春花似錦逞带、人聲如沸欺矫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽穆趴。三九已至,卻和暖如春遇汞,著一層夾襖步出監(jiān)牢的瞬間未妹,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來泰國打工空入, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留络它,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓歪赢,卻偏偏與公主長(zhǎng)得像化戳,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子埋凯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容