自定义View系列教程05--自定义View示例分析

本文深入探讨自定义View的实现,通过两个示例详细解析手动测量View和实现布局切换的方法。示例1展示了一个点击标题展开/收缩图片的View,通过手动测量实现内容的渐显渐隐和箭头旋转。示例2实现了一个自动换行的FlowLayout,覆盖了测量和布局的逻辑,允许标签按需换行显示。

版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.youkuaiyun.com/lfdfhl

自定义View概述

之前结合源码分析完了自定义View的三个阶段:measure,layout,draw。
那么,自定义有哪几种常见的方式呢?

  1. 直接继承自View
    在使用该方式实现自定义View时通常的核心操作都在onDraw( )当中进行。但是,请注意,在分析measure部分源码的时候,我们提到如果直接继承自View在onMeasure( )中要处理view大小为wrap_content的情况,否则这种情况下的大小和match_parent一样。除此以为,还需要注意对于padding的处理。

  2. 继承自系统已有的View
    比如常见的TextView,Button等等。如果采用该方式,我们只需要在系统控件的基础上做出一些调整和扩展即可,而且也不需要去自己支持wrap_content和padding。

  3. 直接继承自ViewGroup
    如果使用该方式实现自定义View,请注意两个问题
    第一点:
    在onMeasure( )实现wrap_content的支持。这点和直接继承自View是一样的。
    第二点:
    在onMeasure( )和onLayout中需要处理自身的padding以及子View的margin

  4. 继承自系统已有的ViewGroup
    比如LinearLayout,Re

