java - 如何在android java中使用PixelCopy API从View中获取位图

标签 java android

目前我正在使用 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()));
    }
无法解决回电
  • Activity
  • 窗口
  • 经办人

  • 哪个是 super 回调类?

    最佳答案

    查看这篇文章CallBack接口(interface)是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

    关于java - 如何在android java中使用PixelCopy API从View中获取位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65828719/

    相关文章:

    android - 将 horizo​​ntalScrollView 添加到底部导航栏

    java - 完成 future : several tasks

    java - 迁移到 Tomcat7 后,BeanUtils.populate 在 struts 中抛出 NullPointerException

    java - 将 JDK 添加到 netbeans 8.1

    java - 如何处理JavaFX标题栏文本太长?

    Android 后退键循环焦点而不是返回到上一个 Activity

    java - Android 中 ArrayList 的问题

    android - 用户空间应用程序如何控制 Android 中的硬件(位置/网络/Wifi)?

    java - 检测代词及其名词?

    android - 设置安卓 :value with a gradle properties on manifest