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);
}
}