我正在使用 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/