2

I am creating a custom component in HarmonyOS using Java SDK, Where I need one attribute to get Element(i.e. VectorElement or png format icon) as input from xml layout file.

for ex:

ohos:sliderIcon="$graphic:custom_icon" (VectorElement)
ohos:sliderIcon="$media:ic_arrow"      (PNG format icon)

Now I am getting element in custom component class like this

Element sliderIcon = attrSet.getAttr(Attribute.SLIDER_ICON).isPresent()
                    ? attrSet.getAttr(Attribute.SLIDER_ICON).get().getElement()
                    : new VectorElement(getContext(), ResourceTable.Graphic_slidetoact_ic_arrow);

but, above code is working only for VectorElement icon NOT for PNG format icon.

And I am able to set both VectorElement and PNG format icon at runtime from java code as follow:

For VectorElement:

Element sliderIcon = new VectorElement(getContext(), ResourceTable.Graphic_custom_icon);

For PNG format icon:

//You can set png format icon using PixelMapElement.
     try {
            Element sliderIcon = new PixelMapElement(getResourceManager().getResource(ResourceTable.Media_ic_arrow));
     } catch (IOException | NotExistException e) {
            e.printStackTrace();
     } 

So my question is

How to get PNG format icon as a attribute in custom component?

OR

How to get resource/reference id of passed Element?

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
C2C
  • 85
  • 6

2 Answers2

0

Just tried your method to get pixelMap through PixelMapElement,

PixelMapElement pixelMapElement = (PixelMapElement) attrSet.getAttr("sliderIcon").get().getElement();
PixelMap pixelMap = pixelMapElement.getPixelMap();

Tested in the onDraw method and proved to be available.

@Override
public void onDraw(Component component, Canvas canvas) {
    PixelMapHolder pixelMapHolder = new PixelMapHolder(pixelMap);
    canvas.drawPixelMapHolder(pixelMapHolder,0,0,new Paint());
}
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
0
Element sliderIcon = attrSet.getAttr(Attribute.SLIDER_ICON).isPresent()
                    ? attrSet.getAttr(Attribute.SLIDER_ICON).get().getElement()
                    : new VectorElement(getContext(), ResourceTable.Graphic_slidetoact_ic_arrow);

This solution is working for both type of element VectorElement and PixleMapElement(PNG format icon). It's my bad I am not doing it properly, I did mistake in xml file ohos:sliderIcon="$Media:ic_arrow" we have to use small m in media. like this ohos:sliderIcon="$media:ic_arrow"

C2C
  • 85
  • 6