如何在Android Java中使用PixelCopy API从View获取位图

3

目前我正在使用 view.getDrawingCache(),但是现在获取绘图缓存的方法已经被弃用

view.setDrawingCacheEnabled(true);
Bitmap bitamp = Bitmap.createBitmap(view.getDrawingCache())
view.setDrawingCacheEnabled(false);

view.getDrawingCache()在Android API 28中已被弃用

Java代码的解决方案出现了错误Callback

@RequiresApi(api = Build.VERSION_CODES.O)
    public static void getBitmapFormView(View view, Activity activity, Callback callback) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);

        int[] locations = new int[2];
        view.getLocationInWindow(locations);
        Rect rect = new Rect(locations[0], locations[1], locations[0] + view.getWidth(), locations[1] + view.getHeight());

        PixelCopy.request(activity.getWindow(), rect, bitmap, copyResult -> {
            if (copyResult == PixelCopy.SUCCESS) {
                callback.onResult(bitmap);
            }
        }, new Handler(Looper.getMainLooper()));
    }

无法解析回调函数

  • 活动(Activity)
  • 窗口(Window)
  • 处理程序(Handler)

哪个是回调函数的超类?


回调是PixelCopy.java中的接口,用于观察PixelCopy请求的完成情况。 - undefined
1个回答

4

请查看这篇文章,回调接口是OnPixelCopyFinishedListener,可在PixalCopy.java文件中使用。

/**
 * Listener for observing the completion of a PixelCopy request.
 */
public interface OnPixelCopyFinishedListener {
    /**
     * Callback for when a pixel copy request has completed. This will be called
     * regardless of whether the copy succeeded or failed.
     *
     * @param copyResult Contains the resulting status of the copy request.
     * This will either be {@link PixelCopy#SUCCESS} or one of the
     * <code>PixelCopy.ERROR_*</code> values.
     */
    void onPixelCopyFinished(@CopyResultStatus int copyResult);
}

点击此处可查看有关使用PixelCopy API编程截图的文章。

有关PixelCopy的Android文档,请参见此处


1
谢谢你帮我节省了创建位图的时间 - @Mansukh Ahir - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接