Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[WEEX-342][android] when animation module or transition parser proper…
Browse files Browse the repository at this point in the history
…ties, some propers may be not right, so add try catch to handle the exceptions
  • Loading branch information
jianbai.gbj committed Jun 19, 2018
1 parent 939be31 commit 906a4ff
Showing 1 changed file with 107 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.taobao.weex.common.Constants;
import com.taobao.weex.utils.FunctionParser;
import com.taobao.weex.utils.WXDataStructureUtil;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXUtils;
import com.taobao.weex.utils.WXViewUtils;

Expand Down Expand Up @@ -101,128 +102,133 @@ public static PropertyValuesHolder[] toHolders(Map<Property<View,Float>, Float>

public static Map<Property<View,Float>, Float> parseTransForm(@Nullable String rawTransform, final int width,
final int height, final int viewportW) {
if (!TextUtils.isEmpty(rawTransform)) {
FunctionParser<Property<View,Float>, Float> parser = new FunctionParser<>
(rawTransform, new FunctionParser.Mapper<Property<View,Float>, Float>() {
@Override
public Map<Property<View,Float>, Float> map(String functionName, List<String> raw) {
if (raw != null && !raw.isEmpty()) {
if (wxToAndroidMap.containsKey(functionName)) {
return convertParam(width, height,viewportW, wxToAndroidMap.get(functionName), raw);
try{

if (!TextUtils.isEmpty(rawTransform)) {
FunctionParser<Property<View,Float>, Float> parser = new FunctionParser<>
(rawTransform, new FunctionParser.Mapper<Property<View,Float>, Float>() {
@Override
public Map<Property<View,Float>, Float> map(String functionName, List<String> raw) {
if (raw != null && !raw.isEmpty()) {
if (wxToAndroidMap.containsKey(functionName)) {
return convertParam(width, height,viewportW, wxToAndroidMap.get(functionName), raw);
}
}
return new HashMap<>();
}
return new HashMap<>();
}

private Map<Property<View,Float>, Float> convertParam(int width, int height, int viewportW,
@NonNull List<Property<View,Float>> propertyList,
@NonNull List<String> rawValue) {
private Map<Property<View,Float>, Float> convertParam(int width, int height, int viewportW,
@NonNull List<Property<View,Float>> propertyList,
@NonNull List<String> rawValue) {

Map<Property<View,Float>, Float> result = WXDataStructureUtil.newHashMapWithExpectedSize(propertyList.size());
List<Float> convertedList = new ArrayList<>(propertyList.size());
if (propertyList.contains(View.ROTATION) ||
propertyList.contains(View.ROTATION_X) ||
propertyList.contains(View.ROTATION_Y)) {
convertedList.addAll(parseRotationZ(rawValue));
}else if (propertyList.contains(View.TRANSLATION_X) ||
propertyList.contains(View.TRANSLATION_Y)) {
convertedList.addAll(parseTranslation(propertyList, width, height, rawValue,viewportW));
} else if (propertyList.contains(View.SCALE_X) ||
propertyList.contains(View.SCALE_Y)) {
convertedList.addAll(parseScale(propertyList.size(), rawValue));
}
else if(propertyList.contains(CameraDistanceProperty.getInstance())){
convertedList.add(parseCameraDistance(rawValue));
}
if (propertyList.size() == convertedList.size()) {
for (int i = 0; i < propertyList.size(); i++) {
result.put(propertyList.get(i), convertedList.get(i));
Map<Property<View,Float>, Float> result = WXDataStructureUtil.newHashMapWithExpectedSize(propertyList.size());
List<Float> convertedList = new ArrayList<>(propertyList.size());
if (propertyList.contains(View.ROTATION) ||
propertyList.contains(View.ROTATION_X) ||
propertyList.contains(View.ROTATION_Y)) {
convertedList.addAll(parseRotationZ(rawValue));
}else if (propertyList.contains(View.TRANSLATION_X) ||
propertyList.contains(View.TRANSLATION_Y)) {
convertedList.addAll(parseTranslation(propertyList, width, height, rawValue,viewportW));
} else if (propertyList.contains(View.SCALE_X) ||
propertyList.contains(View.SCALE_Y)) {
convertedList.addAll(parseScale(propertyList.size(), rawValue));
}
else if(propertyList.contains(CameraDistanceProperty.getInstance())){
convertedList.add(parseCameraDistance(rawValue));
}
if (propertyList.size() == convertedList.size()) {
for (int i = 0; i < propertyList.size(); i++) {
result.put(propertyList.get(i), convertedList.get(i));
}
}
return result;
}
return result;
}

private List<Float> parseScale(int size, @NonNull List<String> rawValue) {
List<Float> convertedList = new ArrayList<>(rawValue.size() * 2);
List<Float> rawFloat = new ArrayList<>(rawValue.size());
for (String item : rawValue) {
rawFloat.add(WXUtils.fastGetFloat(item));
}
convertedList.addAll(rawFloat);
if (size != 1 && rawValue.size() == 1) {
private List<Float> parseScale(int size, @NonNull List<String> rawValue) {
List<Float> convertedList = new ArrayList<>(rawValue.size() * 2);
List<Float> rawFloat = new ArrayList<>(rawValue.size());
for (String item : rawValue) {
rawFloat.add(WXUtils.fastGetFloat(item));
}
convertedList.addAll(rawFloat);
if (size != 1 && rawValue.size() == 1) {
convertedList.addAll(rawFloat);
}
return convertedList;
}
return convertedList;
}

private @NonNull
List<Float> parseRotationZ(@NonNull List<String> rawValue) {
List<Float> convertedList = new ArrayList<>(1);
int suffix;
for (String raw : rawValue) {
if ((suffix = raw.lastIndexOf(DEG)) != -1) {
convertedList.add(WXUtils.fastGetFloat(raw.substring(0, suffix)));
} else {
convertedList.add((float) Math.toDegrees(Double.parseDouble(raw)));
private @NonNull
List<Float> parseRotationZ(@NonNull List<String> rawValue) {
List<Float> convertedList = new ArrayList<>(1);
int suffix;
for (String raw : rawValue) {
if ((suffix = raw.lastIndexOf(DEG)) != -1) {
convertedList.add(WXUtils.fastGetFloat(raw.substring(0, suffix)));
} else {
convertedList.add((float) Math.toDegrees(Double.parseDouble(raw)));
}
}
return convertedList;
}
return convertedList;
}

/**
* As "translate(50%, 25%)" or "translate(25px, 30px)" both are valid,
* parsing translate is complicated than other method.
* Add your waste time here if you try to optimize this method like {@link #parseScale(int, List)}
* Time: 0.5h
*/
private List<Float> parseTranslation(List<Property<View,Float>> propertyList,
int width, int height,
@NonNull List<String> rawValue, int viewportW) {
List<Float> convertedList = new ArrayList<>(2);
String first = rawValue.get(0);
if (propertyList.size() == 1) {
parseSingleTranslation(propertyList, width, height, convertedList, first,viewportW);
} else {
parseDoubleTranslation(width, height, rawValue, convertedList, first,viewportW);
/**
* As "translate(50%, 25%)" or "translate(25px, 30px)" both are valid,
* parsing translate is complicated than other method.
* Add your waste time here if you try to optimize this method like {@link #parseScale(int, List)}
* Time: 0.5h
*/
private List<Float> parseTranslation(List<Property<View,Float>> propertyList,
int width, int height,
@NonNull List<String> rawValue, int viewportW) {
List<Float> convertedList = new ArrayList<>(2);
String first = rawValue.get(0);
if (propertyList.size() == 1) {
parseSingleTranslation(propertyList, width, height, convertedList, first,viewportW);
} else {
parseDoubleTranslation(width, height, rawValue, convertedList, first,viewportW);
}
return convertedList;
}
return convertedList;
}

private void parseSingleTranslation(List<Property<View,Float>> propertyList, int width, int height,
List<Float> convertedList, String first, int viewportW) {
if (propertyList.contains(View.TRANSLATION_X)) {
convertedList.add(parsePercentOrPx(first, width,viewportW));
} else if (propertyList.contains(View.TRANSLATION_Y)) {
convertedList.add(parsePercentOrPx(first, height,viewportW));
private void parseSingleTranslation(List<Property<View,Float>> propertyList, int width, int height,
List<Float> convertedList, String first, int viewportW) {
if (propertyList.contains(View.TRANSLATION_X)) {
convertedList.add(parsePercentOrPx(first, width,viewportW));
} else if (propertyList.contains(View.TRANSLATION_Y)) {
convertedList.add(parsePercentOrPx(first, height,viewportW));
}
}
}

private void parseDoubleTranslation(int width, int height,
@NonNull List<String> rawValue,
List<Float> convertedList, String first, int viewportW) {
String second;
if (rawValue.size() == 1) {
second = first;
} else {
second = rawValue.get(1);
private void parseDoubleTranslation(int width, int height,
@NonNull List<String> rawValue,
List<Float> convertedList, String first, int viewportW) {
String second;
if (rawValue.size() == 1) {
second = first;
} else {
second = rawValue.get(1);
}
convertedList.add(parsePercentOrPx(first, width,viewportW));
convertedList.add(parsePercentOrPx(second, height,viewportW));
}
convertedList.add(parsePercentOrPx(first, width,viewportW));
convertedList.add(parsePercentOrPx(second, height,viewportW));
}

private Float parseCameraDistance(List<String> rawValue){
float ret= Float.MAX_VALUE;
if(rawValue.size() == 1){
float value = WXViewUtils.getRealPxByWidth(WXUtils.getFloat(rawValue.get(0)), viewportW);
float scale = WXEnvironment.getApplication().getResources().getDisplayMetrics().density;
if (!Float.isNaN(value) && value > 0) {
ret = value * scale;
private Float parseCameraDistance(List<String> rawValue){
float ret= Float.MAX_VALUE;
if(rawValue.size() == 1){
float value = WXViewUtils.getRealPxByWidth(WXUtils.getFloat(rawValue.get(0)), viewportW);
float scale = WXEnvironment.getApplication().getResources().getDisplayMetrics().density;
if (!Float.isNaN(value) && value > 0) {
ret = value * scale;
}
}
return ret;
}
return ret;
}
});
return parser.parse();
});
return parser.parse();
}
}catch (Exception e){
WXLogUtils.e("TransformParser", e);
}
return new LinkedHashMap<>();
}
Expand Down

0 comments on commit 906a4ff

Please sign in to comment.