【问题标题】:Rendering a layout onto a small bitmap (Android)将布局渲染到小位图 (Android)
【发布时间】:2014-12-03 04:22:30
【问题描述】:

我想放大布局,将其绘制到 96x96 位图上,然后将其发送到 MetaWatch,它的 96x96 黑白屏幕具有没有灰度渐变。事实上,我已经做到了,但我对结果并不满意:字符要么太大要么太细且难以阅读。

如果我将视图渲染到 96x96 位图上,我会在小屏幕上看到非常大的字母:

public void sendView(View v) {
    Bitmap bm = createViewBitmap(v, 1, 1);
    sendLcdBitmap(bm);
    bm.recycle();
}

如果我将视图渲染到更大的位图上并通过调用sendViewScaled(R.layout.test_layout, 1, 4) 将其缩小,则缩放后的文本看起来不太好。也就是说,它可以更小,但如果我把它做得更小,它会变得太薄。

public void sendViewScaled(View v, int num, int denom) {
    Bitmap bm = createViewBitmap(v, num, denom);
    Bitmap bm2 = Bitmap.createScaledBitmap(bm, SCREEN_SIZE, SCREEN_SIZE, false);
    sendLcdBitmap(bm2);
    bm2.recycle();
    bm.recycle();
}
public void sendViewScaled(int id, int num, int denom) {
    sendViewScaled(inflateView(id), num, denom);
}

将视图转换为位图的函数是:

protected Bitmap createViewBitmap(View v, int num, int denom) {
    v.measure(MeasureSpec.makeMeasureSpec(SCREEN_SIZE*num/denom, MeasureSpec.EXACTLY)
             ,MeasureSpec.makeMeasureSpec(SCREEN_SIZE*num/denom, MeasureSpec.EXACTLY)
             );
    Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.setDensity(Bitmap.DENSITY_NONE);//DisplayMetrics.DENSITY_xxx;
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    v.draw(c);
    return b;

}

函数sendLcdBitmap()使用函数将ARGB_8888转换为单比特位图:

protected boolean isRatherWhite(int color) {
    return Color.red(color)+Color.green(color)+Color.blue(color) > 127*3;
    //return color == Color.WHITE;
}

所以,问题是:

我想将像 R.layout.test_layout 这样的布局渲染到 96x96 位图上,黑白,没有灰度渐变。如何获得优质的产品?

相关问题:
Converting a view to Bitmap without displaying it in Android?
getMeasuredWidth returns totally wrong value
How to Resize a Bitmap in Android?
How to convert Views to bitmaps?
http://www.skoumal.net/en/android-how-draw-text-bitmap

更新:使用 isRatherWhite() 中使用的值有所帮助。

【问题讨论】:

    标签: android android-layout fonts bitmap metawatch


    【解决方案1】:

    我认为您的问题在于缩小比例。你可以尝试不同的算法,比如这里提到的算法:What is the best image downscaling algorithm (quality-wise)?

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多