Skip to content

Instantly share code, notes, and snippets.

View brucetoo's full-sized avatar
🎯
Focusing

Bruce too brucetoo

🎯
Focusing
View GitHub Profile
@brucetoo
brucetoo / RealmModulesExample.java
Created August 18, 2015 12:58
RealmModules的使用
/*
* Copyright 2015 Realm Inc.
*
* 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
@brucetoo
brucetoo / RealmMigrationDemo.java
Created August 19, 2015 03:27
Realm数据库迁移实例
package com.brucetoo.realmdemo.realmmigration;
import android.app.Application;
import android.util.Log;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmMigration;
import io.realm.RealmObject;
import io.realm.internal.ColumnType;
@brucetoo
brucetoo / sp和dp转换成px.java
Created August 19, 2015 12:19
不用Context 将sp和dp转换成px
private int dp2px(float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, getResources().getDisplayMetrics());
}
private int sp2px(float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
dpVal, getResources().getDisplayMetrics());
}
@brucetoo
brucetoo / PullToRefreshRecyclerView.java
Last active September 9, 2015 12:30
PullToRefresh框架支持RecyclerView,且添加了RecyclerView LastItem显示时回调
package com.netease.cc.widget.pulltorefresh;
/**
* Created by N1007-tuyong
* On 9/7/15
* At 14:42
*/
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
@brucetoo
brucetoo / GridRecyclerView.java
Last active November 6, 2021 11:59 — forked from musenkishi/GridRecyclerView
GridRecyclerView. A Grid-specific RecyclerView that can use gridLayoutAnimation. (可以使用GridLayoutAnimation)
/*
* Copyright (C) 2014 Freddie (Musenkishi) Lust-Hed
*
* 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
@brucetoo
brucetoo / Handler.java
Last active November 20, 2015 08:40
解决Handler内存泄露,Drawable导致的内存泄露
package com.bruce.materialapp.activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import java.lang.ref.WeakReference;
@brucetoo
brucetoo / BaseRecyclerViewAdapter.java
Last active October 24, 2015 09:50
RecyclerView支持Footer和Header的BaseRecyclerViewAdapter ,以及使用技巧,特别是在动态添加的时候
package com.do1.flowersapp.common;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
/**
* Created by TuYong N1007
* On 2015/9/11
* At 15:08
* RecyclerView支持Footer和Header
@brucetoo
brucetoo / viewpager.java
Created September 14, 2015 09:44
ViewPager滚动时关闭硬件渲染(特别在ViewPager切换定义了动画时)
特别是在定义了 PageTransformer 来实现Page之间的切换动画
会调用到ViewPaper#setScrollState() -> 然后再调用 ViewPager#enableLayers()
默认就开启了硬件加速,因此在动画执行过程中,会预先执行硬件加速,可能就导致了View动画执行的时候掉帧
可以开启 Show HardWare layers updates 来锁定开启硬件加速的View
//以下是解决办法
@Override
public void onPageScrollStateChanged(int scrollState) {
// A small hack to remove the HW layer that the viewpager add to each page when scrolling.
if (scrollState != ViewPager.SCROLL_STATE_IDLE) {
@brucetoo
brucetoo / Hardware.java
Created September 14, 2015 12:02
硬件加速
硬件加速分四个等级
Application Level:<application android:hardwareAccelerated="true" ...>
Activity Level:<application android:hardwareAccelerated="true">
<activity ... />
<activity android:hardwareAccelerated="false" />
</application>
即使在Application层开启了硬件加速,也可以对单个的Activity选择是否开启硬件加速
Window Level: getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
@brucetoo
brucetoo / CacheUtil.java
Last active September 22, 2015 02:05
文件缓存在data/data路径下的工具,包括常规的增删改查
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;