Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将图表另存为MPAndroidChart中的图像_Android_Mpandroidchart_Save Image - Fatal编程技术网

将图表另存为MPAndroidChart中的图像

将图表另存为MPAndroidChart中的图像,android,mpandroidchart,save-image,Android,Mpandroidchart,Save Image,我正在使用MPAndroidChart来呈现各种图表。我想添加将图表作为图像保存到图库的功能。我在操作栏中添加了一个图标以使用此功能,但图像不会保存到库中 代码如下: <item android:id="@+id/save" android:icon="@drawable/ic_action_accept" android:title="@string/save" app:showAsAction="always"/> 从 更新 谢谢塔米姆指出这一点。这是更

我正在使用MPAndroidChart来呈现各种图表。我想添加将图表作为图像保存到图库的功能。我在操作栏中添加了一个图标以使用此功能,但图像不会保存到库中

代码如下:

<item android:id="@+id/save"
    android:icon="@drawable/ic_action_accept"
    android:title="@string/save"
    app:showAsAction="always"/>

更新

谢谢塔米姆指出这一点。这是更新后的解决方案

公共void setDrawingCacheEnabled(已启用布尔值)

此方法在API级别28中被弃用。视图图形缓存已关闭 随着硬件的引入,基本上已经过时 API 11中的渲染。通过硬件加速,中间缓存 分层在很大程度上是不必要的,并且很容易导致成本的净损失 由于创建和更新层的成本而导致的性能。在 缓存层非常有用的罕见情况,例如alpha 动画,setLayerType(int,android.graphics.Paint)处理这个问题 使用硬件渲染。对于软件渲染的小快照 视图层次的一部分或建议使用的单个视图 从位图或图片创建画布并调用 在视图上绘制(android.graphics.Canvas)。然而这些 不鼓励使用软件呈现,并且存在兼容性问题 使用仅硬件渲染功能,如Config.hardware位图, 实时阴影和轮廓剪裁。查看的用户界面截图 建议使用反馈报告或单元测试PixelCopy API

因此,根据用于获取UI屏幕截图/快照的建议,它在API级别24及更高版本中可用

PixelCopy.request(surfaceViewObject,BitmapDest,listener,new Handler());
在哪里,

SurfaceView对象是曲面视图的对象

BitmapDest是将保存图像的位图对象,它不能为空

监听器已启用PixelCopyFinishedListener


有关更多信息,请参阅使用Kotlin扩展,我可以通过以下方式实现:

//create a bitmap using view height and width to draw to it
fun View.getBitmap(): Bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888).also {
    //create a canvas for this bitmap (it)
    Canvas(it).apply {
        //if the view has a background, draw it to the canvas, else draw a white screen 
        //because canvas default background is black
        background?.draw(this) ?: drawColor(Color.WHITE)
        //draw the view to the canvas
        draw(this)
    }
}

我之所以使用
measuredWidth
和Height,是因为当您有一个可滚动的视图或
ViewGroup
时,屏幕截图会被剪裁。

您在清单中是否有写入外部存储权限添加权限:Dsetdrawingcacheenabled现在在api 28上已被弃用,是否还有其他方法可以安全使用?感谢您的解决方案。如果您想在不使用PixelCopy的情况下实现这一点,请检查我的答案,因为在某些情况下,您需要直接使用位图,而不是通过回调。
PixelCopy.request(surfaceViewObject,BitmapDest,listener,new Handler());
//create a bitmap using view height and width to draw to it
fun View.getBitmap(): Bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888).also {
    //create a canvas for this bitmap (it)
    Canvas(it).apply {
        //if the view has a background, draw it to the canvas, else draw a white screen 
        //because canvas default background is black
        background?.draw(this) ?: drawColor(Color.WHITE)
        //draw the view to the canvas
        draw(this)
    }
}