Skip to content

Commit

Permalink
fix: support minHeight, @android:color, ?attr
Browse files Browse the repository at this point in the history
  • Loading branch information
7hens committed Oct 22, 2019
1 parent ea10ca8 commit c94fc4d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;

import com.zhangyue.we.x2c.X2C;
Expand All @@ -18,6 +19,14 @@ public class ActivityX2C extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
X2C.setContentView(this, R.layout.activity_main_inter);

findViewById(R.id.iv).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

ViewStub stub = (ViewStub) findViewById(R.id.stub);
stub.setLayoutResource(R.layout.stub_layout);
stub.inflate();
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/res/layout/activity_main_inter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
android:id="@+id/iv"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/todo"
android:contentDescription="@android:string/no"
android:minWidth="@android:dimen/app_icon_size"
android:minHeight="12dp"
android:scaleType="centerCrop"
android:src="@drawable/head"
android:src="?attr/selectableItemBackground"
android:background="@android:mipmap/sym_def_app_icon"
app:layout_constraintDimensionRatio="H,1024:539"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
48 changes: 32 additions & 16 deletions x2c-apt/src/main/java/com/zhangyue/we/view/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public View(String packageName, String name, Attributes attributes) {
this.mImports.add("android.util.TypedValue");
this.mImports.add("android.graphics.Color");
this.mImports.add("android.view.ViewGroup");
this.mImports.add("android.graphics.drawable.ColorDrawable");
this.mImports.add("com.zhangyue.we.x2c.X2CUtils");
this.mImports.add(String.format("%s.R", mPackageName));

}
Expand Down Expand Up @@ -449,12 +451,12 @@ private boolean setMaxWidth(StringBuilder stringBuilder, String value) {
}

private boolean setMinWidth(StringBuilder stringBuilder, String value) {
stringBuilder.append(String.format("%s.setMinWidth(%s);\n", getObjName(), getDimen(value)));
stringBuilder.append(String.format("%s.setMinimumWidth(%s);\n", getObjName(), getDimen(value)));
return true;
}

private boolean setMinHeight(StringBuilder stringBuilder, String value) {
stringBuilder.append(String.format("%s.setMinHeight(%s);\n", getObjName(), getDimen(value)));
stringBuilder.append(String.format("%s.setMinimumHeight(%s);\n", getObjName(), getDimen(value)));
return true;
}

