gpt4 book ai didi

android - 在android中保存/恢复自定义 View 的状态

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:52 27 4
gpt4 key购买 nike

我已经为 EditText 实现了自定义 View ,如下图所示:

Custom view for edittext

现在这个自定义 View 包括翻转、编辑、旋转、拖放和调整大小的功能。我在资源管理器中以位图格式保存此自定义 View 。问题是我想编辑它,我想恢复自定义 View 的细节并检索 CustomView 的每个细节,甚至是它的位置。那么,有什么办法可以实现吗?

最佳答案

您需要像这样存储所有编辑:

Rotation:为此,您必须保持从初始值 0 到下一个旋转值的角度。

Flip :为此创建一个标志,并为前后设置 True , false 值。

Text :您知道如何采用 EditText 的文本形式。

父 View 中的布局位置:

为了计算父 View 中的布局位置,使用下面的方法并为此创建四个 int 变量,

=

置顶 =

=

底部 =

高度 =

宽度 =

private Map<String,Integer>  getPositionOnScreen(View view){
Map<String,Integer> map = new HashMap<String,Integer>();
View rootLayout = view.getRootView().findViewById(android.R.id.content);

int[] viewLocation = new int[2];
view.getLocationOnScreen(viewLocation);

int[] rootLocation = new int[2];
rootLayout.getLocationOnScreen(rootLocation);

int relativeLeft = viewLocation[0] - rootLocation[0];
int relativeTop = viewLocation[1] - rootLocation[1];

int height = viewLocation[0] - rootLocation[0];
int width = viewLocation[1] - rootLocation[1];


map.put("left",relativeLeft);
map.put("top",relativeTop);
map.put("height",view.getLayoutParams().height);
map.put("width",view.getLayoutParams().width);
return map;
}

上述方法返回 View 的左侧和顶部位置,但您需要从左侧、顶部和高度、宽度计算右侧和底部位置。

创建一个 Pojo 将所有这些参数保存在一起,并将此 pojo 存储到 SharedPreferences 或数据库或应用程序上下文中。

关于android - 在android中保存/恢复自定义 View 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42137772/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com