harmonyos系统图片,【HarmonyOS】图片圆角功能

public class MainAbilitySlice extends AbilitySlice {

DirectionalLayout imageDirectionalLayout;

@Override

protected void onStart(Intent intent) {

super.onStart(intent);

initView();

}

private void initView(){

setUIContent(ResourceTable.Layout_main_abilityslice_layout);

imageDirectionalLayout = (DirectionalLayout)findComponentById(ResourceTable.Id_image_directional_layout);

RoundRectView rectView = new RoundRectView(this,new Size(SizeUtils.dp2px(100),SizeUtils.dp2px(100)));

rectView.putPixelMap(getPixelMap(ResourceTable.Media_test));//设置图片,ID需要先编译后才能识别到,当前图片资源放置在resources/base/media/test.png

DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,DirectionalLayout.LayoutConfig.MATCH_PARENT);

imageDirectionalLayout.addComponent(rectView,layoutConfig);

}

/**

* 通过资源ID获取位图对象

**/

private PixelMap getPixelMap(int drawableId) {

InputStream drawableInputStream = null;

try {

drawableInputStream = BaseApplication.getInstance().getResourceManager().getResource(drawableId);

ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();

sourceOptions.formatHint = "image/png";

ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);

ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();

decodingOptions.desiredSize = new Size(0, 0);

decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);

decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;

PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);

return pixelMap;

} catch (Exception e) {

e.printStackTrace();

} finally {

try{

if (drawableInputStream != null){

drawableInputStream.close

}

}catch (Exception e) {

e.printStackTrace();

}

}

return null;

}

}

public class MainAbilitySlice extends AbilitySlice { DirectionalLayout imageDirectionalLayout; @Override protected void onStart(Intent intent) { super.onStart(intent); initView(); } private void initView(){ setUIContent(ResourceTable.Layout_main_abilityslice_layout); imageDirectionalLayout = (DirectionalLayout)findComponentById(ResourceTable.Id_image_directional_layout); RoundRectView rectView = new RoundRectView(this,new Size(SizeUtils.dp2px(100),SizeUtils.dp2px(100))); rectView.putPixelMap(getPixelMap(ResourceTable.Media_test));//设置图片,ID需要先编译后才能识别到,当前图片资源放置在resources/base/media/test.png DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,DirectionalLayout.LayoutConfig.MATCH_PARENT); imageDirectionalLayout.addComponent(rectView,layoutConfig); } /** * 通过资源ID获取位图对象 **/ private PixelMap getPixelMap(int drawableId) { InputStream drawableInputStream = null; try { drawableInputStream = BaseApplication.getInstance().getResourceManager().getResource(drawableId); ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions(); sourceOptions.formatHint = "image/png"; ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions); ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions(); decodingOptions.desiredSize = new Size(0, 0); decodingOptions.desiredRegion = new Rect(0, 0, 0, 0); decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888; PixelMap pixelMap = imageSource.createPixelmap(decodingOptions); return pixelMap; } catch (Exception e) { e.printStackTrace(); } finally { try{ if (drawableInputStream != null){ drawableInputStream.close } }catch (Exception e) { e.printStackTrace(); } } return null; } }
经验分享 程序员 微信小程序 职场和发展