Skip to content

Commit

Permalink
增加注释
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyanbin committed Feb 12, 2018
1 parent 3c1a536 commit 0f301d2
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 55 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.8.2"
versionName "1.8.5"
multiDexEnabled true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import java.util.Map;


/**
Expand Down Expand Up @@ -229,6 +231,7 @@ public class ExcelAsyncTask<K> extends AsyncTask<Integer,Void, K[][]>{

WeakReference<Context> softReference;


public ExcelAsyncTask(Context context) {
softReference=new WeakReference<>(context);
}
Expand All @@ -254,7 +257,6 @@ protected void onPostExecute(K[][] data) {
//设置字体
ArrayTableData<K> tableData = ArrayTableData.create((SmartTable<K>) smartTable, "", data, new TextDrawFormat<K>() {


//Excel 因为每格的大小都不一样,所以需要重新计算高度和宽度
@Override
public int measureWidth(Column<K> column, int position, TableConfig config) {
Expand All @@ -266,7 +268,7 @@ public int measureWidth(Column<K> column, int position, TableConfig config) {
if (cell != null) {
int fontSize = (int) (getFontSize(softReference.get(),(T) cell) * fontScale); //增加字体,效果更好看
config.getPaint().setTextSize(DensityUtils.sp2px(softReference.get(), fontSize));
width = DrawUtils.getMultiTextWidth(config.getPaint(), column.format(position));
width = DrawUtils.getMultiTextWidth(config.getPaint(),getSplitString(column.format(position)));
}
return width;
}
Expand All @@ -281,14 +283,14 @@ public int measureHeight(Column<K> column, int position, TableConfig config) {

int fontSize = (int) (getFontSize(softReference.get(),(T) cell) * fontScale); //增加字体,效果更好看
config.getPaint().setTextSize(DensityUtils.sp2px(softReference.get(), fontSize));
return DrawUtils.getMultiTextHeight(config.getPaint(), column.format(position));
return DrawUtils.getMultiTextHeight(config.getPaint(), getSplitString(column.format(position)));
}
return super.measureHeight(column, position, config);
}

@Override
protected void drawText(Canvas c, String value, Rect rect, Paint paint) {
DrawUtils.drawMultiText(c, paint, rect, value);
DrawUtils.drawMultiText(c, paint, rect, getSplitString(value));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void onDraw(Canvas canvas, Rect showRect, String tableName, TableConfig c
switch (direction) {
case TOP:
case BOTTOM:
DrawUtils.drawMultiText(canvas,paint,rect,tableName);
DrawUtils.drawMultiText(canvas,paint,rect,tableName.split("\n"));
break;
case LEFT:
case RIGHT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public int measureHeight(Column<T> column,int position, TableConfig config) {
if(height ==0) {
Paint paint = config.getPaint();
config.getContentStyle().fillPaint(paint);
height = DrawUtils.getMultiTextHeight(paint, column.format(position));
height = DrawUtils.getTextHeight(paint);
}
return height;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import com.bin.david.form.data.format.bg.ICellBackgroundFormat;
import com.bin.david.form.utils.DrawUtils;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;

/**
* Created by huang on 2017/10/30.
*/
Expand All @@ -18,19 +23,24 @@ public class TextDrawFormat<T> implements IDrawFormat<T> {

private CellInfo<T> cellInfo = new CellInfo<>();
private boolean isDrawBg = true;
private Map<String,SoftReference<String[]>> valueMap; //避免产生大量对象

public TextDrawFormat() {
valueMap = new HashMap<>();
}

@Override
public int measureWidth(Column<T>column, int position, TableConfig config) {
Paint paint = config.getPaint();
config.getContentStyle().fillPaint(paint);
return DrawUtils.getMultiTextWidth(paint,column.format(position));
return DrawUtils.getMultiTextWidth(paint,getSplitString(column.format(position)));
}

@Override
public int measureHeight(Column<T> column,int position, TableConfig config) {
Paint paint = config.getPaint();
config.getContentStyle().fillPaint(paint);
return DrawUtils.getMultiTextHeight(paint,column.format(position));
return DrawUtils.getMultiTextHeight(paint,getSplitString(column.format(position)));
}

@Override
Expand All @@ -46,7 +56,7 @@ public void draw(Canvas c, Column<T> column, T t, String value, Rect rect, int p
}

protected void drawText(Canvas c, String value, Rect rect, Paint paint) {
DrawUtils.drawMultiText(c,paint,rect,value);
DrawUtils.drawMultiText(c,paint,rect,getSplitString(value));
}


Expand Down Expand Up @@ -82,4 +92,16 @@ public CellInfo<T> getCellInfo() {
public void setCellInfo(CellInfo<T> cellInfo) {
this.cellInfo = cellInfo;
}

protected String[] getSplitString(String val){
String[] values = null;
if(valueMap.get(val)!=null){
values= valueMap.get(val).get();
}
if(values == null){
values = val.split("\n");
valueMap.put(val, new SoftReference<>(values));
}
return values;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ArrayTableData<T> extends TableData<T> {

private T[][] data;
private List<Column<T>> arrayColumns;
private OnItemClickListener onItemClickListener;


/**
* 提供将数组[col][row]转换成数组[row][col]
Expand All @@ -45,8 +45,7 @@ public static<T> T[][] transformColumnArray(T[][] rowArray){
for (int i = 0; i < rowArray.length; i++) { //转换一下
for (int j = 0; j < rowArray[i].length; j++) {
if(newData[j] == null) {
T[] column = (T[]) Array.newInstance(row.getClass().getComponentType(),rowArray.length);
newData[j] = column;
newData[j] = (T[]) Array.newInstance(row.getClass().getComponentType(),rowArray.length);
}
newData[j][i] = rowArray[i][j];
}
Expand Down Expand Up @@ -168,36 +167,6 @@ public void setData(T[][] data) {



/**
* 获取表格单元格Cell点击事件
*/
public OnItemClickListener getOnItemClickListener() {
return onItemClickListener;

}

/**
* 设置表格单元格Cell点击事件
* @param onItemClickListener 点击事件
*/
public void setOnItemClickListener(final OnItemClickListener<T> onItemClickListener) {
this.onItemClickListener = onItemClickListener;
for(Column<T> column: arrayColumns){
column.setOnColumnItemClickListener(new OnColumnItemClickListener<T>() {
@Override
public void onClick(Column<T> column, String value, T t, int position) {
if(onItemClickListener !=null){
int index = arrayColumns.indexOf(column);
onItemClickListener.onClick(column,value,t,index,position);
}
}
});
}
}
/**
* 表格单元格Cell点击事件接口
*/
public interface OnItemClickListener<T>{
void onClick(Column<T> column,String value, T t, int col,int row);
}
}
41 changes: 37 additions & 4 deletions form/src/main/java/com/bin/david/form/data/table/TableData.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.bin.david.form.data.format.sequence.NumberSequenceFormat;
import com.bin.david.form.data.format.title.ITitleDrawFormat;
import com.bin.david.form.data.format.title.TitleDrawFormat;
import com.bin.david.form.listener.OnColumnItemClickListener;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -34,9 +35,9 @@ public class TableData<T> {
private ITitleDrawFormat titleDrawFormat;
private ISequenceFormat XSequenceFormat;
private ISequenceFormat YSequenceFormat;
// private List<CellRange> cellRangeAddresses;
//用户设置的 不能清除
private List<CellRange> userSetRangeAddress;
private OnItemClickListener onItemClickListener;
/**
*
* @param tableName 表名
Expand Down Expand Up @@ -387,7 +388,39 @@ public void clear(){

}

/* public List<CellRange> getCellRangeAddresses() {
return cellRangeAddresses;
}*/
/**
* 获取表格单元格Cell点击事件
*/
public OnItemClickListener getOnItemClickListener() {
return onItemClickListener;

}

/**
* 设置表格单元格Cell点击事件
* @param onItemClickListener 点击事件
*/
public void setOnItemClickListener(final OnItemClickListener<T> onItemClickListener) {
this.onItemClickListener = onItemClickListener;
for(Column<T> column: columns){
if(!column.isParent()) {
column.setOnColumnItemClickListener(new OnColumnItemClickListener<T>() {
@Override
public void onClick(Column<T> column, String value, T t, int position) {
if (onItemClickListener != null) {
int index = childColumns.indexOf(column);
onItemClickListener.onClick(column, value, t, index, position);
}
}
});
}
}
}

/**
* 表格单元格Cell点击事件接口
*/
public interface OnItemClickListener<T>{
void onClick(Column<T> column,String value, T t, int col,int row);
}
}
12 changes: 5 additions & 7 deletions form/src/main/java/com/bin/david/form/utils/DrawUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ public static boolean isVerticalMixRect(Rect rect,int top,int bottom){
* @param paint
* @return
*/
public static int getMultiTextHeight(Paint paint,String value){
public static int getMultiTextHeight(Paint paint,String[] values){

return getTextHeight(paint)* value.split("\n").length;
return getTextHeight(paint)* values.length;
}

/**
* 获取多行文字宽度
* @param paint
* @return
*/
public static int getMultiTextWidth(Paint paint,String value){
String[] values = value.split("\n");
public static int getMultiTextWidth(Paint paint,String[] values){

int maxWidth =0;
for(String val :values){
int width = (int) paint.measureText(val);
Expand All @@ -101,10 +101,8 @@ public static int getMultiTextWidth(Paint paint,String value){
* @param canvas
* @param paint
* @param rect
* @param value
*/
public static void drawMultiText(Canvas canvas,Paint paint,Rect rect,String value){
String[] values = value.split("\n");
public static void drawMultiText(Canvas canvas,Paint paint,Rect rect,String[] values){
for(int i =0;i <values.length;i++) {
int centerY = (int) ((rect.bottom + rect.top) / 2+ (values.length/2f-i-0.5)*getTextHeight(paint));
canvas.drawText(values[i], DrawUtils.getTextCenterX(rect.left, rect.right, paint),
Expand Down

0 comments on commit 0f301d2

Please sign in to comment.