语言

Menu
Sites
Language
Cairo draw PNG image?

Hi.

I'm trying to draw an PNG image to tizen phone.

Object inside PNG image is displayed good but transparent background of PNG image displayed in black

My code like bellow. Did i wrong something?

static void
win_resize_cb(void *data, Evas *e , Evas_Object *obj , void *event_info)
{
    appdata_s *ad = data;

	char img_path[255] = {0};
	int img_w, img_h;
	snprintf(img_path, sizeof(img_path) , ASSET_PATH"/chrome.png");
	ad->surface = cairo_image_surface_create_from_png(img_path);
	ad->cairo = cairo_create(ad->surface);
	img_w = cairo_image_surface_get_width (ad->surface);
	img_h = cairo_image_surface_get_height(ad->surface);

	evas_object_image_size_set(ad->img, img_w, img_h);
	evas_object_resize(ad->img, img_w, img_h);
	evas_object_show(ad->img);

	//cairo_set_operator(ad->cairo, CAIRO_OPERATOR_CLEAR);
	cairo_set_source_surface(ad->cairo, ad->surface, 0, 0);
	cairo_paint(ad->cairo);
	cairo_surface_destroy (ad->surface);

	unsigned char * imageData = cairo_image_surface_get_data(cairo_get_target(ad->cairo));
	evas_object_image_data_set(ad->img, imageData);
	evas_object_image_data_update_add(ad->img, 0, 0, img_w, img_h);
}

static bool
app_create(void *data)
{
	appdata_s *ad = data;

	/* Window */
	ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
	elm_win_autodel_set(ad->win, EINA_TRUE);

	if (elm_win_wm_rotation_supported_get(ad->win)) {
		int rots[4] = { 0, 90, 180, 270 };
		elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4);
	}

	evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
	eext_object_event_callback_add(ad->win, EEXT_CALLBACK_BACK, win_back_cb, ad);
	evas_object_event_callback_add(ad->win, EVAS_CALLBACK_RESIZE, win_resize_cb, ad);

	/* Show window after base gui is set up */
	evas_object_show(ad->win);

	ad->img = evas_object_image_filled_add(evas_object_evas_get(ad->win));
	evas_object_show(ad->img);

	return true;
}

 

编辑者为: Son Tien 27 11月, 2015

响应

4 回复
Jeongsu Kim

I have a question.

It seems that your sample code doesn't have any background and image object only. Do you want see other background application? Or you have a background that is not provided here?

If you just want see your background, use evas_object_image_alpha_set.
https://developer.tizen.org/dev-guide/2.4.0/org.tizen.native.mobile.apireference/group__Evas__Object__Image.html#ga5f0773379970c1b5e0d1c4d34f2fbbd4

If you want to see other backgound app too, use elm_win_alpha_set
https://developer.tizen.org/dev-guide/2.4.0/org.tizen.native.mobile.apireference/group__Win.html#ga58866fc5f3f5e66ed04a4326ffa1c682

colin Rao

the black background issue will fixed by evas_object_image_alpha_set(ad->img, EINA_TRUE);

Leo Liao

Yes, Colin's solution fixed your problem.

Below are information from wikipedia about Alpha channel and how PNG support transparency.

The concept of an alpha channel was introduced by Alvy Ray Smith in the late 1970s, and fully developed in a 1984 paper by Thomas Porter and Tom Duff.[1] In a 2D image element, which stores a color for each pixel, additional data is stored in the alpha channel with a value between 0 and 1. A value of 0 means that the pixel does not have any coverage information and is transparent; i.e. there was no color contribution from any geometry because the geometry did not overlap this pixel. A value of 1 means that the pixel is opaque because the geometry completely overlapped the pixel.

PNG offers a variety of transparency options. With true-color and grayscale images either a single pixel value can be declared as transparent or an alpha channel can be added (enabling any percentage of partial transparency to be used).

Leo Liao

BTW, here is the link you can find the usage of evas_object_image_alpha_set():

https://developer.tizen.org/development/api-references/native-application?redirect=https%3A//developer.tizen.org/dev-guide/2.4.0/org.tizen.native.mobile.apireference/group__Evas__Object__Image.html