完善 AutoWrapLayout onMeasure 函数,图片内存缓存设为 8MB

This commit is contained in:
seven332 committed 2014-03-24 23:52:07 +08:00
1 parent aa3d3ca57c
commit 601d426721
5 files changed
+98 -38

No files matched your search

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+4 -4
View File
@@ -62,16 +62,16 @@ public class Cache {
}
}
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 24;
//final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = 8 * 1024 * 1024; // 8MB, can store 80 thumb
memoryCache = new LruCache<String, Bitmap>(cacheSize) {
@SuppressLint("NewApi")
@Override
protected int sizeOf(String key, Bitmap bitmap) {
if (Build.VERSION.SDK_INT < 19)
return bitmap.getByteCount() / 1024;
return bitmap.getByteCount();
else
return bitmap.getAllocationByteCount() / 1024;
return bitmap.getAllocationByteCount();
}
};
@@ -3,17 +3,23 @@ package com.hippo.ehviewer.widget;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
/**
* A ViewGroup that can layout views in line and auto wrap
*
* @author Hippo
*
*/
public class AutoWrapLayout extends ViewGroup {
@SuppressWarnings("unused")
private final static String TAG = "AutoWrapLayout";
private List<Rect> rectList = new ArrayList<Rect>();
public AutoWrapLayout(Context context) {
@@ -26,59 +32,90 @@ public class AutoWrapLayout extends ViewGroup {
super(context, attrs, defStyle);
}
static int adjust(int measureSpec, int delta) {
return MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(measureSpec + delta), MeasureSpec.getMode(measureSpec));
}
// TODO Take vertical mode
/**
* each row or line at least show one child
*
* horizontal only show child can show or partly show in parent
*/
@SuppressLint("DrawAllocation")
@Override
protected void onMeasure(int widthMeasureSpec, int heightSpec) {
final int mWidth = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
final int childCount = getChildCount();
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
int maxHeight = MeasureSpec.getSize(heightMeasureSpec);
Log.d(TAG, "widthMode = " + widthMode);
Log.d(TAG, "heightMode = " + heightMode);
Log.d(TAG, "maxWidth = " + maxWidth);
Log.d(TAG, "maxHeight = " + maxHeight);
Log.d(TAG, "getChildCount() = " + getChildCount());
if (widthMode == MeasureSpec.UNSPECIFIED)
maxWidth = Integer.MAX_VALUE;
if (heightMode == MeasureSpec.UNSPECIFIED)
maxHeight = Integer.MAX_VALUE;
int paddingLeft = getPaddingLeft();
int paddingTop = getPaddingTop();
int paddingRight = getPaddingRight();
int paddingBottom = getPaddingBottom();
int maxRightBound = maxWidth - paddingRight;
int maxBottomBound = maxWidth - paddingBottom;
int left;
int top;
int right;
int bottom;
int rightBound = 0;
int rightBound = paddingLeft;
int maxRightNoPadding = rightBound;
int bottomBound;
int lastMaxBottom = 0;
int maxBottom = lastMaxBottom;
int width;
int height;
int lastMaxBottom = paddingTop;
int maxBottom = lastMaxBottom; // maxBottom is maxBottomNoPadding
//int maxBottomNoPadding = maxBottom;
int childWidth;
int childHeight;
rectList.clear();
for(int i = 0; i < childCount; i++){
final View child = this.getChildAt(i);
int childCount = getChildCount();
for (int index = 0; index < childCount; index++) {
final View child = getChildAt(index);
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
if (child.getVisibility() == View.GONE)
continue;
final AutoWrapLayout.LayoutParams lp =
(AutoWrapLayout.LayoutParams)child.getLayoutParams();
width = child.getMeasuredWidth();
height = child.getMeasuredHeight();
childWidth = child.getMeasuredWidth();
childHeight = child.getMeasuredHeight();
left = rightBound + lp.leftMargin;
right = left + width;
right = left + childWidth;
rightBound = right + lp.rightMargin;
//if it can't drawing on a same line , skip to next line
if(rightBound > mWidth) {
left = 0 + lp.leftMargin;
right = left + width;
if (rightBound > maxRightBound) { // Go to next row
// If child can't show in parent
if (maxBottom >= maxBottomBound)
break;
left = paddingLeft + lp.leftMargin;
right = left + childWidth;
rightBound = right + lp.rightMargin;
lastMaxBottom = maxBottom;
top = lastMaxBottom + lp.topMargin;
bottom = top + height;
bottom = top + childHeight;
bottomBound = bottom + lp.bottomMargin;
maxBottom = bottomBound;
} else {
top = lastMaxBottom + lp.topMargin;
bottom = top + height;
bottom = top + childHeight;
bottomBound = bottom + lp.bottomMargin;
}
// Update max bottom
// Update max
if (rightBound > maxRightNoPadding)
maxRightNoPadding = rightBound;
if (bottomBound > maxBottom)
maxBottom = bottomBound;
Rect rect = new Rect();
@@ -88,15 +125,38 @@ public class AutoWrapLayout extends ViewGroup {
rect.bottom = bottom;
rectList.add(rect);
}
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), maxBottom + getPaddingTop() + getPaddingBottom());
int measuredWidth;
int measuredHeight;
if (widthMode == MeasureSpec.EXACTLY)
measuredWidth = maxWidth;
else {
measuredWidth = maxRightNoPadding + paddingRight;
if (widthMode == MeasureSpec.AT_MOST)
measuredWidth = measuredWidth > maxWidth ? maxWidth : measuredWidth;
}
if (heightMode == MeasureSpec.EXACTLY)
measuredHeight = maxHeight;
else {
measuredHeight = maxBottom + paddingBottom;
if (heightMode == MeasureSpec.AT_MOST)
measuredHeight = measuredHeight > maxHeight ? maxHeight : measuredHeight;
}
Log.d(TAG, "measuredWidth = " + measuredWidth);
Log.d(TAG, "measuredHeight = " + measuredHeight);
setMeasuredDimension(measuredWidth, measuredHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount() > rectList.size() ? rectList.size() : getChildCount();
final int count = rectList.size();
for(int i = 0; i < count; i++){
final View child = this.getChildAt(i);
if (child.getVisibility() == View.GONE)
continue;
Rect rect = rectList.get(i);
child.layout(rect.left, rect.top, rect.right, rect.bottom);
}