align_center在JAVA_Java Alignment.ALIGN_CENTER屬性代碼示例

private void redraw() {

LogUtils.d(this, "redraw");

SurfaceHolder holder = getSurfaceHolder();

Canvas c = null;

try {

c = holder.lockCanvas();

if (c != null) {

int width = c.getWidth();

int height = c.getHeight();

int soundWaveHeight = height / 3;

Rect soundwaveBoundingRect = new Rect(0, soundWaveHeight, width, soundWaveHeight * 2);

Rect titleBoundingRect = new Rect(dipToPixels(8),

soundwaveBoundingRect.bottom + dipToPixels(8),

width - dipToPixels(8),

soundwaveBoundingRect.bottom + soundWaveHeight / 2);

c.drawColor(getApplicationContext().getResources().getColor(R.color.black));

c.save();

// draw the inside of the sound wave

int sc = c.saveLayer(0, 0, width, height, null,

Canvas.MATRIX_SAVE_FLAG |

Canvas.CLIP_SAVE_FLAG |

Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |

Canvas.FULL_COLOR_LAYER_SAVE_FLAG |

Canvas.CLIP_TO_LAYER_SAVE_FLAG);

c.clipRect(soundwaveBoundingRect);

Paint paint = new Paint();

paint.setFilterBitmap(false);

paint.setColor(getApplicationContext().getResources().getColor(R.color.orange));

c.drawPaint(paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

c.drawBitmap(mCurrentSoundwave,

new Rect(0,0,mCurrentSoundwave.getWidth(), mCurrentSoundwave.getHeight()),

soundwaveBoundingRect, paint);

paint.setXfermode(null);

c.restore();

c.restoreToCount(sc);

// Draw the title

int startTextSizeInPixels = dipToPixels(16);

TextPaint textPaint = new TextPaint();

textPaint.setTextSize(startTextSizeInPixels);

textPaint.setColor(Color.WHITE);

textPaint.setAntiAlias(true);

// setup the bounding box for the text

int maxRowsCount = (int)Math.floor(((double)titleBoundingRect.height()) / (1.5d * (double)startTextSizeInPixels));

int maxTitleWidth = maxRowsCount * titleBoundingRect.width();

float wholeWidth = textPaint.measureText(mTitle);

StaticLayout sl;

if(wholeWidth > maxTitleWidth) {

int symbolsCount = (int)(maxTitleWidth / (wholeWidth / mTitle.length())) - 4;

String truncated = mTitle.substring(0, symbolsCount) + "...";

sl = new StaticLayout(truncated, 0, truncated.length(),

textPaint, titleBoundingRect.width(),

Alignment.ALIGN_CENTER, 1.0f, 1.0f,

false, null, 0);

}

else {

sl = new StaticLayout(mTitle, 0, mTitle.length(),

textPaint, titleBoundingRect.width(),

Alignment.ALIGN_CENTER, 1.0f, 1.0f,

false, null, 0);

}

// draw the text

c.save();

c.translate(titleBoundingRect.left, titleBoundingRect.top);

sl.draw(c);

c.restore();

}

} finally {

if (c != null)

holder.unlockCanvasAndPost(c);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值