PixelFormat(像素格式)以及view的高度getX(),getRawX()以及Path所用

慢慢看你们不会领悟的

public class PixelFormat
{
    /* these constants need to match those in hardware/hardware.h */

    public static final int UNKNOWN     = 0;

    /** System chooses a format that supports translucency (many alpha bits) */
    public static final int TRANSLUCENT = -3;

    /**
     * System chooses a format that supports transparency
     * (at least 1 alpha bit)
     */
    public static final int TRANSPARENT = -2;

    /** System chooses an opaque format (no alpha bits required) */
    public static final int OPAQUE      = -1;

    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;

    @Deprecated
    public static final int RGBA_5551   = 6;
    @Deprecated
    public static final int RGBA_4444   = 7;
    @Deprecated
    public static final int A_8         = 8;
    @Deprecated
    public static final int L_8         = 9;
    @Deprecated
    public static final int LA_88       = 0xA;
    @Deprecated
    public static final int RGB_332     = 0xB;


    /**
     * @deprecated use {@link android.graphics.ImageFormat#NV16
     * ImageFormat.NV16} instead.
     */
    @Deprecated
    public static final int YCbCr_422_SP= 0x10;

    /**
     * @deprecated use {@link android.graphics.ImageFormat#NV21
     * ImageFormat.NV21} instead.
     */
    @Deprecated
    public static final int YCbCr_420_SP= 0x11;

    /**
     * @deprecated use {@link android.graphics.ImageFormat#YUY2
     * ImageFormat.YUY2} instead.
     */
    @Deprecated
    public static final int YCbCr_422_I = 0x14;

    /**
     * @deprecated use {@link android.graphics.ImageFormat#JPEG
     * ImageFormat.JPEG} instead.
     */
    @Deprecated
    public static final int JPEG        = 0x100;

    public static void getPixelFormatInfo(int format, PixelFormat info) {
        switch (format) {
            case RGBA_8888:
            case RGBX_8888:
                info.bitsPerPixel = 32;
                info.bytesPerPixel = 4;
                break;
            case RGB_888:
                info.bitsPerPixel = 24;
                info.bytesPerPixel = 3;
                break;
            case RGB_565:
            case RGBA_5551:
            case RGBA_4444:
            case LA_88:
                info.bitsPerPixel = 16;
                info.bytesPerPixel = 2;
                break;
            case A_8:
            case L_8:
            case RGB_332:
                info.bitsPerPixel = 8;
                info.bytesPerPixel = 1;
                break;
            case YCbCr_422_SP:
            case YCbCr_422_I:
                info.bitsPerPixel = 16;
                info.bytesPerPixel = 1;
                break;
            case YCbCr_420_SP:
                info.bitsPerPixel = 12;
                info.bytesPerPixel = 1;
                break;
            default:
                throw new IllegalArgumentException("unknown pixel format " + format);
        }
    }

    public static boolean formatHasAlpha(int format) {
        switch (format) {
            case PixelFormat.A_8:
            case PixelFormat.LA_88:
            case PixelFormat.RGBA_4444:
            case PixelFormat.RGBA_5551:
            case PixelFormat.RGBA_8888:
            case PixelFormat.TRANSLUCENT:
            case PixelFormat.TRANSPARENT:
                return true;
        }
        return false;
    }

    public int  bytesPerPixel;
    public int  bitsPerPixel;

    /**
     * Determine whether or not this is a public-visible and non-deprecated {@code format}.
     *
     * <p>In particular, {@code @hide} formats will return {@code false}.</p>
     *
     * <p>Any other indirect formats (such as {@code TRANSPARENT} or {@code TRANSLUCENT})
     * will return {@code false}.</p>
     *
     * @param format an integer format
     * @return a boolean
     *
     * @hide
     */
    public static boolean isPublicFormat(int format) {
        switch (format) {
            case RGBA_8888:
            case RGBX_8888:
            case RGB_888:
            case RGB_565:
                return true;
        }

        return false;
    }
}

优先考虑线性布局,再使用相对布局,优先考虑padding,在使用margin

做个笔记

getRawX()是相对于手机屏幕
getX()相对于自身左上角
getTop()相对于父空间

这里写图片描述

Path类中的几个方法用法


public void lineTo (float x, float y)
Added in API level 1

Add a line from the last point to the specified point (x,y). If no moveTo() call has been made for this contour, the first point is automatically set to (0,0).
Parameters
x   The x-coordinate of the end of a line
y   The y-coordinate of the end of a line
public void moveTo (float x, float y)
Added in API level 1

Set the beginning of the next contour to the point (x,y).
Parameters
x   The x-coordinate of the start of a new contour
y   The y-coordinate of the start of a new contour 


 public void quadTo (float x1, float y1, float x2, float y2)
Added in API level 1

