Android 中 PixelFormat 定义

PixelFormat定义:

   public static final int RGBA_8888  = 1;
   public static final int RGBX_8888  = 2;
   public static final int RGB_888    = 3;
   public static final int RGB_565    = 4;
 
   public static final int RGBA_5551  = 6;
   public static final int RGBA_4444  = 7;
   public static final int A_8         = 8;
   public static final int L_8         = 9;
   public static final int LA_88       = 0xA;
   public static final int RGB_332    = 0xB;

Android 中 Bitmap 灰度设置

  private Bitmap bitmap2Gray(Bitmap bmSrc) {
        int width = bmSrc.getWidth();
        int height = bmSrc.getHeight();
        Bitmap bmpGray = null;
        bmpGray = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bmpGray);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmSrc, 0, 0, paint);
        return bmpGray;
    }

Android 使用 Surface 截屏

    /**
     * Takes a screenshot of the current display and shows an animation.
     */
    void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) {
        // We need to orient the screenshot correctly (and the Surface api seems to take screenshots
        // only in the natural orientation of the device :!)
        mDisplay.getRealMetrics(mDisplayMetrics);
        float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels};
        float degrees = getDegreesForRotation(mDisplay.getRotation());
        boolean requiresRotation = (degrees > 0);
        if (requiresRotation) {
            // Get the dimensions of the device in its native orientation
            mDisplayMatrix.reset();
            mDisplayMatrix.preRotate(-degrees);
            mDisplayMatrix.mapPoints(dims);
            dims[0] = Math.abs(dims[0]);
            dims[1] = Math.abs(dims[1]);
        }

        // Take the screenshot
        mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);
        if (mScreenBitmap == null) {
            notifyScreenshotError(mContext, mNotificationManager);
            finisher.run();
            return;
        }

        if (requiresRotation) {
            // Rotate the screenshot to the current orientation
            Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels,
                    mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(ss);
            c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
            c.rotate(degrees);
            c.translate(-dims[0] / 2, -dims[1] / 2);
            c.drawBitmap(mScreenBitmap, 0, 0, null);
            c.setBitmap(null);
            // Recycle the previous bitmap
            mScreenBitmap.recycle();
            mScreenBitmap = ss;
        }

        // Optimizations
        mScreenBitmap.setHasAlpha(false);
        mScreenBitmap.prepareToDraw();

        // Start the post-screenshot animation
        startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
                statusBarVisible, navBarVisible);
    }

    private float getDegreesForRotation(int value) {
         switch (value) {
         case Surface.ROTATION_90:
               return 360f - 90f;
         case Surface.ROTATION_180:
               return 360f - 180f;
         case Surface.ROTATION_270:
               return 360f - 270f;
         }
         return 0f;
     }

ref: frameworks/base/packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java

Android Framework 中使用代码关闭和打开屏幕旋转功能

 ContentResolver resolver = getContext().getContentResolver();
  //关闭
  Settings.System.putInt(resolver, Settings.System.ACCELEROMETER_ROTATION, 0);
  //打开
  Settings.System.putInt(resolver, Settings.System.ACCELEROMETER_ROTATION, 1);

这种方法的效果相当于用户在设置中对屏幕旋转选项的操作。

如果你工作在Framework层,还有一个更灵活的 方法

Android 中 OrientationEventListener 的使用

 class MyOrientationEventListener extends OrientationEventListener {
        public MyOrientationEventListener(Context ctx, int rate) {
            super(ctx, rate);
        }

        @Override
        public void onOrientationChanged(int orientation) {
            if (orientation == 0 || orientation == 90 || orientation == 270) {
                Log.d("NotificationPanelView", "onOrientationChanged");
            }
        }
    }

 boolean mCanDetectOrientation;
 MyOrientationEventListener myOrientationEventLister;

 myOrientationEventLister = new MyOrientationEventListener(
                getContext(), SensorManager.SENSOR_DELAY_FASTEST);
        if (myOrientationEventLister.canDetectOrientation()) {
            mCanDetectOrientation = true;
        } else {
            mCanDetectOrientation = false;
        }

  if (mCanDetectOrientation)
                myOrientationEventLister.enable();

  if (mCanDetectOrientation)
                myOrientationEventLister.disable();