#include <stddef.h> #ifndef FASTERSTEREOCUDA_STEREO_H_ #define FASTERSTEREOCUDA_STEREO_H_ #ifndef FASTERSTEREOCUDA_EXPORTS #define FASTERSTEREOCUDA_DLL __declspec(dllimport) #else #define FASTERSTEREOCUDA_DLL __declspec(dllexport) #endif /** * \brief faster stereo algorithm based on cuda * Besides cuda, other optimization strategies are used, such as hierarchical optimization */ // class __declspec(dllexport) FasterStereoCuda class FasterStereoCuda { public: /**\brief parameters of polar image pairs*/ typedef struct EpipolarStereoParam{ double x0_left, y0_left; // image coordinates of principal point(left view) double x0_right, y0_right; // image coordinates of principal point(right view) double baseline; // baseline(mm) double focus; // focal length in pixel units EpipolarStereoParam(){ x0_left = y0_left = x0_right = y0_right = focus = baseline = 0; } }epi_ste_t; /**\brief parameters of stereo algorithm (for generating disparity map)*/ typedef struct StereoOption1 { int width; // width of image int height; // height of image int min_disp; // minimum disparity int max_disp; // maximum disparity int num_layers; // matching layers(the more layers the faster) bool do_lr_check; // whether to doing consistency check bool do_rm_peaks; // whether to removing peaks bool do_smooth; // whether to smoothing disparity map StereoOption1() : width(0), height(0), min_disp(0), max_disp(0), num_layers(1), do_lr_check(true),do_rm_peaks(true), do_smooth(false) {}; }ste_opt1_t; /**\brief parameters of stereo algorithm (for generating depth map)*/ typedef struct StereoOption2 { int width; // width of image int height; // height of image float min_depth; // minimum disparity float max_depth; // maximum disparity int num_layers; // matching layers(the more layers the faster) bool do_lr_check; // whether to doing consistency check bool do_rm_peaks; // whether to removing peaks bool do_smooth; // whether to smoothing disparity map epi_ste_t epi; // parameters of polar image pairs StereoOption2(): width(0), height(0), min_depth(0), max_depth(0), num_layers(1), do_lr_check(true), do_rm_peaks(true), do_smooth(false){} ; }ste_opt2_t; public: FasterStereoCuda(); ~FasterStereoCuda(); /** * \brief alloc page-locked memory on the host * by this way, you can get memory on the host with faster transmission speed between host(CPU) and device(GPU) * \param ptr device pointer to allocated memory * \param size requested allocation size in bytes * \return true: succeed false: filed */ static bool MallocPageLockedPtr(void** ptr, size_t size); /** * \brief frees page-locked memory * \param ptr pointer to memory to free * \return true: succeed false: filed */ static bool FreePageLockedPtr(void* ptr); public: /** * \brief initialization 1, for generating disparity map * \param option in, parameters of algorithm * \return true: succeed false: filed */ bool Init(const ste_opt1_t& option) const; /** * \brief initialization 2, for generating depth map * \param option in, parameters of algorithm * \return true: succeed false: filed */ bool Init2(const ste_opt2_t& option) const; /**\brief release*/ void Release() const; /** * \brief interface 1 : doing stereo matching to generate disparity map * \param bytes_left in��image data of left view * \param bytes_right in��image data of right view * \param disparity_data out��disparity map, the same size as image��you mast malloc memory in advance. invalid value : -999999.0f * \return true: succeed false: filed */ bool Match(const unsigned char* bytes_left, const unsigned char* bytes_right, float* disparity_data) const; /** * \brief interface 2 : doing stereo matching to generate depth map * \param bytes_left in��image data of left view * \param bytes_right in��image data of right view * \param depth_data out��depth map, the same size as image��you mast malloc memory in advance. invalid value : -999999.0f * \return true: succeed false: filed */ bool Match2(const unsigned char* bytes_left, const unsigned char* bytes_right, float* depth_data) const; private: void* impl_; }; 注释上述代码
最新发布
09-23
这段代码定义了一个名为`FasterStereoCuda`的类,用于实现基于CUDA的快速立体视觉算法。以下是代码的详细注释: ```cpp // 防止头文件重复包含 #ifndef FASTERSTEREOCUDA_STEREO_H_ #define FASTERSTEREOCUDA_STEREO_H_ // 导入标准库头文件 #include <stddef.h> // 定义DLL导入导出宏 #ifndef FASTERSTEREOCUDA_EXPORTS #define FASTERSTEREOCUDA_DLL __declspec(dllimport) #else #define FASTERSTEREOCUDA_DLL __declspec(dllexport) #endif /** * \brief 基于CUDA的快速立体视觉算法 * 除了CUDA外,还使用了其他优化策略,如分层优化 */ class FasterStereoCuda { public: /**\brief 极线图像对的参数*/ typedef struct EpipolarStereoParam{ double x0_left, y0_left; // 左视图主点坐标 double x0_right, y0_right; // 右视图主点坐标 double baseline; // 基线长度(mm) double focus; // 焦距(像素单位) EpipolarStereoParam(){ x0_left = y0_left = x0_right = y0_right = focus = baseline = 0; } }epi_ste_t; /**\brief 立体视觉算法参数(用于生成视差图)*/ typedef struct StereoOption1 { int width; // 图像宽度 int height; // 图像高度 int min_disp; // 最小视差 int max_disp; // 最大视差 int num_layers; // 匹配层数(层数越多越快) bool do_lr_check; // 是否进行一致性检查 bool do_rm_peaks; // 是否去除峰值 bool do_smooth; // 是否平滑视差图 StereoOption1() : width(0), height(0), min_disp(0), max_disp(0), num_layers(1), do_lr_check(true),do_rm_peaks(true), do_smooth(false) {}; }ste_opt1_t; /**\brief 立体视觉算法参数(用于生成深度图)*/ typedef struct StereoOption2 { int width; // 图像宽度 int height; // 图像高度 float min_depth; // 最小深度 float max_depth; // 最大深度 int num_layers; // 匹配层数(层数越多越快) bool do_lr_check; // 是否进行一致性检查 bool do_rm_peaks; // 是否去除峰值 bool do_smooth; // 是否平滑深度图 epi_ste_t epi; // 极线图像对参数 StereoOption2(): width(0), height(0), min_depth(0), max_depth(0), num_layers(1), do_lr_check(true), do_rm_peaks(true), do_smooth(false){} ; }ste_opt2_t; public: FasterStereoCuda(); // 构造函数 ~FasterStereoCuda(); // 析构函数 /** * \brief 在主机上分配页锁定内存 * 这种方式可以获得主机上传输速度更快的内存(CPU和GPU之间) * \param ptr 指向分配内存的设备指针 * \param size 请求分配的字节大小 * \return true: 成功 false: 失败 */ static bool MallocPageLockedPtr(void** ptr, size_t size); /** * \brief 释放页锁定内存 * \param ptr 要释放的内存指针 * \return true: 成功 false: 失败 */ static bool FreePageLockedPtr(void* ptr); public: /** * \brief 初始化1,用于生成视差图 * \param option 输入,算法参数 * \return true: 成功 false: 失败 */ bool Init(const ste_opt1_t& option) const; /** * \brief 初始化2,用于生成深度图 * \param option 输入,算法参数 * \return true: 成功 false: 失败 */ bool Init2(const ste_opt2_t& option) const; /**\brief 释放资源*/ void Release() const; /** * \brief 接口1:进行立体匹配生成视差图 * \param bytes_left 输入,左视图图像数据 * \param bytes_right 输入,右视图图像数据 * \param disparity_data 输出,视差图(与图像大小相同),需预先分配内存。无效值: -999999.0f * \return true: 成功 false: 失败 */ bool Match(const unsigned char* bytes_left, const unsigned char* bytes_right, float* disparity_data) const; /** * \brief 接口2:进行立体匹配生成深度图 * \param bytes_left 输入,左视图图像数据 * \param bytes_right 输入,右视图图像数据 * \param depth_data 输出,深度图(与图像大小相同),需预先分配内存。无效值: -999999.0f * \return true: 成功 false: 失败 */ bool Match2(const unsigned char* bytes_left, const unsigned char* bytes_right, float* depth_data) const; private: void* impl_; // 实现类的指针(隐藏实现细节) }; ```
评论 60
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值