Android - 使用 JSON 文件中颜色的最佳方式
Android - Best way to use colors from JSON file
我正在开发一个将从服务器检索 colors.json 文件的应用程序。该文件将具有以下结构:
[
{"ServerActionButton.background.normal":
{ "red": 29, "green": 188, "blue": 189, "alpha":255}
},
{"ServerActionButton.background.disabled":
{ "red": 164, "green": 228, "blue": 229, "alpha":255}
},
{"ServerActionButton.title.normal":
{ "red": 255, "green": 255, "blue": 255, "alpha":255}
},
...
]
这个 JSON 应该在应用程序启动时检索,我应该能够在整个应用程序中使用这些颜色。
问题:最好的方法是什么?我考虑过以下选项:
- 正在从 JSON 生成资源 XML 文件(这会很棒,但有可能吗?)。
- 将 JSON 映射到模型,然后将其存储在 SharedPreferences 中。
- 使用从 JSON 检索到的数据构建本地数据库。
哪种方法最好?还有其他一些我没有考虑的可能性吗?
提前致谢!
首先下载json颜色数据。
案例 1 :- 如果你有少量 json 颜色的数据,那么使用 SharedPreferences.
案例2:-如果你有大量的数据那么去SQLite数据库
我个人认为 SQLite 是最好的主意,因此您可以使用查询轻松管理。
如果你想使用 SharedPreference 那么你可以。
SQLite
大量相同的结构化数据应存储在 SQLite 数据库中,因为数据库是为此类数据设计的。由于数据是由数据库构建和管理的,因此可以使用 SQL 等查询语言查询数据以获取符合特定条件的数据子集。这使得在数据中搜索成为可能。当然,管理和搜索大量数据会影响性能,因此从数据库读取数据可能比从 SharedPreferences 读取数据慢。
SharedPreferences
SharedPreferences 是一个 key/value 存储,您可以在其中保存特定键下的数据。要从商店中读取数据,您必须知道数据的键。这使得读取数据变得非常容易。但是,存储少量数据很容易,存储和读取大型结构化数据也很困难,因为您需要为每个数据定义键,此外,除非您有一定的概念,否则您无法真正在数据中进行搜索命名键。
考虑一个更分层的结构:
[
{
"item" : "ServerActionButton",
"styles" : {
"normal" :
{
"title" : "#FFFFFFFF",
"background" : "#FF1DBCBD"
},
"disabled" : {
"title" : "#FF7F7F7F",
"background" : "#FF1DBCBD"
}
}
},
...
]
然后很容易将其解析为地图(或地图的地图)
class StyleProperties
{
public Color title;
public Color background;
}
HashMap<String, HashMap<String, StyleProperties>> stylemappings = YourFunctionToParseTheJson(strJson);
然后您可以轻松地将样式应用到元素,如下所示:
void ApplyStylesToView(View v, String item, String style)
{
StyleProperty styleProp = null;
HashMap<String, StyleProperties> stylePropertiesForItem = stylemappings.get("item");
if (stylePropertiesForItem != null)
{
styleProp = stylePropertiesForItem.get(style);
v.setBackgroundColor(styleProp.background);
// if the item is a textview, apply the title color
TextView tv = v as TextView;
if (v instanceof TextView)
{
((TextView)v).setTextColor(styleProp.title);
}
}
}
我正在开发一个将从服务器检索 colors.json 文件的应用程序。该文件将具有以下结构:
[
{"ServerActionButton.background.normal":
{ "red": 29, "green": 188, "blue": 189, "alpha":255}
},
{"ServerActionButton.background.disabled":
{ "red": 164, "green": 228, "blue": 229, "alpha":255}
},
{"ServerActionButton.title.normal":
{ "red": 255, "green": 255, "blue": 255, "alpha":255}
},
...
]
这个 JSON 应该在应用程序启动时检索,我应该能够在整个应用程序中使用这些颜色。
问题:最好的方法是什么?我考虑过以下选项:
- 正在从 JSON 生成资源 XML 文件(这会很棒,但有可能吗?)。
- 将 JSON 映射到模型,然后将其存储在 SharedPreferences 中。
- 使用从 JSON 检索到的数据构建本地数据库。
哪种方法最好?还有其他一些我没有考虑的可能性吗?
提前致谢!
首先下载json颜色数据。
案例 1 :- 如果你有少量 json 颜色的数据,那么使用 SharedPreferences.
案例2:-如果你有大量的数据那么去SQLite数据库
我个人认为 SQLite 是最好的主意,因此您可以使用查询轻松管理。
如果你想使用 SharedPreference 那么你可以。
SQLite
大量相同的结构化数据应存储在 SQLite 数据库中,因为数据库是为此类数据设计的。由于数据是由数据库构建和管理的,因此可以使用 SQL 等查询语言查询数据以获取符合特定条件的数据子集。这使得在数据中搜索成为可能。当然,管理和搜索大量数据会影响性能,因此从数据库读取数据可能比从 SharedPreferences 读取数据慢。
SharedPreferences
SharedPreferences 是一个 key/value 存储,您可以在其中保存特定键下的数据。要从商店中读取数据,您必须知道数据的键。这使得读取数据变得非常容易。但是,存储少量数据很容易,存储和读取大型结构化数据也很困难,因为您需要为每个数据定义键,此外,除非您有一定的概念,否则您无法真正在数据中进行搜索命名键。
考虑一个更分层的结构:
[
{
"item" : "ServerActionButton",
"styles" : {
"normal" :
{
"title" : "#FFFFFFFF",
"background" : "#FF1DBCBD"
},
"disabled" : {
"title" : "#FF7F7F7F",
"background" : "#FF1DBCBD"
}
}
},
...
]
然后很容易将其解析为地图(或地图的地图)
class StyleProperties
{
public Color title;
public Color background;
}
HashMap<String, HashMap<String, StyleProperties>> stylemappings = YourFunctionToParseTheJson(strJson);
然后您可以轻松地将样式应用到元素,如下所示:
void ApplyStylesToView(View v, String item, String style)
{
StyleProperty styleProp = null;
HashMap<String, StyleProperties> stylePropertiesForItem = stylemappings.get("item");
if (stylePropertiesForItem != null)
{
styleProp = stylePropertiesForItem.get(style);
v.setBackgroundColor(styleProp.background);
// if the item is a textview, apply the title color
TextView tv = v as TextView;
if (v instanceof TextView)
{
((TextView)v).setTextColor(styleProp.title);
}
}
}