下面列出了android.graphics.drawable.NinePatchDrawable#setBounds() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*
* @param canvas The canvas you need to draw on.
* @param point The Point consists of the x y coordinates from left bottom to right top.
* Like is ↓
* 3
* 2
* 1
* 0 1 2 3 4 5
*/
private void drawPopup(Canvas canvas,String num, Point point){
boolean singularNum = (num.length() == 1);
int sidePadding = MyUtils.dip2px(getContext(),singularNum? 8:5);
int x = point.x;
int y = point.y-MyUtils.dip2px(getContext(),5);
Rect popupTextRect = new Rect();
popupTextPaint.getTextBounds(num,0,num.length(),popupTextRect);
Rect r = new Rect(x-popupTextRect.width()/2-sidePadding,
y - popupTextRect.height()-bottomTriangleHeight-popupTopPadding*2-popupBottomMargin,
x + popupTextRect.width()/2+sidePadding,
y+popupTopPadding-popupBottomMargin);
NinePatchDrawable popup = (NinePatchDrawable)getResources().
getDrawable(R.drawable.popup_red);
popup.setBounds(r);
popup.draw(canvas);
canvas.drawText(num, x, y-bottomTriangleHeight-popupBottomMargin, popupTextPaint);
}
/**
* @param canvas The canvas you need to draw on.
* @param point The Point consists of the x y coordinates from left bottom to right top.
* Like is
*
* 3
* 2
* 1
* 0 1 2 3 4 5
*/
private void drawPopup(Canvas canvas, float num, Point point, int PopupColor) {
String numStr = showFloatNumInPopup ? String.valueOf(num) : String.valueOf(Math.round(num));
boolean singularNum = (numStr.length() == 1);
int sidePadding = MyUtils.dip2px(getContext(), singularNum ? 8 : 5);
int x = point.x;
int y = point.y - MyUtils.dip2px(getContext(), 5);
Rect popupTextRect = new Rect();
popupTextPaint.getTextBounds(numStr, 0, numStr.length(), popupTextRect);
Rect r = new Rect(x - popupTextRect.width() / 2 - sidePadding, y
- popupTextRect.height()
- bottomTriangleHeight
- popupTopPadding * 2
- popupBottomMargin, x + popupTextRect.width() / 2 + sidePadding,
y + popupTopPadding - popupBottomMargin + popupBottomPadding);
NinePatchDrawable popup =
(NinePatchDrawable) getResources().getDrawable(R.drawable.popup_white);
popup.setColorFilter(new PorterDuffColorFilter(PopupColor, PorterDuff.Mode.MULTIPLY));
popup.setBounds(r);
popup.draw(canvas);
canvas.drawText(numStr, x, y - bottomTriangleHeight - popupBottomMargin, popupTextPaint);
}
private void updateShadowBounds() {
NinePatchDrawable shadow = mShadow;
if (shadow == null) {
return;
}
int width = getWidth();
int height = getHeight();
Rect paddings = mShadowPaddings;
if (width != 0 && height != 0) {
shadow.setBounds(-paddings.left, -paddings.top, width + paddings.right, height + paddings.bottom);
}
}
private void updateShadowBounds() {
NinePatchDrawable shadow = mShadow;
if (shadow == null) {
return;
}
int width = getWidth();
int height = getHeight();
Rect paddings = mShadowPaddings;
if (width != 0 && height != 0) {
shadow.setBounds(-paddings.left, -paddings.top, width + paddings.right, height + paddings.bottom);
}
}
private void updateNinePatchBounds(NinePatchDrawable ninePatch, int childLeft, int childTop, int childRight, int childBottom) {
if (ninePatch == null) {
return;
}
final Rect t = mTempRect;
ninePatch.getPadding(t);
ninePatch.setBounds(
childLeft - t.left, childTop - t.top,
childRight + t.right, childBottom + t.bottom);
}
private void updateShadowBounds() {
NinePatchDrawable shadow = mShadow;
if (shadow == null) {
return;
}
int width = getWidth();
int height = getHeight();
Rect paddings = mShadowPaddings;
if (width != 0 && height != 0) {
shadow.setBounds(-paddings.left, -paddings.top, width + paddings.right, height + paddings.bottom);
}
}
/**
* Renders a 14 line bar graph/ histogram of the FFT data
*/
@Override
public void onRender(Canvas canvas, FFTData data, Rect rect)
{
//space between lines of graph
float space = 4f;
Resources resources = mContext.getResources();
NinePatchDrawable bg = (NinePatchDrawable) resources.getDrawable(R.drawable.music_bar_graph);
DisplayMetrics metrics = resources.getDisplayMetrics();
//margin from left/right edges
int margin = (int) ( ( 16 * (metrics.densityDpi/160f) ) + 0.5f );
//Calculate width of each bar
float bar_width = ( ( rect.width() - ((13 * space) + (margin * 2)) ) / 14 );
//calculate length between the start of each bar
float next_start = bar_width + space;
for (int i = 0; i < 14; i++) {
//set x start of bar
float x1 = margin + (i * next_start);
//calculate height of bar based on sampling 4 data points
byte rfk = data.bytes[ (10 * i)];
byte ifk = data.bytes[ (10 * i + 1)];
float magnitude = (rfk * rfk + ifk * ifk);
int dbValue = (int) (10 * Math.log10(magnitude));
rfk = data.bytes[ (10 * i + 2)];
ifk = data.bytes[ (10 * i + 3)];
magnitude = (rfk * rfk + ifk * ifk);
dbValue = (int) ( (10 * Math.log10(magnitude)) + dbValue) / 2;
//Average with previous bars value(reduce spikes / smoother transitions)
dbValue =( mData[i] + ((dbValue < 0) ? 0 : dbValue) ) / 2;
mData[i] = dbValue;
//only jump height on multiples of 5
if(dbValue >= 5)
dbValue = (int) Math.floor(dbValue/5) * 5;
//bottom edge of canvas
float y1 = rect.height();
int blockHeight = 10;
int numBlocks = (int) Math.floor((dbValue * 8) / blockHeight);
//cycle through and render individual blocks
for( int j = 0; j < numBlocks; j++ ){
int yEnd = (int)( y1 - ( blockHeight * j ));
Rect nRect = new Rect((int)x1, yEnd - blockHeight, (int)(x1+bar_width), yEnd);
bg.setBounds(nRect);
bg.draw(canvas);
}
}
}