健康宝——呼吸频率部分开发(七)

呼吸频率页面


呼吸频率页面需要对传入不同的分贝数进行刷新,将新采集的声音大小动态地展示出来,这个类可以使用前边的工具类World获取当前的分贝值,有过渡的缓慢刷新分贝仪页面。
代码:

public class SoundDiscView extends ImageView {
private float scaleWidth, scaleHeight;
private int newWidth, newHeight;
private Matrix mMatrix = new Matrix();
private Bitmap indicatorBitmap;
private Paint paint = new Paint();
static final long ANIMATION_INTERVAL = 20;

public SoundDiscView(Context context) {
    super(context);
}

public SoundDiscView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

private void init() {
    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.noise_index);
    int bitmapWidth = myBitmap.getWidth();
    int bitmapHeight = myBitmap.getHeight();
    newWidth = getWidth();
    newHeight = getHeight();
    scaleWidth = ((float) newWidth) / (float) bitmapWidth;  // 获取缩放比例
    scaleHeight = ((float) newHeight) / (float) bitmapHeight;  //获取缩放比例
    mMatrix.postScale(scaleWidth, scaleHeight);   //设置mMatrix的缩放比例
    indicatorBitmap = Bitmap.createBitmap(myBitmap, 0, 0, bitmapWidth, bitmapHeight, mMatrix, true);  //获取同等和背景宽高的指针图的bitmap

    paint = new Paint();
    paint.setTextSize(18 * ScreenUtil.getDensity(getContext()));
    paint.setAntiAlias(true);  //抗锯齿
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setColor(Color.WHITE);
}

public void refresh() {
    postInvalidateDelayed(ANIMATION_INTERVAL); //子线程刷新view
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (indicatorBitmap == null) {
        init();
    }
    mMatrix.setRotate(getAngle(World.dbCount), newWidth / 2, newHeight * 215 / 460);   //片相对位置
    canvas.drawBitmap(indicatorBitmap, mMatrix, paint);
    canvas.drawText((int) World.dbCount + " DB", newWidth / 2, newHeight * 36 / 46, paint); //图片相对位置
}

private float getAngle(float db) {
    return (db - 85) * 5 / 3;

}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,034评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,939评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,487评论 4 61
  • 我还是最喜欢盛华的那个时候――鸡不生蛋,鸟不拉屎,交通不便,组团儿吃饭,在宿舍阳台偷看那些腻歪的小情侣,没有王者,...
    邓怂怂阅读 1,648评论 0 0
  • 我见过不少的人觉得好事肯定是多磨的,而之前我也是如此认为的,但后来仔细想一次之后,我觉得并不是如此的。至少目前这种...
    海峰匠阅读 6,404评论 1 2

友情链接更多精彩内容