【问题标题】:How to use PixelCopy API in android java to get bitmap from View如何在android java中使用PixelCopy API从View中获取位图
【发布时间】:2021-01-21 13:42:07
【问题描述】:

目前我正在使用 view.getDrawingCache() 现在获取绘图缓存 已弃用

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

view.getDrawingCache() is deprecated in 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()));
    }

无法解决回调

  • 活动
  • 窗口
  • 处理程序

回调的超级类是什么?

【问题讨论】:

  • callback 是 PixelCopy.java Listener 内部的接口,用于观察 PixelCopy 请求的编译情况。

标签: java android


【解决方案1】:

查看本文CallBack接口是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);
}

https://medium.com/@shiveshmehta09/taking-screenshot-programmatically-using-pixelcopy-api-83c84643b02a

PixelCopy 的 Android 文档doc

【讨论】:

  • 感谢您节省我创建位图的时间 -@Mansukh Ahir
猜你喜欢
  • 2021-06-29
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 2022-01-10
  • 1970-01-01
相关资源
最近更新 更多