android - 在 MPAndroidChart 中将图表保存为图像

标签 android mpandroidchart save-image

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

代码如下:

<item android:id="@+id/save"
    android:icon="@drawable/ic_action_accept"
    android:title="@string/save"
    app:showAsAction="always"/>
<小时/>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
        case R.id.save :
            mlineChart.saveToGallery("Chart",50);
            return true;
        case R.id.action_settings :
            return true;
        default: return super.onOptionsItemSelected(item);
    }
}

最佳答案

来自 How to programatically take a screenshot on Android?

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   
// create bitmap screen capture
Bitmap bitmap;
View v1 = mWebview.getRootView(); // take the view from your webview
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
  fout = new FileOutputStream(imageFile);
  bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
  fout.flush();
  fout.close();

} catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

更新

感谢塔米姆指出这一弃用。这是更新的解决方案

public void setDrawingCacheEnabled( bool 值启用)

This method was deprecated in API level 28. The view drawing cache was largely made obsolete with the introduction of hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache layers are largely unnecessary and can easily result in a net loss in performance due to the cost of creating and updating the layer. In the rare cases where caching layers are useful, such as for alpha animations, setLayerType(int, android.graphics.Paint) handles this with hardware rendering. For software-rendered snapshots of a small part of the View hierarchy or individual Views it is recommended to create a Canvas from either a Bitmap or Picture and call draw(android.graphics.Canvas) on the View. However these software-rendered usages are discouraged and have compatibility issues with hardware-only rendering features such as Config.HARDWARE bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback reports or unit testing the PixelCopy API is recommended.

因此,根据建议使用 PixelCopy用于获取 UI 的屏幕截图/快照。它适用于 API 级别 24 及以上

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

哪里,

surfaceViewObject是surface view的对象

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

监听器是OnPixelCopyFinishedListener

有关更多信息,请参阅 Pixel Copy Android Documentation

关于android - 在 MPAndroidChart 中将图表保存为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29846146/

相关文章:

android - MPAndroidChart 折线图小尺寸

java - 尝试为 Android 中已有的类创建一个扩展类

c# - 如何在 WPF 中使用 OpenDialog 将文件复制到特定目录并设置文件名、扩展名?

javascript - 如何使带有文本的 html div 图像可供用户下载/保存?

android - Google Play 应用内购买返回错误代码 -1008 : null puchaseData or dataSignature

android - 如何调整 fab 按钮内图标的大小

android - lateinit的isInitialized属性在伴侣对象中不起作用

android - 从字符串中删除空格

android - MPAndroidChart PieChart 在 ScrollView 内部使用时不显示

android - 以 block 的形式保存位图图像