Add a quadratic bezier from the last point, approaching control point (x1,y1), and ending at (x2,y2). If no moveTo() call has been made for this contour, the first point is automatically set to (0,0).
Parameters
x1  The x-coordinate of the control point on a quadratic curve
y1  The y-coordinate of the control point on a quadratic curve
x2  The x-coordinate of the end point on a quadratic curve
y2  The y-coordinate of the end point on a quadratic curve 
<think>嗯,用户现在问的是PixelFormat,我之前帮他解答过Android的window.setLayout(),可能他还在进行Android开发相关的工作。我需要先回忆一下PixelFormat在Android中的相关知识。 首先,PixelFormat主要处理像素的存储和显示方式,对吧?用户可能是遇到了图像显示的问题,比如颜色不对或者透明度处理有问题,或者想优化渲染性能。需要详细解释PixelFormat的作用和常见类型。 我应该先介绍PixelFormat的基本概念,说明它定义了像素在内存中的排列方式,包括颜色通道、透明度等。然后列举常见的格式,比如RGBA_8888、RGB_565、TRANSLUCENT等,解释它们的区别和应用场景。 用户可能想知道如何在实际开发中设置PixelFormat,比如在SurfaceView或TextureView中使用。需要给出代码示例,比如在Activity中设置窗口的PixelFormat,或者在自定义View中处理。还要提到OpenGL ES中的使用情况,因为游戏或高性能应用可能会用到。 另外,注意事项也很重要。比如不同设备的支持情况,性能与质量的权衡,兼容性问题,还有Android 8.0之后的变化。用户可能不知道这些细节,容易踩坑,需要提醒他们。 要确保回答结构清晰,分点说明,让用户能一步步理解。可能用户是中级开发者,已经了解基本UI布局,现在需要深入图形处理部分。需要避免使用过于专业的术语,但又要准确,比如解释每个格式的位数和优缺点。 最后,检查是否有遗漏的重要点,比如如何检测设备支持的格式,或者处理不兼容的情况。确保回答覆盖了常见使用场景和潜在问题,帮助用户全面理解PixelFormat的应用和注意事项。</think>在 Android 开发中,`PixelFormat` 定义了像素在内存中的存储和显示方式,直接影响图形渲染效果和性能。以下是结构化解析: --- ### 一、核心作用 1. **像素数据规范**:描述每个像素的颜色/透明度通道的排列方式 2. **显示适配**:匹配硬件屏幕特性(如 RGB 排列方式) 3. **性能优化**:通过选择合适格式降低内存占用和 GPU 负载 --- ### 二、常见格式类型 | 格式常量 | 位数 | 特性 | 典型场景 | |---------|------|-----|---------| | `PixelFormat.RGBA_8888` | 32bit | 4通道(红/绿/蓝/透明) | 高质量图像、透明效果 | | `PixelFormat.RGB_565` | 16bit | 无透明通道,色彩精度较低 | 性能敏感型界面 | | `PixelFormat.RGBX_8888` | 32bit | 无透明通道(保留未使用字节) | 无透明需求的图像处理 | | `PixelFormat.TRANSLUCENT` | 系统自动选择 | 强制支持透明度 | SurfaceView/GLSurfaceView | | `PixelFormat.OPAQUE` | 系统自动选择 | 不透明内容优化 | 无透明元素的渲染 | --- ### 三、代码实践 #### 1. 窗口级设置 ```java // 在 Activity 中设置窗口像素格式 getWindow().setFormat(PixelFormat.RGBA_8888); ``` #### 2. SurfaceView 配置 ```java SurfaceView surfaceView = new SurfaceView(this); surfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT); ``` #### 3. OpenGL ES 关联 ```java // EGL 配置选择时指定 int[] configSpec = { EGL10.EGL_RED_SIZE, 8, EGL10.EGL_GREEN_SIZE, 8, EGL10.EGL_BLUE_SIZE, 8, EGL10.EGL_ALPHA_SIZE, 8, EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL10.EGL_NONE }; ``` --- ### 四、关键注意事项 1. **设备兼容性**: - 不同硬件支持的格式存在差异(通过 `WindowManager.getSupportedPixelFormats()` 检测) - 低端设备可能不支持高精度格式 2. **性能取舍**: - `RGB_565` 比 `RGBA_8888` 减少 50% 内存占用 - 透明通道会增加 GPU 混合计算开销 3. **Android 8.0+ 变化**: - 默认使用 `PixelFormat.RGBA_8888` - 强制硬件加速时自动忽略某些格式设置 4. **视觉效果影响**: - `RGB_565` 可能出现色彩断层(Banding) - 无透明通道格式无法显示渐变透明效果 --- ### 五、调试技巧 1. 通过 `adb shell dumpsys window` 查看当前窗口格式 2. 使用 Android GPU Inspector 分析不同格式的渲染性能 3. 在 `onCreate()` 中调用 `getWindow().getAttributes().format` 验证实际生效格式 如需处理复杂图形需求(如视频解码),建议结合 `ImageFormat` 和 `HardwareBuffer` 使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值