com.tencent.demo.avatar
folder of the demo to your project.com.tencent.demo.avatar.AvatarEditActivity
class of the demo):<com.tencent.demo.avatar.view.AvatarPanelandroid:id="@+id/avatar_panel"android:layout_width="match_parent"android:layout_height="300dp"app:layout_constraintBottom_toBottomOf="parent" />
avatarPanel.setAvatarPanelCallBack(new AvatarPanelCallBack() {@Overridepublic void onReceiverBindData(List<AvatarData> avatarData) {mXMagicApi.updateAvatar(avatarData, AvatarEditActivity.this);}@Overridepublic void onItemChecked(MainTab mainTab, AvatarItem avatarItem) {if (avatarItem.avatarData == null && URLUtil.isNetworkUrl(avatarItem.downloadUrl)) { // Resources are downloaded dynamically.downloadAvatarData(avatarItem, () -> updateConfig(avatarItem));} else {updateConfig(avatarItem);List<AvatarData> bindAvatarData = AvatarResManager.getAvatarDataByBindData(avatarItem.bindData);mXmagicApi.updateAvatar(bindAvatarData, AvatarActivity.this);}}@Overridepublic void onItemValueChange(AvatarItem avatarItem) {updateConfig(avatarItem);}@Overridepublic boolean onShowPage(AvatarPageInf avatarPageInf, SubTab subTab) {if (subTab != null && subTab.items != null && subTab.items.size() > 0) {AvatarItem avatarItem = subTab.items.get(0);if (avatarItem.type == AvatarData.TYPE_SLIDER && avatarItem.avatarData == null && URLUtil.isNetworkUrl(avatarItem.downloadUrl)) { //Resources are downloaded dynamically.downloadAvatarData(avatarItem, () -> {if (avatarPageInf != null) {avatarPageInf.refresh();}});return false;}}return true;}private void updateConfig(AvatarItem avatarItem) {if (mXmagicApi != null && avatarItem != null) {List<AvatarData> avatarConfigList = new ArrayList<>();avatarConfigList.add(avatarItem.avatarData);mXmagicApi.updateAvatar(avatarConfigList, AvatarActivity.this);}}});
AvatarResManager.getInstance().getAvatarData(avatarResName, getAvatarConfig(), allData -> {avatarPanel.initView(allData);});
xmagicApi
object and load the avatar resources:protected void initXMagicAndLoadAvatar(String avatarConfig, UpdatePropertyListener updatePropertyListener) {if (mXMagicApi == null && !isFinishing() && !isDestroyed()) {WorkThread.getInstance().run(() - > {synchronized(lock) {if (isXMagicApiDestroyed) {return;}mXMagicApi = XmagicApiUtils.createXMagicApi(getApplicationContext(), null);AvatarResManager.getInstance().loadAvatarRes(mXMagicApi, avatarResName, avatarConfig == null ? getAvatarConfig() : avatarConfig, updatePropertyListener);setAvatarPlaneType();}}, this.hashCode());}}
saveAvatarConfigs
in the demo):/*** Save the property values set by the user or the default property values.*/public void onSaveBtnClick() {//Get the user-configured properties through getUsedAvatarData of AvatarResManagerList < AvatarData > avatarDataList = AvatarResManager.getUsedAvatarData(avatarPanel.getMainTabList());//Convert the configured properties to a string through the XmagicApi.exportAvatar method.String content = XmagicApi.exportAvatar(avatarDataList);//Save this string, and when downloading and using it, set this string to the loadAvatarRes method of the SDK to restore this avatar.if (mXMagicApi != null) {mXMagicApi.exportCurrentTexture(bitmap - > saveAvatarModes(bitmap, content));}}
Was this page helpful?