Skip to content

Commit

Permalink
优化自定义描述文字api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay-huangjie committed Dec 2, 2019
1 parent b958877 commit 0f1cbae
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 24 deletions.
50 changes: 35 additions & 15 deletions app/src/main/java/jie/com/funnel/CustomLabelActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package jie.com.funnel;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import java.util.List;

import jie.com.funnellib.CustomLabelCallback;
import jie.com.funnellib.FunnelView;
import jie.com.funnellib.Util;
import jie.com.funnellib.LabelHelper;

/**
* Created by hj on 2019/2/25.
Expand All @@ -19,21 +17,43 @@ public class CustomLabelActivity extends BaseFunnelActivity {
@Override
void initEvent(FunnelView funnelView) {
final List<FunnelData> data = FunnelData.getFourCountData();
//第一种方式,获取画笔,画布,实现逻辑自由,缺点是需要熟悉自定义View的绘制Api
// funnelView.addCustomLabelCallback(new CustomLabelCallback() {
// @Override
// public void drawText(LabelHelper mHelper, int index) {
// FunnelData mData = data.get(index);
// //获取控件
// Canvas canvas = mHelper.getCanvas();
// float mLabelX = mHelper.getLabelX();
// float mLabelY = mHelper.getLabelY();
// Paint mPaintLabel = mHelper.getPaint();
// mPaintLabel.setColor(mData.color);
// mPaintLabel.setFakeBoldText(false);
// //先画前面的文字
// canvas.drawText(mData.getLabel() + ":", mLabelX, mLabelY, mPaintLabel);
// //计算前面文字的长度
// float labelWidth = Util.getTextWidth(mPaintLabel, mData.getLabel() + ":");
// mPaintLabel.setColor(Color.parseColor("#333333"));
// mPaintLabel.setFakeBoldText(true);
// //画后面的文字
// canvas.drawText(mData.num + "个", mLabelX + labelWidth, mLabelY, mPaintLabel);
// }
// });

//第二种方式,使用封装的Api,能覆盖大部分场景,但缺少定制化的自由
funnelView.addCustomLabelCallback(new CustomLabelCallback() {
@Override
public void drawText(Canvas canvas, Paint mPaintLabel, float labelX, float labelY, int index) {
FunnelData funnelData = data.get(index);
//先画前面的文字
mPaintLabel.setColor(funnelData.color);
mPaintLabel.setFakeBoldText(false);
canvas.drawText(funnelData.getLabel()+":", labelX, labelY, mPaintLabel);
//计算前面文字的长度
float labelWidth = Util.getTextWidth(mPaintLabel,funnelData.getLabel()+":");
mPaintLabel.setColor(Color.parseColor("#333333"));
mPaintLabel.setFakeBoldText(true);
//画后面的文字
canvas.drawText(funnelData.num+"个", labelX + labelWidth, labelY, mPaintLabel);
public void drawText(LabelHelper mHelper, int index) {
FunnelData mData = data.get(index);
mHelper.build(
mHelper.getBuild()
.setFirstHalfText(mData.label+":")
.setFirstHalfTextStyle(mData.color)
.setCenterHalfText(mData.num)
.setCenterHalfTextStyle(Color.parseColor("#333333"),true)
.setFooterHalfText("个")
.setFooterTextStyle(Color.parseColor("#333333"))
);
}
});
funnelView.setChartData(data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package jie.com.funnellib;

import android.graphics.Canvas;
import android.graphics.Paint;

/**
* Created by hj on 2019/2/22.
* 说明:开放自定义描述绘制画笔
*/
public interface CustomLabelCallback {
/**
* 循环绘制线后面的文字
* @param canvas 画布
* @param mPaintLabel 画笔
* @param labelX 文字开始X坐标
* @param labelY 文字开始Y坐标
* @param mHelper 描述文字辅助类
* @param index 绘制下标
* 注意:绘制顺序是从下往上绘制!!!
*/
void drawText(Canvas canvas, Paint mPaintLabel,float labelX, float labelY,int index);
void drawText(LabelHelper mHelper,int index);
}
10 changes: 9 additions & 1 deletion funnellib/src/main/java/jie/com/funnellib/FunnelView.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public class FunnelView extends View {
* */
private float[] halfArrays;

/**
* 描述文字辅助类
*/
private LabelHelper mLabelHelper;

public FunnelView(Context context) {
super(context);
initView(context, null);
Expand All @@ -135,6 +140,7 @@ public FunnelView(Context context, AttributeSet attrs, int defStyle) {

private void initView(Context context, AttributeSet attributeSet) {
this.mContext = context;
mLabelHelper = new LabelHelper();
if (attributeSet != null) {
TypedArray ta = context.obtainStyledAttributes(attributeSet, R.styleable.FunnelView);
mLineWidth = ta.getDimension(R.styleable.FunnelView_lineWidth, dip2px(context, 12));
Expand Down Expand Up @@ -272,6 +278,7 @@ private void renderPlotDesc(Canvas canvas, float cx, float funnelHeight) {
pStart.y = pStop.y = mPlotBottom;
float lineY;
Path path = new Path();
mLabelHelper.setDrawBasicParameters(canvas,mPaintLabel);
for (int i = 0; i < count; i++) {
IFunnelData d = mDataSet.get(i);
path.reset();
Expand Down Expand Up @@ -319,7 +326,8 @@ private void renderPlotDesc(Canvas canvas, float cx, float funnelHeight) {
if (mCustomLabelCallback == null) {
canvas.drawText(d.getLabel(), labelX, labelY, mPaintLabel);
} else {
mCustomLabelCallback.drawText(canvas, mPaintLabel, labelX, labelY, i);
mLabelHelper.updateXY(labelX,labelY);
mCustomLabelCallback.drawText(mLabelHelper, i);
}
}
}
Expand Down
138 changes: 138 additions & 0 deletions funnellib/src/main/java/jie/com/funnellib/LabelHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package jie.com.funnellib;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.text.TextUtils;

/**
* Created by huangjie on 2019/12/2.
* 说明:描述文字辅助类
*/
public final class LabelHelper {
private float mLabelX;
private float mLabelY;
private Canvas mCanvas;
private Paint mPaint;
private Builder mBuilder;

protected void setDrawBasicParameters(Canvas mCanvas, Paint mPaint) {
this.mCanvas = mCanvas;
this.mPaint = mPaint;
}

protected void updateXY(float mLabelX, float mLabelY) {
this.mLabelX = mLabelX;
this.mLabelY = mLabelY;
}

public float getLabelX() {
return mLabelX;
}

public float getLabelY() {
return mLabelY;
}

public Canvas getCanvas() {
return mCanvas;
}

public Paint getPaint() {
return mPaint;
}

public void build(@NonNull Builder builder) {
String mFirstHalfText = builder.mFirstHalfText;
float mFirstHalfX = 0;
if (!TextUtils.isEmpty(mFirstHalfText)) {
mPaint.setColor(builder.mFirstHalfTextColor);
mPaint.setFakeBoldText(builder.mFirstHalfFakeBoldText);
mCanvas.drawText(mFirstHalfText, mLabelX, mLabelY, mPaint);
mFirstHalfX = Util.getTextWidth(mPaint, mFirstHalfText);
}
String mCenterHalfText = builder.mCenterHalfText;
float mCenterHalfX = 0;
if (!TextUtils.isEmpty(mCenterHalfText)) {
mPaint.setColor(builder.mCenterHalfTextColor);
mPaint.setFakeBoldText(builder.mCenterHalfFakeBoldText);
mCanvas.drawText(mCenterHalfText, mLabelX + mFirstHalfX, mLabelY, mPaint);
mCenterHalfX = Util.getTextWidth(mPaint, mCenterHalfText);
}
String mFooterHalfText = builder.mFooterHalfText;
if (!TextUtils.isEmpty(mFooterHalfText)) {
mPaint.setColor(builder.mFooterTextColor);
mPaint.setFakeBoldText(builder.mFooterHalfFakeBoldText);
mCanvas.drawText(mFooterHalfText, mLabelX + mCenterHalfX + mFirstHalfX, mLabelY, mPaint);
}
}

public Builder getBuild() {
if (mBuilder == null) {
mBuilder = new Builder();
}
return mBuilder;
}

public final static class Builder {
//内容
private String mFirstHalfText;
private String mCenterHalfText;
private String mFooterHalfText;
//字体颜色
private int mFirstHalfTextColor;
private int mCenterHalfTextColor;
private int mFooterTextColor;
//是否加粗
private boolean mFirstHalfFakeBoldText;
private boolean mCenterHalfFakeBoldText;
private boolean mFooterHalfFakeBoldText;

public Builder setFirstHalfText(String mFirstHalfText) {
this.mFirstHalfText = mFirstHalfText;
return this;
}

public Builder setCenterHalfText(String mCenterHalfText) {
this.mCenterHalfText = mCenterHalfText;
return this;
}

public Builder setFooterHalfText(String mFooterHalfText) {
this.mFooterHalfText = mFooterHalfText;
return this;
}

public Builder setFirstHalfTextStyle(int mFirstHalfTextColor) {
return setFirstHalfTextStyle(mFirstHalfTextColor,false);
}

public Builder setFirstHalfTextStyle(int mFirstHalfTextColor,boolean mFirstHalfFakeBoldText) {
this.mFirstHalfTextColor = mFirstHalfTextColor;
this.mFirstHalfFakeBoldText = mFirstHalfFakeBoldText;
return this;
}

public Builder setCenterHalfTextStyle(int mCenterHalfTextColor) {
return setCenterHalfTextStyle(mCenterHalfTextColor,false);
}

public Builder setCenterHalfTextStyle(int mCenterHalfTextColor,boolean mCenterHalfFakeBoldText) {
this.mCenterHalfTextColor = mCenterHalfTextColor;
this.mCenterHalfFakeBoldText = mCenterHalfFakeBoldText;
return this;
}

public Builder setFooterTextStyle(int mFooterTextColor) {
return setFooterTextStyle(mFooterTextColor,false);
}

public Builder setFooterTextStyle(int mFooterTextColor,boolean mFooterHalfFakeBoldText) {
this.mFooterTextColor = mFooterTextColor;
this.mFooterHalfFakeBoldText = mFooterHalfFakeBoldText;
return this;
}

}

}

0 comments on commit 0f1cbae

Please sign in to comment.