2

我正在使用 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);
    }
}
4

2 回答 2

5

如何以编程方式在 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();
}

更新

感谢 tamim 指出这种弃用。这是更新的解决方案

public void setDrawingCacheEnabled(启用布尔值)

此方法在 API 级别 28 中已弃用。随着 API 11 中硬件加速渲染的引入,视图绘图缓存在很大程度上已经过时。使用硬件加速,中间缓存层在很大程度上是不必要的,并且很容易导致性能的净损失由于创建和更新图层的成本。在缓存层有用的极少数情况下,例如 alpha 动画,setLayerType(int, android.graphics.Paint) 通过硬件渲染来处理这个问题。对于视图层次结构的一小部分或单个视图的软件渲染快照,建议从位图或图片创建画布并在视图上调用 draw(android.graphics.Canvas)。但是,不鼓励使用这些软件渲染的用法,并且与仅硬件渲染功能(例如 Config.HARDWARE 位图、实时阴影和轮廓裁剪)存在兼容性问题。对于反馈报告或单元测试的 UI 屏幕截图,推荐使用 PixelCopy API。

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

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

在哪里,

surfaceViewObject 是表面视图的对象

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

监听器是 OnPixelCopyFinishedListener

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

于 2015-04-24T11:28:56.833 回答
2

使用 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)
    }
}

我使用measuredWidthHeight 的原因是因为当您有可滚动视图或ViewGroup截屏时。

于 2019-11-07T12:17:18.640 回答