Skip to content

Commit

Permalink
BaseDanmaku: padding property supported
Browse files Browse the repository at this point in the history
  • Loading branch information
ctiao committed Apr 23, 2014
1 parent 4471d5f commit 5c69b22
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public abstract class BaseDanmaku {
* 框的颜色,0表示无框
*/
public int borderColor = 0;

/**
* 内边距(像素)
*/
public int padding = 0;

/**
* 占位宽度
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ public static void drawDanmaku(BaseDanmaku danmaku, Canvas canvas, float left, f
boolean quick) {
float _left = left;
float _top = top;
left += danmaku.padding;
top += danmaku.padding;
if (danmaku.borderColor != 0) {
left += BORDER_WIDTH;
top += BORDER_WIDTH;
Expand Down Expand Up @@ -394,12 +396,7 @@ private void calcPaintWH(BaseDanmaku danmaku, TextPaint paint) {
Float textHeight = getTextHeight(paint);
if (danmaku.lines == null) {
w = danmaku.text == null ? 0 : paint.measureText(danmaku.text);
danmaku.paintWidth = w;
danmaku.paintHeight = textHeight;
if (danmaku.borderColor != 0) {
danmaku.paintWidth += 2 * BORDER_WIDTH;
danmaku.paintHeight += 2 * BORDER_WIDTH;
}
setDanmakuPaintWidthAndHeight(danmaku,w,textHeight);
return;
}

Expand All @@ -410,15 +407,21 @@ private void calcPaintWH(BaseDanmaku danmaku, TextPaint paint) {
}
}

danmaku.paintWidth = w;
danmaku.paintHeight = danmaku.lines.length * textHeight;
setDanmakuPaintWidthAndHeight(danmaku,w,danmaku.lines.length * textHeight);
}

private void setDanmakuPaintWidthAndHeight(BaseDanmaku danmaku, float w, float h) {
float pw = w + 2 * danmaku.padding;
float ph = h + 2 * danmaku.padding;
if (danmaku.borderColor != 0) {
danmaku.paintWidth += 2 * BORDER_WIDTH;
danmaku.paintHeight += 2 * BORDER_WIDTH;
pw += 2 * BORDER_WIDTH;
ph += 2 * BORDER_WIDTH;
}
danmaku.paintWidth = pw;
danmaku.paintHeight = ph;
}

private static Float getTextHeight(TextPaint paint) {
private static float getTextHeight(TextPaint paint) {
Float textSize = paint.getTextSize();
Float textHeight = sTextHeightCache.get(textSize);
if(textHeight == null){
Expand Down
6 changes: 4 additions & 2 deletions Sample/src/main/java/com/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,18 @@ public void onClick(View v) {
} else if (v == mBtnSendDanmaku) {
BaseDanmaku danmaku = DanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL, mDanmakuView.getWidth());
StringBuffer sb = new StringBuffer();
//for(int i=0;i<100;i++){
for(int i=0;i<100;i++){
sb.append("这是一条弹幕");
//}
}
danmaku.text = sb.toString();
danmaku.padding = 5;
danmaku.time = mParser.getTimer().currMillisecond + 100;
danmaku.textSize = 25f * (mParser.getDisplayer().getDensity() - 0.6f);
danmaku.textColor = Color.RED;
danmaku.textShadowColor = Color.WHITE;
//danmaku.underlineColor = Color.GREEN;
danmaku.borderColor = Color.GREEN;

mDanmakuView.addDanmaku(danmaku);
}
}
Expand Down

0 comments on commit 5c69b22

Please sign in to comment.