Expand Down Expand Up @@ -536,7 +538,7 @@ private boolean setTextSize(StringBuilder stringBuilder, String value) {
String dim;
if (value.startsWith("@")) {
unit = "TypedValue.COMPLEX_UNIT_PX";
dim = String.format("(int)res.getDimension(R.dimen.%s)", value.substring(value.indexOf("/") + 1));
dim = String.format("(int)res.getDimension(R.dimen.%s)", getDimen(value));
} else {
if (value.endsWith("dp") || value.endsWith("dip")) {
unit = "TypedValue.COMPLEX_UNIT_DIP";
Expand All @@ -555,27 +557,34 @@ private boolean setTextSize(StringBuilder stringBuilder, String value) {
}

private boolean setImageResource(StringBuilder stringBuilder, String value) {
if (value.startsWith("#") || value.startsWith("@color")) {
stringBuilder.append(String.format("%s.setBackgroundColor(%s);\n", getObjName(), getColor(value)));
if (value.startsWith("#") || value.startsWith("@color") || value.startsWith("@android:color")) {
stringBuilder.append(String.format("%s.setImageDrawable(new ColorDrawable(%s));\n", getObjName(), getColor(value)));
} else if (value.equals("null")) {
stringBuilder.append(String.format("%s.setImageDrawable(%s);\n", getObjName(), "null"));
} else if (value.startsWith("?")) {
String objName = getObjName();
// stringBuilder.append("try {\n");
// stringBuilder.append(String.format(" %s.setImageDrawable(new ColorDrawable(res.getColor(%s)));\n", objName, getUnknown(value)));
// stringBuilder.append("} catch (Exception e) {\n");
// stringBuilder.append(" //这里再来异常就是不支持的资源类型\n");
// stringBuilder.append(String.format(" %s.setImageResource(%s);\n", objName, getUnknown(value)));
// stringBuilder.append("}\n");

stringBuilder.append(String.format("%s.setImageResource(X2CUtils.getResourceIdFromAttr(ctx, %s));\n", objName, getUnknown(value)));
} else {
stringBuilder.append(String.format("%s.setImageResource(%s);\n", getObjName(), getDrawable(value)));
}
return true;
}

private boolean setBackground(StringBuilder stringBuilder, String value) {
if (value.startsWith("#") || value.startsWith("@color")) {
if (value.startsWith("#") || value.startsWith("@color") || value.startsWith("@android:color")) {
stringBuilder.append(String.format("%s.setBackgroundColor(%s);\n", getObjName(), getColor(value)));
} else if (value.equals("null")) {
stringBuilder.append(String.format("%s.setBackgroundDrawable(%s);\n", getObjName(), "null"));
} else if (value.startsWith("?")) {
String objName = getObjName();
stringBuilder.append("try {\n");
stringBuilder.append(String.format(" %s.setBackgroundColor(%s);\n", objName, getUnknown(value)));
stringBuilder.append("} catch (Exception e) {\n");
stringBuilder.append(" //这里再来异常就是不支持的资源类型\n");
stringBuilder.append(String.format(" %s.setBackgroundResource(%s);\n", objName, getUnknown(value)));
stringBuilder.append("}\n");
stringBuilder.append(String.format("%s.setBackgroundResource(X2CUtils.getResourceIdFromAttr(ctx, %s));\n", objName, getUnknown(value)));
} else {
stringBuilder.append(String.format("%s.setBackgroundResource(%s);\n", getObjName(), getDrawable(value)));
}
Expand Down Expand Up @@ -637,8 +646,10 @@ public static String getWH(String value) {
}

public static String getDimen(String value) {
if (value.startsWith("@")) {
if (value.startsWith("@dimen")) {
return String.format("(int)res.getDimension(R.dimen.%s)", value.substring(value.indexOf("/") + 1));
} else if (value.startsWith("@android:dimen")) {
return String.format("(int)res.getDimension(android.R.dimen.%s)", value.substring(value.indexOf("/") + 1));
}
String unit;
String dim;
Expand Down Expand Up @@ -667,7 +678,7 @@ public static String getColor(String value) {
return "Color.parseColor(\"" + value + "\")";
} else if (value.startsWith("@android:color")) {
return "res.getColor(android.R.color." + value.substring(value.indexOf("/") + 1) + ")";
} else if (value.startsWith("@")) {
} else if (value.startsWith("@color")) {
return "res.getColor(R.color." + value.substring(value.indexOf("/") + 1) + ")";
} else {
return "0";
Expand All @@ -693,17 +704,23 @@ public static String getBoolean(String value) {
}

public static String getString(String value) {
if (value.startsWith("@")) {
if (value.startsWith("@string")) {
return "R.string." + value.substring(value.indexOf("/") + 1);
} else if (value.startsWith("@android:string")) {
return "android.R.string." + value.substring(value.indexOf("/") + 1);
}
return String.format("\"%s\"", value);
}

public static String getDrawable(String value) {
if (value.startsWith("@drawable")) {
return "R.drawable." + value.substring(value.indexOf("/") + 1);
} else if (value.startsWith("@android:drawable")) {
return "android.R.drawable." + value.substring(value.indexOf("/") + 1);
} else if (value.startsWith("@mipmap")) {
return "R.mipmap." + value.substring(value.indexOf("/") + 1);
}else if (value.startsWith("@android:mipmap")) {
return "android.R.mipmap." + value.substring(value.indexOf("/") + 1);
}
return value;
}
Expand All @@ -716,7 +733,6 @@ public static String getUnknown(String value) {
}
}


private String getGravity(String value) {
String[] ss = value.split("\\|");
StringBuilder sb = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletion x2c-lib/src/main/java/com/zhangyue/we/x2c/X2C.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @author:chengwei 2018/8/9
* @description
*/
public class X2C {
public final class X2C {
private static final SparseArray<IViewCreator> sSparseArray = new SparseArray<>();

/**
Expand Down
25 changes: 25 additions & 0 deletions x2c-lib/src/main/java/com/zhangyue/we/x2c/X2CUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.zhangyue.we.x2c;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.TypedValue;

/**
* @author 7hens
*/
public final class X2CUtils {

public static int getResourceIdFromAttr(Context ctx, int attr) {
TypedValue outValue = new TypedValue();
Resources.Theme theme = ctx.getTheme();
theme.resolveAttribute(attr, outValue, true);
TypedArray typedArray = theme.obtainStyledAttributes(outValue.resourceId, new int[]{attr});
try {
return typedArray.getResourceId(0, 0);
} catch (Exception e) {
typedArray.recycle();
return 0;
}
}
}

0 comments on commit c94fc4d

Please sign in to comment.