Android布局到位图渲染在Activity上下文中不起作用

问题描述

我有以下代码,可对布局文件进行充气并将其渲染为位图。

如果parent是AppCompatActivity的实例,则一切正常。 如果我将方法签名更改为createRouteBitmapFromLayout(Activity parent,ColorStateList color),并从扩展了android.app.Activity的活动中调用它,那么我会得到一个正确大小的空位图。

回顾一下,唯一改变的是AppCompatActivity =>活动。

private static Bitmap createRouteBitmapFromLayout(AppCompatActivity parent,ColorStateList color) {
    View newViewElement = View.inflate(parent,R.layout.icon_node_topo_display,null);
    ((ImageView) newViewElement.findViewById(R.id.imagePin)).setimageTintList(color);

    newViewElement.measure(IconType.poiRouteIcon.measuredWidth,IconType.poiRouteIcon.measuredHeight);
    newViewElement.layout(0,newViewElement.getMeasuredWidth(),newViewElement.getMeasuredHeight());

    Bitmap bitmap = Bitmap.createBitmap(newViewElement.getMeasuredWidth(),newViewElement.getMeasuredHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.setDrawFilter(new PaintFlagsDrawFilter(0,Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));

    Drawable background = newViewElement.getBackground();

    if (background != null) {
        background.draw(canvas);
    }
    newViewElement.draw(canvas);
    return Bitmap.createScaledBitmap(bitmap,IconType.poiRouteIcon.iconPxWith,IconType.poiRouteIcon.iconPxHeight,true);
}

感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...