mirror of
https://github.com/lvgl/lvgl.git
synced 2025-01-14 06:42:58 +08:00
add lv_slider_set_img to display an image on the knob
This commit is contained in:
parent
2bd600b68b
commit
a181a1feb5
@ -98,7 +98,7 @@ lv_obj_t * lv_bar_create(lv_obj_t * par, const lv_obj_t * copy)
|
||||
/*Init the new bar object*/
|
||||
if(copy == NULL) {
|
||||
lv_obj_set_click(new_bar, false);
|
||||
lv_obj_set_size(new_bar, LV_DPI * 2, LV_DPI / 3);
|
||||
lv_obj_set_size(new_bar, LV_DPI * 2, LV_DPI / 4);
|
||||
lv_bar_set_value(new_bar, ext->cur_value, false);
|
||||
|
||||
lv_theme_t * th = lv_theme_get_current();
|
||||
|
@ -1117,7 +1117,7 @@ static void lv_chart_draw_areas(lv_obj_t * chart, const lv_area_t * mask)
|
||||
int16_t mask_fade_id = LV_MASK_ID_INV;
|
||||
lv_draw_mask_fade_param_t mask_fade_p;
|
||||
if(ext->type & LV_CHART_TYPE_AREA_FADED) {
|
||||
lv_draw_mask_fade_init(&mask_fade_p, &chart->coords, LV_OPA_COVER, chart->coords.y1 + (h >> 2), LV_OPA_TRANSP, chart->coords.y2 - (h >> 2));
|
||||
lv_draw_mask_fade_init(&mask_fade_p, &chart->coords, LV_OPA_90, chart->coords.y1 + (h >> 2), LV_OPA_10, chart->coords.y2 - (h >> 2));
|
||||
mask_fade_id = lv_draw_mask_add(&mask_fade_p, NULL);
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include "../lv_draw/lv_draw.h"
|
||||
#include "../lv_themes/lv_theme.h"
|
||||
#include "../lv_misc/lv_math.h"
|
||||
#include "lv_img.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
@ -71,6 +72,7 @@ lv_obj_t * lv_slider_create(lv_obj_t * par, const lv_obj_t * copy)
|
||||
|
||||
/*Initialize the allocated 'ext' */
|
||||
ext->style_knob = &lv_style_pretty;
|
||||
ext->knob_img = NULL;
|
||||
|
||||
/*The signal and design functions are not copied so set them here*/
|
||||
lv_obj_set_signal_cb(new_slider, lv_slider_signal);
|
||||
@ -108,6 +110,22 @@ lv_obj_t * lv_slider_create(lv_obj_t * par, const lv_obj_t * copy)
|
||||
* Setter functions
|
||||
*====================*/
|
||||
|
||||
/**
|
||||
* Set an image to display on the knob of the slider
|
||||
* @param slider pointer to a slider object
|
||||
* @param img_src pointer to an `lv_img_dsc_t` variable or a path to an image (not an `lv_img` object)
|
||||
*/
|
||||
void lv_slider_set_knob_img(lv_obj_t * slider, const void * img_src)
|
||||
{
|
||||
LV_ASSERT_OBJ(slider, LV_OBJX_NAME);
|
||||
|
||||
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
|
||||
|
||||
ext->knob_img = img_src;
|
||||
lv_obj_refresh_ext_draw_pad(slider);
|
||||
lv_obj_invalidate(slider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a style of a slider
|
||||
* @param slider pointer to a slider object
|
||||
@ -158,6 +176,20 @@ bool lv_slider_is_dragged(const lv_obj_t * slider)
|
||||
return ext->dragging ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an image to display on the knob of the slider
|
||||
* @param slider pointer to a slider object
|
||||
* @return the image source: pointer to an `lv_img_dsc_t` variable or a path to an image (not an `lv_img` object)
|
||||
*/
|
||||
const void * lv_slider_get_knob_img(lv_obj_t * slider, const void * img_src)
|
||||
{
|
||||
LV_ASSERT_OBJ(slider, LV_OBJX_NAME);
|
||||
|
||||
lv_slider_ext_t * ext = lv_obj_get_ext_attr(slider);
|
||||
|
||||
return ext->knob_img;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a style of a slider
|
||||
* @param slider pointer to a slider object
|
||||
@ -261,6 +293,25 @@ static lv_design_res_t lv_slider_design(lv_obj_t * slider, const lv_area_t * cli
|
||||
knob_area.y2 += style_knob->body.padding.bottom;
|
||||
|
||||
lv_draw_rect(&knob_area, clip_area, style_knob, opa_scale);
|
||||
|
||||
if(ext->knob_img) {
|
||||
lv_res_t res;
|
||||
lv_img_header_t info;
|
||||
res = lv_img_decoder_get_info(ext->knob_img, &info);
|
||||
if(res == LV_RES_OK) {
|
||||
lv_coord_t x_ofs = knob_area.x1 + (lv_area_get_width(&knob_area) - info.w) / 2;
|
||||
lv_coord_t y_ofs = knob_area.y1 + (lv_area_get_height(&knob_area) - info.h) / 2;
|
||||
lv_area_t a;
|
||||
a.x1 = x_ofs;
|
||||
a.y1 = y_ofs;
|
||||
a.x2 = info.w - 1 + x_ofs;
|
||||
a.y2 = info.h - 1 + y_ofs;
|
||||
|
||||
lv_draw_img(&a, clip_area, ext->knob_img, style_knob, 0, LV_IMG_ZOOM_NONE, false, opa_scale);
|
||||
} else {
|
||||
LV_LOG_WARN("lv_slider_design: can't get knob image info")
|
||||
}
|
||||
}
|
||||
}
|
||||
/*Post draw when the children are drawn*/
|
||||
else if(mode == LV_DESIGN_DRAW_POST) {
|
||||
@ -355,6 +406,19 @@ static lv_res_t lv_slider_signal(lv_obj_t * slider, lv_signal_t sign, void * par
|
||||
knob_size += knob_style->body.shadow.width + knob_style->body.shadow.spread;
|
||||
knob_size += LV_MATH_MAX(LV_MATH_ABS(knob_style->body.shadow.offset.x), LV_MATH_ABS(knob_style->body.shadow.offset.y));
|
||||
|
||||
if(ext->knob_img) {
|
||||
lv_img_header_t info;
|
||||
lv_res_t res;
|
||||
res = lv_img_decoder_get_info(ext->knob_img, &info);
|
||||
if(res == LV_RES_OK) {
|
||||
knob_size = LV_MATH_MAX(knob_size, info.w);
|
||||
knob_size = LV_MATH_MAX(knob_size, info.h);
|
||||
} else {
|
||||
LV_LOG_WARN("slider signal (LV_SIGNAL_REFR_EXT_DRAW_PAD): can't get knob image info")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
lv_coord_t bg_size = bg_style->body.shadow.width + bg_style->body.shadow.spread;
|
||||
bg_size += LV_MATH_MAX(LV_MATH_ABS(bg_style->body.shadow.offset.x), LV_MATH_ABS(bg_style->body.shadow.offset.y));
|
||||
|
||||
|
@ -42,6 +42,7 @@ typedef struct
|
||||
lv_bar_ext_t bar; /*Ext. of ancestor*/
|
||||
/*New data for this type */
|
||||
const lv_style_t * style_knob; /*Style of the knob*/
|
||||
const void * knob_img;
|
||||
uint8_t dragging :1; /*1: the slider is being dragged*/
|
||||
} lv_slider_ext_t;
|
||||
|
||||
@ -102,6 +103,13 @@ static inline void lv_slider_set_anim_time(lv_obj_t * slider, uint16_t anim_time
|
||||
lv_bar_set_anim_time(slider, anim_time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an image to display on the knob of the slider
|
||||
* @param slider pointer to a slider object
|
||||
* @param img_src pointer to an `lv_img_dsc_t` variable or a path to an image
|
||||
*/
|
||||
void lv_slider_set_knob_img(lv_obj_t * slider, const void * img_src);
|
||||
|
||||
/**
|
||||
* Set the animation time of the slider
|
||||
* @param slider pointer to a bar object
|
||||
@ -158,6 +166,13 @@ static inline int16_t lv_slider_get_max_value(const lv_obj_t * slider)
|
||||
*/
|
||||
bool lv_slider_is_dragged(const lv_obj_t * slider);
|
||||
|
||||
/**
|
||||
* Get an image to display on the knob of the slider
|
||||
* @param slider pointer to a slider object
|
||||
* @return the image source: pointer to an `lv_img_dsc_t` variable or a path to an image (not an `lv_img` object)
|
||||
*/
|
||||
const void * lv_slider_get_knob_img(lv_obj_t * slider, const void * img_src);
|
||||
|
||||
/**
|
||||
* Get the animation time of the slider
|
||||
* @param slider pointer to a slider object
|
||||
|
Loading…
x
Reference in New Issue
Block a user