getStatCpuInfoList(){
42 | return mCpuInfoList;
43 | }
44 | private void dumpCpuInfo() {
45 | BufferedReader cpuReader = null;
46 | BufferedReader pidReader = null;
47 | try {
48 | cpuReader = new BufferedReader(new InputStreamReader(
49 | new FileInputStream("/proc/stat")), 1024);
50 | String cpuRate = cpuReader.readLine();
51 | if (cpuRate == null) {
52 | cpuRate = "";
53 | }
54 | if (mPid < 0) {
55 | mPid = android.os.Process.myPid();
56 | }
57 | pidReader = new BufferedReader(new InputStreamReader(
58 | new FileInputStream("/proc/" + mPid + "/stat")), 1024);
59 | String pidCpuRate = pidReader.readLine();
60 | if (pidCpuRate == null) {
61 | pidCpuRate = "";
62 | }
63 | parseCpuRate(cpuRate, pidCpuRate);
64 | } catch (Throwable ex) {
65 | Log.e(TAG, "doSample: ", ex);
66 | } finally {
67 | try {
68 | if (cpuReader != null) {
69 | cpuReader.close();
70 | }
71 | if (pidReader != null) {
72 | pidReader.close();
73 | }
74 | } catch (IOException e) {
75 | Log.e(TAG, "doSample: ", e);
76 | }
77 | }
78 | }
79 | private void parseCpuRate(String cpuRate, String pidCpuRate) {
80 | String[] cpuInfoArray = cpuRate.split(" ");
81 | if (cpuInfoArray.length < 9) {
82 | return;
83 | }
84 | long user_time = Long.parseLong(cpuInfoArray[2]);
85 | long nice_time = Long.parseLong(cpuInfoArray[3]);
86 | long system_time = Long.parseLong(cpuInfoArray[4]);
87 | long idle_time = Long.parseLong(cpuInfoArray[5]);
88 | long ioWait_time = Long.parseLong(cpuInfoArray[6]);
89 | long total_time = user_time + nice_time + system_time + idle_time + ioWait_time + Long.parseLong(cpuInfoArray[7]) + Long.parseLong(cpuInfoArray[8]);
90 | String[] pidCpuInfos = pidCpuRate.split(" ");
91 | if (pidCpuInfos.length < 17) {
92 | return;
93 | }
94 | long appCpu_time = Long.parseLong(pidCpuInfos[13]) + Long.parseLong(pidCpuInfos[14])
95 | + Long.parseLong(pidCpuInfos[15]) + Long.parseLong(pidCpuInfos[16]);
96 | if (mAppCpuTimePre > 0) {
97 | CpuInfo mCi = new CpuInfo(System.currentTimeMillis());
98 | long idleTime = idle_time - mIdlePre;
99 | long totalTime = total_time - mTotalPre;
100 | mCi.mCpuRate = (totalTime - idleTime) * 100L / totalTime;
101 | mCi.mAppRate = (appCpu_time - mAppCpuTimePre) * 100L / totalTime;
102 | mCi.mSystemRate = (system_time - mSystemPre) * 100L / totalTime;
103 | mCi.mUserRate = (user_time - mUserPre) * 100L / totalTime;
104 | mCi.mIoWait = (ioWait_time - mIoWaitPre) * 100L / totalTime;
105 | synchronized (mCpuInfoList) {
106 | mCpuInfoList.add(mCi);
107 | GLog.d(TAG,"cpu info :" + mCi.toString());
108 | }
109 | }
110 | mUserPre = user_time;
111 | mSystemPre = system_time;
112 | mIdlePre = idle_time;
113 | mIoWaitPre = ioWait_time;
114 | mTotalPre = total_time;
115 | mAppCpuTimePre = appCpu_time;
116 | }
117 | private long mUserPre = 0;
118 | private long mSystemPre = 0;
119 | private long mIdlePre = 0;
120 | private long mIoWaitPre = 0;
121 | private long mTotalPre = 0;
122 | private long mAppCpuTimePre = 0;
123 | }
124 |
--------------------------------------------------------------------------------
/app/src/main/java/com/android/androidtech/ui/ListViewWithViewPager.java:
--------------------------------------------------------------------------------
1 | package com.android.androidtech.ui;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.view.MotionEvent;
6 | import android.view.View;
7 | import android.widget.ListView;
8 |
9 | public class ListViewWithViewPager extends ListView {
10 |
11 | private static final String TAG = "ListViewWithViewPager";
12 | private static final int DISTANCE_X_SCROLL = 10;
13 |
14 | private View mHeaderView = null;
15 | private float mHeaderWidth = 0;
16 | private float mHeaderHeight = 0;
17 |
18 | public ListViewWithViewPager(Context context) {
19 | super(context);
20 | }
21 |
22 | public ListViewWithViewPager(Context context, AttributeSet attrs, int defStyle) {
23 | super(context, attrs, defStyle);
24 |
25 | }
26 |
27 | public ListViewWithViewPager(Context context, AttributeSet attrs) {
28 | super(context, attrs);
29 | }
30 |
31 | /**
32 | * touch事件分发
33 | *
34 | * 在该方法中,将对触控到非HeaderView的事件进行拦截
35 | */
36 | @Override
37 | public boolean onInterceptTouchEvent(MotionEvent ev) {
38 |
39 | // boolean result;
40 | if (ev != null) {
41 |
42 | int action = ev.getAction();
43 | // MLog.d(TAG, "onInterceptTouchEvent action:"+action);
44 |
45 | if (MotionEvent.ACTION_DOWN == action) {
46 |
47 | if (mHeaderView != null) {
48 |
49 | mHeaderWidth = mHeaderView.getWidth();
50 | mHeaderHeight = mHeaderView.getHeight();
51 |
52 | View firstVisibleView = this.getChildAt(getFirstVisiblePosition());
53 | final float firstVisibleViewLeftY;
54 | if (firstVisibleView != null && firstVisibleView == mHeaderView) {
55 |
56 | if (Math.abs(firstVisibleView.getTop()) < mHeaderHeight) {
57 |
58 | firstVisibleViewLeftY = mHeaderHeight - (Math.abs(firstVisibleView.getTop()));
59 |
60 | // MLog.d(TAG,
61 | // "mHeaderWidth :"+mHeaderWidth+" mHeaderHeight:"+mHeaderHeight);
62 | if (mHeaderHeight != 0 && mHeaderWidth != 0) {
63 | // final float downX = ev.getX();
64 | final float downY = ev.getY();
65 | // MLog.d(TAG, "ev X:"+downX+" y:"+downY);
66 | // MLog.d(TAG,
67 | // "firstVisibleViewLeftY:"+firstVisibleViewLeftY);
68 |
69 | // 触控的区域为HeaderView区域,则不拦截事件
70 | if (firstVisibleViewLeftY > downY) {
71 |
72 | // MLog.d(TAG, "ListView不截获Touch事件");
73 |
74 | return false;
75 | }
76 |
77 | }
78 | }
79 | }
80 |
81 | }
82 | }
83 | }
84 |
85 | // MLog.d(TAG, "ListView截获Touch事件");
86 | if (ev != null) {
87 | return super.onInterceptTouchEvent(ev);
88 | }
89 | return false;
90 | }
91 |
92 | @Override
93 | public void addHeaderView(View v, Object data, boolean isSelectable) {
94 | super.addHeaderView(v, data, isSelectable);
95 | handlerView(v);
96 | }
97 |
98 | @Override
99 | public void addHeaderView(View v) {
100 | super.addHeaderView(v);
101 | handlerView(v);
102 | }
103 |
104 | private void handlerView(View v) {
105 | mHeaderView = v;
106 | }
107 |
108 | public View getHandlerHeaderView() {
109 | return mHeaderView;
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/app/src/main/java/com/android/androidtech/ui/MultiCardsView.java:
--------------------------------------------------------------------------------
1 | package com.android.androidtech.ui;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Rect;
6 | import android.graphics.Region;
7 | import android.util.AttributeSet;
8 | import android.view.View;
9 |
10 | import com.android.androidtech.fragment.performance.ui.SingleCard;
11 | import com.android.androidtech.utils.GLog;
12 |
13 | import java.util.ArrayList;
14 |
15 | /**
16 | * Created by yuchengluo on 2015/7/16.
17 | */
18 | public class MultiCardsView extends View{
19 | private ArrayList cardsList = new ArrayList(5);
20 | private boolean enableOverdrawOpt = true;
21 |
22 | public MultiCardsView(Context context) {
23 | this(context, null, 0);
24 | }
25 | public MultiCardsView(Context context, AttributeSet attrs) {
26 | this(context, attrs, 0);
27 | }
28 | public MultiCardsView(Context context, AttributeSet attrs, int defStyleAttr) {
29 | super(context, attrs, defStyleAttr);
30 | }
31 |
32 | public void addCards(SingleCard card) {
33 | cardsList.add(card);
34 | }
35 | //设置是否消除过度绘制
36 | public void enableOverdrawOpt(boolean enableOrNot) {
37 | this.enableOverdrawOpt = enableOrNot;
38 | invalidate();
39 | }
40 | @Override
41 | public void onDraw(Canvas canvas) {
42 | super.onDraw(canvas);
43 | if (cardsList == null || canvas == null)
44 | return;
45 | Rect clip = canvas.getClipBounds();
46 | GLog.d("draw", String.format("clip bounds %d %d %d %d", clip.left, clip.top, clip.right, clip.bottom));
47 | //根据enableOverdrawOpt值来调用不同的绘制方法,对比效果
48 | if (enableOverdrawOpt) {
49 | drawCardsWithotOverDraw(canvas, cardsList.size() - 1);
50 | } else {
51 | drawCardsNormal(canvas, cardsList.size() - 1);
52 | }
53 | }
54 | //没有过度绘制的实现
55 | protected void drawCardsWithotOverDraw(Canvas canvas, int index) {
56 | if (canvas == null || index < 0 || index >= cardsList.size())
57 | return;
58 | SingleCard card = cardsList.get(index);
59 | //判断是否没和某个卡片相交,从而跳过那些非矩形区域内的绘制操作
60 | if (card != null && !canvas.quickReject(card.area, Canvas.EdgeType.BW)) {
61 | int saveCount = canvas.save(Canvas.CLIP_SAVE_FLAG);
62 | //只绘制可见区域
63 | if (canvas.clipRect(card.area, Region.Op.DIFFERENCE)) {
64 | drawCardsWithotOverDraw(canvas, index - 1);
65 | }
66 | canvas.restoreToCount(saveCount);
67 | saveCount = canvas.save(Canvas.CLIP_SAVE_FLAG);
68 | //只绘制可见区域
69 | if (canvas.clipRect(card.area)) {
70 | GLog.d("draw", "overdraw opt: draw cards index: " + index);
71 | Rect clip = canvas.getClipBounds();
72 | GLog.d("draw", String.format("current clip bounds %d %d %d %d", clip.left, clip.top, clip.right, clip.bottom));
73 | card.draw(canvas);
74 | }
75 | canvas.restoreToCount(saveCount);
76 | }else{
77 | drawCardsWithotOverDraw(canvas, index - 1);
78 | }
79 | }
80 | //普通绘制
81 | protected void drawCardsNormal(Canvas canvas, int index) {
82 | if (canvas == null || index < 0 || index >= cardsList.size())
83 | return;
84 | SingleCard card = cardsList.get(index);
85 | if (card != null) {
86 | drawCardsNormal(canvas, index - 1);
87 | GLog.d("draw", "draw cards index: " + index);
88 | card.draw(canvas);
89 | }
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/app/src/main/java/com/android/androidtech/ui/TopBar.java:
--------------------------------------------------------------------------------
1 | package com.android.androidtech.ui;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.widget.LinearLayout;
8 |
9 | import com.android.androidtech.R;
10 |
11 | /**
12 | * Created by yuchengluo on 2016/3/17.
13 | */
14 | public class TopBar extends LinearLayout{
15 | public TopBar(Context context) {
16 | this(context, null);
17 | }
18 |
19 | public TopBar(Context context, AttributeSet attrs) {
20 | super(context, attrs);
21 | InitView(context);
22 | }
23 |
24 | public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
25 | super(context, attrs, defStyleAttr);
26 | InitView(context);
27 | }
28 |
29 | public TopBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
30 | super(context, attrs, defStyleAttr, defStyleRes);
31 | InitView(context);
32 | }
33 | private void InitView(Context ctx){
34 | View view = LayoutInflater.from(ctx).inflate(R.layout.common_top_bar, this, true);
35 | }
36 |
37 | @Override
38 | public boolean isInEditMode() {
39 | return super.isInEditMode();
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/app/src/main/java/com/android/androidtech/utils/CpuFreqSet.java:
--------------------------------------------------------------------------------
1 | package com.android.androidtech.utils;
2 |
3 | import java.io.DataInputStream;
4 | import java.io.DataOutputStream;
5 | import java.io.IOException;
6 | import java.util.ArrayList;
7 |
8 | /**
9 | * Created by yuchengluo on 2016/7/5.
10 | */
11 |
12 | public class CpuFreqSet {
13 | public class CPUFreqSetting {
14 | /**
15 | * cpu cat命令大全
16 | * cat [%cpuFreqPath%]/cpuinfo_cur_freq (当前cpu频率)
17 | * cat [%cpuFreqPath%]/cpuinfo_max_freq (最大cpu频率)
18 | * cat [%cpuFreqPath%]/cpuinfo_min_freq (最小cpu频率)
19 | * cat [%cpuFreqPath%]/related_cpus (cpu数量标号,从0开始,如果是双核,结果为0,1)
20 | * cat [%cpuFreqPath%]/scaling_available_frequencies (cpu所有可用频率)
21 | * cat [%cpuFreqPath%]/scaling_available_governors (cpu所有可用调控模式)
22 | * cat [%cpuFreqPath%]/scaling_available_governors (cpu所有可用调控模式)
23 | * cat [%cpuFreqPath%]/scaling_cur_freq (?????)
24 | * cat [%cpuFreqPath%]/scaling_driver (?????)
25 | * cat [%cpuFreqPath%]/scaling_governor (?????)
26 | * cat [%cpuFreqPath%]/scaling_max_freq (?????)
27 | * cat [%cpuFreqPath%]/scaling_min_freq (?????)
28 | * cat [%cpuFreqPath%]/scaling_setspeed (?????)
29 | * cat [%cpuFreqPath%]/cpuinfo_transition_latency (?????)
30 | */
31 | private final String TAG = "CpuFreqSet";
32 | private final String cpuFreqPath = "/sys/devices/system/cpu/cpu0/cpufreq";
33 | // private final static String PERFORMANCE_GOVERNOR = "performance";
34 | // private final static String POWER_SAVE_GOVERNOR = "performance";
35 | // private final static String ONDEMAND_GOVERNOR = "performance";
36 | // private final static String CONSERVATIVE_GOVERNOR = "performance";
37 | // private final static String USERSAPCE_GOVERNOR = "performance";
38 | /**
39 | * 获取当前CPU调控模式
40 | */
41 | public void getCpuCurGovernor() {
42 | try {
43 | DataInputStream is = null;
44 | Process process = Runtime.getRuntime().exec("cat " + cpuFreqPath + "/scaling_governor");
45 | is = new DataInputStream(process.getInputStream());
46 | String line = is.readLine();
47 | } catch (IOException e) {
48 | e.printStackTrace();
49 | }
50 | }
51 |
52 | /**
53 | * 设置CPU调控模式
54 | */
55 | private boolean writeCpuGovernor(String governor) {
56 | DataOutputStream os = null;
57 | byte[] buffer = new byte[256];
58 | String command = "echo " + governor + " > " + cpuFreqPath + "/scaling_governor";
59 | try {
60 | Process process = Runtime.getRuntime().exec("su");
61 | os = new DataOutputStream(process.getOutputStream());
62 | os.writeBytes(command + "\n");
63 | os.writeBytes("exit\n");
64 | os.flush();
65 | process.waitFor();
66 | } catch (IOException e) {
67 | return false;
68 | } catch (InterruptedException e) {
69 | e.printStackTrace();
70 | }
71 | return true;
72 | }
73 |
74 | /**
75 | * 获得CPU所有调控模式
76 | * @return
77 | */
78 | private ArrayList readCpuGovernors() {
79 | ArrayList governors = new ArrayList();
80 | DataInputStream is = null;
81 | try {
82 | Process process = Runtime.getRuntime().exec("cat " + cpuFreqPath + "/scaling_available_governors");
83 | is = new DataInputStream(process.getInputStream());
84 | String line = is.readLine();
85 |
86 | String[] strs = line.split(" ");
87 | for (int i = 0; i < strs.length; i++)
88 | governors.add(strs[i]);
89 | } catch (IOException e) {
90 | }
91 | return governors;
92 | }
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/app/src/main/java/com/android/androidtech/utils/GLog.java:
--------------------------------------------------------------------------------
1 | package com.android.androidtech.utils;
2 |
3 | import android.util.Log;
4 |
5 | /**
6 | * Created by yuchengluo on 2015/6/26.
7 | */
8 | public class GLog {
9 | private final static String TAG = "YCLog";
10 | public static void d(String tag,String value){
11 | Log.d(tag,value);
12 | }
13 |
14 | public static void e(String tag,String value){
15 | Log.e(tag, value);
16 | }
17 |
18 | public static void w(String tag,String value){
19 | Log.w(tag, value);
20 | }
21 |
22 | public static void i(String tag,String value){
23 | Log.i(tag, value);
24 | }
25 | public static void e(String tag,Throwable t){
26 | e(tag,"",t);
27 | }
28 | public static void e(String tag, String info, Throwable t) {
29 | try {
30 | if (tag != null && info != null && t != null) {
31 | e(tag, info + "\n" + Log.getStackTraceString(t));
32 | }
33 | } catch (Exception e) {
34 | GLog.e(TAG, e.getMessage());
35 | }
36 |
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/android/androidtech/utils/Util4Common.java:
--------------------------------------------------------------------------------
1 | package com.android.androidtech.utils;
2 |
3 | import android.view.View;
4 | import android.view.ViewParent;
5 |
6 | import java.lang.reflect.Field;
7 |
8 | /**
9 | * Created by yuchengluo on 2015/6/26.
10 | */
11 | public class Util4Common {
12 | public static boolean findView(View viewParent, Object findView) {
13 | if (viewParent == null || findView == null) {
14 | return false;
15 | }
16 |
17 | if (findView == viewParent) {
18 | return true;
19 | }
20 |
21 | boolean result = false;
22 | if (findView instanceof View) {
23 | View view = (View) findView;
24 | ViewParent parent = view.getParent();
25 | while (parent != null) {
26 | if (parent == viewParent) {
27 | result = true;
28 | break;
29 | }
30 |
31 | parent = parent.getParent();
32 | }
33 | }
34 | return result;
35 | }
36 | public static Field getgetObjectField(Object obj, String fieldName){
37 | java.lang.reflect.Field field = null;
38 | try {
39 |
40 | Class parentClass = obj.getClass();
41 | while (field == null) {
42 | try {
43 | field = parentClass.getDeclaredField(fieldName);
44 | } catch (Throwable e) {
45 | e.printStackTrace();
46 | }
47 |
48 | try {
49 | parentClass = parentClass.getSuperclass();
50 | if (parentClass == null) {
51 | break;
52 | }
53 | } catch (Throwable e) {
54 | e.printStackTrace();
55 | }
56 |
57 | }
58 |
59 | if (field != null && !field.isAccessible()) {
60 | field.setAccessible(true);
61 | }
62 | } catch (Throwable e) {
63 | e.printStackTrace();
64 | }
65 |
66 | return field;
67 | }
68 | public static Object getObjectFieldValue(Object obj, String fieldName) {
69 | Object result = null;
70 | try {
71 | java.lang.reflect.Field field = getgetObjectField(obj, fieldName);
72 | if (field != null) {
73 | field.setAccessible(true);
74 | result = field.get(obj);
75 | }
76 | } catch (Throwable e) {
77 | e.printStackTrace();
78 | }
79 |
80 | return result;
81 | }
82 | public static boolean setObjectField(Object obj, String fieldName, Object newValue) {
83 | boolean result = false;
84 | try {
85 | Field field = getgetObjectField(obj,fieldName);
86 | field.setAccessible(true);
87 | field.set(obj, newValue);
88 | result = true;
89 | } catch (Throwable e) {
90 | e.printStackTrace();
91 | }
92 |
93 | return result;
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/app/src/main/java/com/android/androidtech/utils/Util4Phone.java:
--------------------------------------------------------------------------------
1 | package com.android.androidtech.utils;
2 |
3 | import android.os.Build;
4 |
5 | /**
6 | * Created by yuchengluo on 2015/6/26.
7 | */
8 | public class Util4Phone {
9 |
10 | /**
11 | *
12 | * @Discription:TODO
13 | * @return
14 | */
15 | public static boolean isSupportAnimation() {
16 | return android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
17 | }
18 | }
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/android/androidtech/utils/dex/DexUtil.java:
--------------------------------------------------------------------------------
1 | package com.android.androidtech.utils.dex;
2 |
3 | import android.text.TextUtils;
4 |
5 | import java.io.File;
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | import dalvik.system.PathClassLoader;
10 |
11 | /**
12 | * Created by yuchengluo on 2015/10/30.
13 | */
14 | public class DexUtil {
15 | public static boolean loadDexToClassLoader(String dexFilePath, String libPath, String optimizedDirectoryPath,
16 | PathClassLoader classLoader) {
17 | boolean result = false;
18 | if (TextUtils.isEmpty(dexFilePath) || classLoader == null) {
19 | return false;
20 | }
21 |
22 | try {
23 | List additionalClassPathEntries = new ArrayList();
24 | additionalClassPathEntries.add(new File(dexFilePath));
25 | MultiDex.installSecondaryDexes(classLoader, new File(optimizedDirectoryPath), additionalClassPathEntries);
26 | result = true;
27 | } catch (Throwable e) {
28 | e.printStackTrace();
29 | }
30 | return result;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/app/src/main/java/com/android/androidtech/utils/dex/ReflectUtil.java:
--------------------------------------------------------------------------------
1 | package com.android.androidtech.utils.dex;
2 |
3 | import java.lang.reflect.Field;
4 | import java.lang.reflect.Method;
5 | import java.util.Arrays;
6 |
7 | /**
8 | * Created by yuchengluo on 2015/10/30.
9 | */
10 | public class ReflectUtil {
11 |
12 | public static boolean setObjectField(Object obj, String fieldName, Object newValue) {
13 | boolean result = false;
14 | try {
15 | Field field = obj.getClass().getDeclaredField(fieldName);
16 | field.setAccessible(true);
17 | field.set(obj, newValue);
18 | result = true;
19 | } catch (Throwable e) {
20 | // e.printStackTrace();
21 | }
22 |
23 | return result;
24 | }
25 |
26 | public static Method getObjectMethod(Object obj, String methodName) {
27 | try {
28 | Method[] methods = obj.getClass().getDeclaredMethods();
29 | if (methods != null) {
30 | for (Method method : methods) {
31 | if (method != null && method.getName().equals(methodName)) {
32 | return method;
33 | }
34 | }
35 | }
36 | } catch (Throwable e) {
37 | }
38 |
39 | return null;
40 | }
41 |
42 | public static Method getObjectMethodByArgs(Object instance, String name, Class... parameterTypes)
43 | throws NoSuchMethodException {
44 | Class clazz = instance.getClass();
45 |
46 | while (clazz != null) {
47 | try {
48 | Method method = clazz.getDeclaredMethod(name, parameterTypes);
49 | if (!method.isAccessible()) {
50 | method.setAccessible(true);
51 | }
52 |
53 | return method;
54 | } catch (NoSuchMethodException var5) {
55 | clazz = clazz.getSuperclass();
56 | }
57 | }
58 | throw new NoSuchMethodException("Method " + name + " with parameters " + Arrays.asList(parameterTypes)
59 | + " not found in " + instance.getClass());
60 | }
61 |
62 | public static Field getObjectField(Object obj, String fieldName) {
63 | Field field = null;
64 | try {
65 |
66 | Class parentClass = obj.getClass();
67 | while (field == null) {
68 | try {
69 | field = parentClass.getDeclaredField(fieldName);
70 | } catch (Throwable e) {
71 | e.printStackTrace();
72 | }
73 |
74 | try {
75 | parentClass = parentClass.getSuperclass();
76 | if (parentClass == null) {
77 | break;
78 | }
79 | } catch (Throwable e) {
80 | e.printStackTrace();
81 | }
82 |
83 | }
84 |
85 | if (field != null && !field.isAccessible()) {
86 | field.setAccessible(true);
87 | }
88 | } catch (Throwable e) {
89 | e.printStackTrace();
90 | }
91 |
92 | return field;
93 | }
94 |
95 | public static Object getObjectFieldValue(Object obj, String fieldName) {
96 | Object result = null;
97 | try {
98 | Field field = getObjectField(obj, fieldName);
99 | if (field != null) {
100 | field.setAccessible(true);
101 | result = field.get(obj);
102 | }
103 | } catch (Throwable e) {
104 | e.printStackTrace();
105 | }
106 |
107 | return result;
108 | }
109 |
110 | public static Object invoke(Object obj, String methodName, Object[] args) {
111 | Object result = null;
112 | try {
113 | Method method = getObjectMethod(obj, methodName);
114 | if (method != null) {
115 | method.setAccessible(true);
116 | result = method.invoke(obj, args);
117 | }
118 | } catch (Throwable e) {
119 | e.printStackTrace();
120 | }
121 |
122 | return result;
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/app/src/main/java/com/android/androidtech/utils/dex/ZipUtil.java:
--------------------------------------------------------------------------------
1 | package com.android.androidtech.utils.dex;
2 |
3 | import java.io.File;
4 | import java.io.IOException;
5 | import java.io.RandomAccessFile;
6 | import java.util.zip.CRC32;
7 | import java.util.zip.ZipException;
8 |
9 | /**
10 | * Created by yuchengluo on 2015/10/30.
11 | */
12 | public class ZipUtil {
13 |
14 | static class CentralDirectory {
15 | long offset;
16 | long size;
17 | }
18 |
19 | /* redefine those constant here because of bug 13721174 preventing to compile using the
20 | * constants defined in ZipFile */
21 | private static final int ENDHDR = 22;
22 | private static final int ENDSIG = 0x6054b50;
23 |
24 | /**
25 | * Size of reading buffers.
26 | */
27 | private static final int BUFFER_SIZE = 0x4000;
28 |
29 | /**
30 | * Compute crc32 of the central directory of an apk. The central directory contains
31 | * the crc32 of each entries in the zip so the computed result is considered valid for the whole
32 | * zip file. Does not support zip64 nor multidisk but it should be OK for now since ZipFile does
33 | * not either.
34 | */
35 | static long getZipCrc(File apk) throws IOException {
36 | RandomAccessFile raf = new RandomAccessFile(apk, "r");
37 | try {
38 | CentralDirectory dir = findCentralDirectory(raf);
39 |
40 | return computeCrcOfCentralDir(raf, dir);
41 | } finally {
42 | raf.close();
43 | }
44 | }
45 |
46 | /* Package visible for testing */
47 | static CentralDirectory findCentralDirectory(RandomAccessFile raf) throws IOException,
48 | ZipException {
49 | long scanOffset = raf.length() - ENDHDR;
50 | if (scanOffset < 0) {
51 | throw new ZipException("File too short to be a zip file: " + raf.length());
52 | }
53 |
54 | long stopOffset = scanOffset - 0x10000 /* ".ZIP file comment"'s max length */;
55 | if (stopOffset < 0) {
56 | stopOffset = 0;
57 | }
58 |
59 | int endSig = Integer.reverseBytes(ENDSIG);
60 | while (true) {
61 | raf.seek(scanOffset);
62 | if (raf.readInt() == endSig) {
63 | break;
64 | }
65 |
66 | scanOffset--;
67 | if (scanOffset < stopOffset) {
68 | throw new ZipException("End Of Central Directory signature not found");
69 | }
70 | }
71 | // Read the End Of Central Directory. ENDHDR includes the signature
72 | // bytes,
73 | // which we've already read.
74 |
75 | // Pull out the information we need.
76 | raf.skipBytes(2); // diskNumber
77 | raf.skipBytes(2); // diskWithCentralDir
78 | raf.skipBytes(2); // numEntries
79 | raf.skipBytes(2); // totalNumEntries
80 | CentralDirectory dir = new CentralDirectory();
81 | dir.size = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL;
82 | dir.offset = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL;
83 | return dir;
84 | }
85 |
86 | /* Package visible for testing */
87 | static long computeCrcOfCentralDir(RandomAccessFile raf, CentralDirectory dir)
88 | throws IOException {
89 | CRC32 crc = new CRC32();
90 | long stillToRead = dir.size;
91 | raf.seek(dir.offset);
92 | int length = (int) Math.min(BUFFER_SIZE, stillToRead);
93 | byte[] buffer = new byte[BUFFER_SIZE];
94 | length = raf.read(buffer, 0, length);
95 | while (length != -1) {
96 | crc.update(buffer, 0, length);
97 | stillToRead -= length;
98 | if (stillToRead == 0) {
99 | break;
100 | }
101 | length = (int) Math.min(BUFFER_SIZE, stillToRead);
102 | length = raf.read(buffer, 0, length);
103 | }
104 | return crc.getValue();
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/hp_button_right.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_red_dot.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/img_top_back.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_app_start.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
14 |
19 |
27 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_homepage.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
14 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_layout_per.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
11 |
21 |
32 |
41 |
42 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/common_top_bar.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
13 |
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fm_listview.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fm_overdraw.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
20 |
21 |
22 |
23 |
29 |
30 |
35 |
36 |
41 |
42 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fm_performance.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
21 |
22 |
28 |
34 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fm_ui_perf.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
19 |
25 |
26 |
32 |
33 |
39 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fm_xml_show.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
12 |
20 |
28 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_homepage.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
19 |
20 |
25 |
26 |
36 |
37 |
38 |
42 |
43 |
46 |
47 |
57 |
58 |
68 |
69 |
70 |
81 |
82 |
86 |
87 |
97 |
98 |
108 |
109 |
110 |
114 |
115 |
121 |
122 |
123 |
128 |
129 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_test_sec.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_third.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragmet_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_listview_tag.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
12 |
24 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_listview_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
14 |
15 |
21 |
22 |
29 |
30 |
31 |
39 |
40 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_bitmap_show.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
15 |
16 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/list.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/list_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/view_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/viewstub_text_layout1.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
14 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_layout_per.xml:
--------------------------------------------------------------------------------
1 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/activity_back_normal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/app/src/main/res/mipmap-xhdpi/activity_back_normal.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/activity_back_pressed.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/app/src/main/res/mipmap-xhdpi/activity_back_pressed.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/app/src/main/res/mipmap-xhdpi/logo.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/main_bg.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/app/src/main/res/mipmap-xhdpi/main_bg.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/maintabbar_button_search_highlight.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/app/src/main/res/mipmap-xhdpi/maintabbar_button_search_highlight.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/maintabbar_button_search_normal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/app/src/main/res/mipmap-xhdpi/maintabbar_button_search_normal.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/maintabbar_button_search_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/test3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/app/src/main/res/mipmap-xhdpi/test3.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #66000000
4 | #FFFFFFFF
5 | #ff0000
6 | #0000ff
7 | #ffff00
8 | #00ff00
9 | #000000
10 | #00000000
11 | #CF000000
12 | #E0000000
13 | #F8F8F8
14 | #ABABAB
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 210dip
5 | 96dip
6 | 50dip
7 | 48dip
8 | 60dip
9 |
10 | 16dp
11 | 16dp
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | 性能优化示例
3 | 启动监控
4 | 停止监控
5 | AppStartActivity
6 | Dummy Button
7 | DUMMY\nCONTENT
8 | Tab1
9 | Tab2
10 | Tab3
11 | LayoutPerActivity
12 |
13 | Hello world!
14 | Settings
15 | account_auth_provider
16 |
17 | Hello world!
18 | Settings
19 | account_auth_provider
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
14 |
15 |
16 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | maven {
6 | url "http://maven.oa.com/nexus/content/repositories/android"
7 | }
8 | maven {
9 | url "http://maven.oa.com/nexus/content/repositories/thirdparty"
10 | }
11 | maven {
12 | url "http://maven.oa.com/nexus/content/repositories/thirdparty-snapshots"
13 | }
14 | //jcenter()
15 | }
16 | dependencies {
17 | classpath 'com.android.tools.build:gradle:1.2.3'
18 |
19 | // NOTE: Do not place your application dependencies here; they belong
20 | // in the individual module build.gradle files
21 | }
22 | }
23 |
24 | allprojects {
25 | repositories {
26 | maven {
27 | url "http://maven.oa.com/nexus/content/repositories/android"
28 | }
29 | maven {
30 | url "http://maven.oa.com/nexus/content/repositories/thirdparty"
31 | }
32 | maven {
33 | url "http://maven.oa.com/nexus/content/repositories/thirdparty-snapshots"
34 | }
35 | // jcenter()
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | ## Project-wide Gradle settings.
2 | #
3 | # For more details on how to configure your build environment visit
4 | # http://www.gradle.org/docs/current/userguide/build_environment.html
5 | #
6 | # Specifies the JVM arguments used for the daemon process.
7 | # The setting is particularly useful for tweaking memory settings.
8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m
9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
10 | #
11 | # When configured, Gradle will run in incubating parallel mode.
12 | # This option should only be used with decoupled projects. More details, visit
13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
14 | # org.gradle.parallel=true
15 | #Mon Jun 27 15:56:41 CST 2016
16 | systemProp.http.proxyHost=web-proxyhk.oa.com
17 | systemProp.http.proxyPort=8080
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lyc7898/AndroidTech/e34b6cf8dfb598ca3fdead27a96f4d035bb714ab/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 10 15:27:10 PDT 2013
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/miniimageloder/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/miniimageloder/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.1"
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | compile 'com.android.support:support-annotations:22.0.0'
23 | }
24 |
--------------------------------------------------------------------------------
/miniimageloder/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\DevSoft\adt-bundle-windows-x86-20140702\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/miniimageloder/src/androidTest/java/com/android/miniimageloader/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.android.miniimageloader;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/miniimageloder/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/miniimageloder/src/main/java/com/android/miniimageloader/ImageLoader.java:
--------------------------------------------------------------------------------
1 | package com.android.miniimageloader;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.content.res.Resources;
6 | import android.graphics.Bitmap;
7 | import android.graphics.BitmapFactory;
8 | import android.graphics.drawable.BitmapDrawable;
9 | import android.graphics.drawable.Drawable;
10 | import android.media.Image;
11 | import android.os.Build;
12 | import android.widget.ImageView;
13 |
14 | import com.android.miniimageloader.cache.ImageCache;
15 | import com.android.miniimageloader.config.BitmapConfig;
16 |
17 | import java.io.InputStream;
18 | import java.lang.ref.WeakReference;
19 |
20 | /**
21 | * Created by yuchengluo on 2016/5/5.
22 | */
23 | public abstract class ImageLoader {
24 | private boolean mExitTasksEarly = false; // 是否提前退出的标志
25 | protected boolean mPauseWork = false;
26 | private final Object mPauseWorkLock = new Object();
27 | public final String TAG = "ImageLoader";
28 |
29 | protected ImageLoader() {
30 | Init();
31 | }
32 |
33 | private void Init() {
34 | }
35 |
36 | public void loadImage(String url, ImageView imageView, BitmapConfig bmConfig) {
37 | if (url == null) {
38 | return;
39 | }
40 | Bitmap bitmap = getmImageCache().getBitmap(url);;
41 | if (bitmap != null) {
42 | setImageBitmap(imageView,bitmap);
43 | }
44 | //否则走加载
45 | else {
46 |
47 | final BitmapLoadTask task = new BitmapLoadTask(url, imageView, bmConfig);
48 |
49 | task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR);
50 | }
51 | }
52 |
53 | private class BitmapLoadTask extends
54 | AsyncTask {
55 | private String mUrl;
56 | private BitmapConfig mBitmapConfig = null;
57 | private final WeakReference imageViewReference;
58 |
59 | public BitmapLoadTask(String url, ImageView imageView, BitmapConfig bmConfig) {
60 | mUrl = url;
61 | imageViewReference = new WeakReference(imageView);
62 | mBitmapConfig = bmConfig;
63 | }
64 |
65 | @Override
66 | protected Bitmap doInBackground(Void... params) {
67 | Bitmap bitmap = null;
68 | synchronized (mPauseWorkLock) {
69 | while (mPauseWork && !isCancelled()) {
70 | try {
71 | mPauseWorkLock.wait();
72 | } catch (InterruptedException e) {
73 | e.printStackTrace();
74 | }
75 | }
76 | }
77 | if (bitmap == null && !isCancelled()
78 | && imageViewReference.get() != null && !mExitTasksEarly) {
79 | bitmap = getmImageCache().getBitmapFromDisk(mUrl, mBitmapConfig);
80 | }
81 |
82 | if (bitmap == null && !isCancelled()
83 | && imageViewReference.get() != null && !mExitTasksEarly) {
84 | bitmap = downLoadBitmap(mUrl, mBitmapConfig);
85 | }
86 | if (bitmap != null) {
87 | getmImageCache().addToCache(mUrl, bitmap);
88 | }
89 |
90 | return bitmap;
91 | }
92 |
93 | @Override
94 | protected void onPostExecute(Bitmap result) {
95 | if (isCancelled() || mExitTasksEarly) {
96 | result = null;
97 | }
98 |
99 | ImageView imageView = imageViewReference.get();
100 | if (result != null && imageView != null) {
101 | setImageBitmap(imageView, result);
102 | }
103 | }
104 |
105 | @Override
106 | protected void onCancelled(Bitmap value) {
107 | super.onCancelled(value);
108 | synchronized (mPauseWorkLock) {
109 | mPauseWorkLock.notifyAll();
110 | }
111 | }
112 | }
113 |
114 | /**
115 | * @param imageView
116 | * @param drawable
117 | */
118 | private void setImageBitmap(ImageView imageView, Bitmap bitmap) {
119 | imageView.setImageBitmap(bitmap);
120 | }
121 |
122 | public void setPauseWork(boolean pauseWork) {
123 | synchronized (mPauseWorkLock) {
124 | mPauseWork = pauseWork;
125 | if (!mPauseWork) {
126 | mPauseWorkLock.notifyAll();
127 | }
128 | }
129 | }
130 |
131 | public void setExitTasksEarly(boolean exitTasksEarly) {
132 | mExitTasksEarly = exitTasksEarly;
133 | setPauseWork(false);
134 | }
135 |
136 | /**
137 | * 下载
138 | *
139 | * @param url
140 | * @return
141 | */
142 | protected abstract Bitmap downLoadBitmap(String url, BitmapConfig bmConfig);
143 |
144 | protected abstract ImageCache getmImageCache();
145 | }
146 |
--------------------------------------------------------------------------------
/miniimageloder/src/main/java/com/android/miniimageloader/MiniImageLoader.java:
--------------------------------------------------------------------------------
1 | package com.android.miniimageloader;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.util.Log;
7 |
8 | import com.android.miniimageloader.cache.ImageCache;
9 | import com.android.miniimageloader.config.BitmapConfig;
10 | import com.android.miniimageloader.utils.BitmapUtil;
11 | import com.android.miniimageloader.utils.CloseUtil;
12 |
13 | import java.io.IOException;
14 | import java.io.InputStream;
15 | import java.net.HttpURLConnection;
16 | import java.net.URL;
17 |
18 | /**
19 | * Created by yuchengluo on 2016/4/29.
20 | */
21 | public class MiniImageLoader extends ImageLoader{
22 | private volatile static MiniImageLoader miniImageLoader = null;
23 | private ImageCache mImageCache = null;
24 | static private Context mContext = null;
25 | public static MiniImageLoader getInstance(){
26 | if(null == miniImageLoader){
27 | synchronized (MiniImageLoader.class){
28 | miniImageLoader = new MiniImageLoader();
29 | }
30 | }
31 | return miniImageLoader;
32 | }
33 | public static void progrem(Context context){
34 | mContext = context;
35 | }
36 | public MiniImageLoader(){
37 | mImageCache = new ImageCache(mContext);
38 | }
39 | @Override
40 | public Bitmap downLoadBitmap(String urlString,BitmapConfig bmConfig) {
41 | HttpURLConnection urlConnection = null;
42 | InputStream in = null;
43 |
44 | try {
45 | final URL url = new URL(urlString);
46 | urlConnection = (HttpURLConnection) url.openConnection();
47 | in = urlConnection.getInputStream();
48 | getmImageCache().saveToDisk(urlString,in);
49 | final BitmapFactory.Options options = bmConfig.getBitmapOptions(in);
50 | in.close();
51 | urlConnection.disconnect();
52 | urlConnection = (HttpURLConnection) url.openConnection();
53 | in = urlConnection.getInputStream();
54 | Bitmap bitmap = BitmapUtil.decodeSampledBitmapFromStream(in, options,getmImageCache());
55 | return bitmap;
56 |
57 | } catch (final IOException e) {
58 | Log.e("text", "Error in downloadBitmap - " + e);
59 | } finally {
60 | if (urlConnection != null) {
61 | urlConnection.disconnect();
62 | }
63 | CloseUtil.closeQuietly(in);
64 | }
65 | return null;
66 | }
67 |
68 | @Override
69 | protected ImageCache getmImageCache() {
70 | if(null == mImageCache){
71 | mImageCache = new ImageCache(mContext);
72 | }
73 | return mImageCache;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/miniimageloder/src/main/java/com/android/miniimageloader/cache/ImageCache.java:
--------------------------------------------------------------------------------
1 | package com.android.miniimageloader.cache;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.graphics.Bitmap;
6 | import android.graphics.BitmapFactory;
7 | import android.graphics.drawable.BitmapDrawable;
8 | import android.os.Build;
9 | import android.os.Bundle;
10 | import android.util.Log;
11 | import android.util.LruCache;
12 |
13 | import com.android.miniimageloader.config.BitmapConfig;
14 | import com.android.miniimageloader.config.MiniImageLoaderConfig;
15 |
16 | import java.io.InputStream;
17 | import java.lang.ref.SoftReference;
18 | import java.util.Collections;
19 | import java.util.HashSet;
20 | import java.util.Iterator;
21 | import java.util.Set;
22 |
23 | /**
24 | * Created by yuchengluo on 2016/4/29.
25 | */
26 | public class ImageCache {
27 | private MemoryCache mMemoryCache;
28 | private DiskCache mDiskCache;
29 | public ImageCache(Context ctx){
30 | mMemoryCache = new MemoryCache(0.4f);
31 | mDiskCache = new DiskCache(ctx, MiniImageLoaderConfig.DEFAULT_DISK_CACHE_SIZE);
32 | }
33 | public Bitmap getBitmap(String url){
34 | Bitmap bitmap = mMemoryCache.getBitmap(url);
35 |
36 | return bitmap;
37 | }
38 | public void addToCache(String url,Bitmap bitmap){
39 | mMemoryCache.addBitmapToCache(url,bitmap);
40 | }
41 | public Bitmap getBitmapFromDisk(String url,BitmapConfig config){
42 | return mDiskCache.getBitmapFromDiskCache(url,config);
43 | }
44 | public void saveToDisk(String url,InputStream is){
45 | mDiskCache.saveToDisk(url,is);
46 | }
47 | public Bitmap getBitmapFromReusableSet(BitmapFactory.Options options){
48 | return mMemoryCache.getBitmapFromReusableSet(options);
49 | }
50 | }
--------------------------------------------------------------------------------
/miniimageloder/src/main/java/com/android/miniimageloader/cache/MemoryCache.java:
--------------------------------------------------------------------------------
1 | package com.android.miniimageloader.cache;
2 |
3 | import android.annotation.TargetApi;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.graphics.drawable.BitmapDrawable;
7 | import android.os.Build;
8 | import android.util.Log;
9 | import android.util.LruCache;
10 |
11 | import com.android.miniimageloader.utils.MLog;
12 |
13 | import java.lang.ref.SoftReference;
14 | import java.util.Collections;
15 | import java.util.HashSet;
16 | import java.util.Iterator;
17 | import java.util.Set;
18 |
19 | /**
20 | * Created by yuchengluo on 2016/5/12.
21 | */
22 | public class MemoryCache{
23 | private final int DEFAULT_MEM_CACHE_SIZE = 1024 * 12;
24 | private LruCache mMemoryCache;
25 | private Set> mReusableBitmaps;
26 | private final String TAG = "MemoryCache";
27 |
28 | public MemoryCache(float sizePer){
29 | Init(sizePer);
30 | }
31 | private void Init(float sizePer){
32 | int cacheSize = DEFAULT_MEM_CACHE_SIZE;
33 | if(sizePer> 0){
34 | cacheSize = Math.round(sizePer * Runtime.getRuntime().maxMemory() / 1024);
35 | }
36 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
37 | mReusableBitmaps =
38 | Collections.synchronizedSet(new HashSet>());
39 | }
40 | mMemoryCache = new LruCache(cacheSize){
41 | @Override
42 | protected int sizeOf(String key, Bitmap value) {
43 | final int bitmapSize = getBitmapSize(value) / 1024;
44 | return bitmapSize == 0 ? 1 : bitmapSize;
45 | }
46 |
47 | @Override
48 | protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
49 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
50 | mReusableBitmaps.add(new SoftReference(oldValue));
51 | }
52 | }
53 | };
54 | }
55 |
56 | @TargetApi(Build.VERSION_CODES.KITKAT)
57 | public int getBitmapSize(Bitmap bitmap) {
58 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
59 | return bitmap.getAllocationByteCount();
60 | }
61 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
62 | return bitmap.getByteCount();
63 | }
64 | return bitmap.getRowBytes() * bitmap.getHeight();
65 | }
66 | @TargetApi(Build.VERSION_CODES.KITKAT)
67 | private static boolean canUseForInBitmap(
68 | Bitmap candidate, BitmapFactory.Options targetOptions) {
69 |
70 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
71 | return candidate.getWidth() == targetOptions.outWidth
72 | && candidate.getHeight() == targetOptions.outHeight
73 | && targetOptions.inSampleSize == 1;
74 | }
75 | int width = targetOptions.outWidth / targetOptions.inSampleSize;
76 | int height = targetOptions.outHeight / targetOptions.inSampleSize;
77 |
78 | int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
79 |
80 | return byteCount <= candidate.getAllocationByteCount();
81 | }
82 | private static int getBytesPerPixel(Bitmap.Config config) {
83 | if (config == Bitmap.Config.ARGB_8888) {
84 | return 4;
85 | } else if (config == Bitmap.Config.RGB_565) {
86 | return 2;
87 | } else if (config == Bitmap.Config.ARGB_4444) {
88 | return 2;
89 | } else if (config == Bitmap.Config.ALPHA_8) {
90 | return 1;
91 | }
92 | return 1;
93 | }
94 | //获取inBitmap,实现内存复用
95 | public Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
96 | Bitmap bitmap = null;
97 |
98 | if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
99 | final Iterator> iterator = mReusableBitmaps.iterator();
100 | Bitmap item;
101 |
102 | while (iterator.hasNext()) {
103 | item = iterator.next().get();
104 |
105 | if (null != item && item.isMutable()) {
106 | if (canUseForInBitmap(item, options)) {
107 |
108 | Log.v("TEST", "canUseForInBitmap!!!!");
109 |
110 | bitmap = item;
111 |
112 | // Remove from reusable set so it can't be used again
113 | iterator.remove();
114 | break;
115 | }
116 | } else {
117 | // Remove from the set if the reference has been cleared.
118 | iterator.remove();
119 | }
120 | }
121 | }
122 |
123 | return bitmap;
124 | }
125 | public Bitmap getBitmap(String url) {
126 | Bitmap bitmap = null;
127 | if (mMemoryCache != null) {
128 | bitmap = mMemoryCache.get(url);
129 | }
130 | if (bitmap != null) {
131 | MLog.d(TAG, "Memory cache EXIET!");
132 | }
133 | return bitmap;
134 | }
135 | public void addBitmapToCache(String url,Bitmap bitmap) {
136 | if (url == null || bitmap == null) {
137 | return;
138 | }
139 | MLog.d(TAG,"addBitmapToCache size:" + getBitmapSize(bitmap));
140 | mMemoryCache.put(url, bitmap);
141 | }
142 | public void clearCache() {
143 | if (mMemoryCache != null) {
144 | mMemoryCache.evictAll();
145 | }
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/miniimageloder/src/main/java/com/android/miniimageloader/config/BitmapConfig.java:
--------------------------------------------------------------------------------
1 | package com.android.miniimageloader.config;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapFactory;
5 |
6 | import java.io.InputStream;
7 |
8 | /**
9 | * Created by yuchengluo on 2016/5/9.
10 | */
11 | public class BitmapConfig {
12 | private int mWidth, mHeight;
13 | private Bitmap.Config mPreferred;
14 |
15 | public BitmapConfig(int width, int height) {
16 | this(width, height, Bitmap.Config.RGB_565);
17 | }
18 |
19 | public BitmapConfig(int width, int height, Bitmap.Config preferred) {
20 | this.mWidth = width;
21 | this.mHeight = height;
22 | this.mPreferred = preferred;
23 | }
24 |
25 | public BitmapFactory.Options getBitmapOptions() {
26 | return getBitmapOptions(null);
27 | }
28 |
29 | //精确计算,需要图片is流先解码,再计算宽高比
30 | public BitmapFactory.Options getBitmapOptions(InputStream is) {
31 | BitmapFactory.Options options = new BitmapFactory.Options();
32 | options.inPreferredConfig = Bitmap.Config.RGB_565;
33 | if(is != null) {
34 | options.inJustDecodeBounds = true;
35 | BitmapFactory.decodeStream(is, null, options);
36 | options.inSampleSize = calculateInSampleSize(options, mWidth, mHeight);
37 | }
38 | options.inJustDecodeBounds = false;
39 | return options;
40 | }
41 |
42 | public static int calculateInSampleSize(BitmapFactory.Options options,
43 | int reqWidth, int reqHeight) {
44 | final int height = options.outHeight;
45 | final int width = options.outWidth;
46 | int inSampleSize = 1;
47 | if (height > reqHeight || width > reqWidth) {
48 | final int halfHeight = height / 2;
49 | final int halfWidth = width / 2;
50 | while ((halfHeight / inSampleSize) > reqHeight
51 | && (halfWidth / inSampleSize) > reqWidth) {
52 | inSampleSize *= 2;
53 | }
54 | }
55 | return inSampleSize;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/miniimageloder/src/main/java/com/android/miniimageloader/config/MiniImageLoaderConfig.java:
--------------------------------------------------------------------------------
1 | package com.android.miniimageloader.config;
2 |
3 | import android.support.annotation.IntDef;
4 |
5 | import java.lang.annotation.Retention;
6 | import java.lang.annotation.RetentionPolicy;
7 |
8 | /**
9 | * Created by yuchengluo on 2016/5/3.
10 | */
11 | public class MiniImageLoaderConfig {
12 | public static final int VESION_IMAGELOADER = 1;
13 | public static final long DEFAULT_DISK_CACHE_SIZE = 10 * 1024 * 1024;//10M
14 | }
15 |
--------------------------------------------------------------------------------
/miniimageloder/src/main/java/com/android/miniimageloader/utils/BitmapUtil.java:
--------------------------------------------------------------------------------
1 | package com.android.miniimageloader.utils;
2 |
3 | import android.annotation.TargetApi;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.os.Build;
7 |
8 | import com.android.miniimageloader.cache.ImageCache;
9 |
10 | import java.io.InputStream;
11 |
12 | /**
13 | * Created by yuchengluo on 2016/5/12.
14 | */
15 | public class BitmapUtil {
16 | static public Bitmap decodeSampledBitmapFromStream(
17 | InputStream is, BitmapFactory.Options options) {
18 | MLog.d("BitmapUtil", "mSampleSize:" + options.inSampleSize);
19 | return BitmapFactory.decodeStream(is, null, options);
20 | }
21 |
22 | public static Bitmap decodeSampledBitmapFromStream(
23 | InputStream is, BitmapFactory.Options options, ImageCache cache) {
24 |
25 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
26 | addInBitmapOptions(options, cache);
27 | }
28 |
29 | return BitmapFactory.decodeStream(is, null, options);
30 | }
31 |
32 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
33 | private static void addInBitmapOptions(BitmapFactory.Options options, ImageCache cache) {
34 |
35 | options.inMutable = true;
36 | if (cache != null) {
37 | Bitmap inBitmap = cache.getBitmapFromReusableSet(options);
38 |
39 | if (inBitmap != null) {
40 | options.inBitmap = inBitmap;
41 | }
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/miniimageloder/src/main/java/com/android/miniimageloader/utils/CloseUtil.java:
--------------------------------------------------------------------------------
1 | package com.android.miniimageloader.utils;
2 |
3 | import java.io.Closeable;
4 | import java.io.IOException;
5 |
6 | /**
7 | * Created by yuchengluo on 2016/5/7.
8 | */
9 | public class CloseUtil {
10 | public static void closeQuietly(Closeable closeable){
11 | if(null != closeable){
12 | try{
13 | closeable.close();
14 | }catch (IOException e){
15 | e.printStackTrace();
16 | }
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/miniimageloder/src/main/java/com/android/miniimageloader/utils/FileUtil.java:
--------------------------------------------------------------------------------
1 | package com.android.miniimageloader.utils;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 | import java.io.OutputStream;
7 |
8 | /**
9 | * Created by yuchengluo on 2016/5/16.
10 | */
11 | public class FileUtil {
12 | public static boolean copyStream(InputStream input, OutputStream output)
13 | {
14 | byte[] buffer = new byte[1024]; // Adjust if you want
15 | int bytesRead;
16 | try {
17 | while ((bytesRead = input.read(buffer)) != -1) {
18 | output.write(buffer, 0, bytesRead);
19 | }
20 | }catch (IOException e){
21 | MLog.e("FileUtil","copyStream Error:" + e.getMessage());
22 | return false;
23 | }
24 | return true;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/miniimageloder/src/main/java/com/android/miniimageloader/utils/MLog.java:
--------------------------------------------------------------------------------
1 | package com.android.miniimageloader.utils;
2 |
3 | import android.util.Log;
4 |
5 | /**
6 | * Created by yuchengluo on 2016/5/10.
7 | */
8 | public class MLog {
9 | private final static String TAG = "MLog";
10 | public static void d(String tag,String value){
11 | Log.d(tag, value);
12 | }
13 |
14 | public static void e(String tag,String value){
15 | Log.e(tag, value);
16 | }
17 |
18 | public static void w(String tag,String value){
19 | Log.w(tag, value);
20 | }
21 |
22 | public static void i(String tag,String value){
23 | Log.i(tag, value);
24 | }
25 | public static void e(String tag,Throwable t){
26 | e(tag,"",t);
27 | }
28 | public static void e(String tag, String info, Throwable t) {
29 | try {
30 | if (tag != null && info != null && t != null) {
31 | e(tag, info + "\n" + Log.getStackTraceString(t));
32 | }
33 | } catch (Exception e) {
34 | MLog.e(TAG, e.getMessage());
35 | }
36 |
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/miniimageloder/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | MiniImageLoder
3 |
4 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':miniimageloder'
2 |
--------------------------------------------------------------------------------