Skip to content

Instantly share code, notes, and snippets.

@davidliu
Created April 4, 2019 21:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidliu/f19f4a3ee4ee8827f3aa0727c7b4ce97 to your computer and use it in GitHub Desktop.
Save davidliu/f19f4a3ee4ee8827f3aa0727c7b4ce97 to your computer and use it in GitHub Desktop.
ViewCompiler input <-> output
package is.shortcut;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.util.AttributeSet;
import android.util.Xml;
import android.view.*;
import android.widget.*;
public final class CompiledView {
static <T extends View> T createView(Context context, AttributeSet attrs, View parent, String name, LayoutInflater.Factory factory, LayoutInflater.Factory2 factory2) {
if (factory2 != null) {
return (T)factory2.onCreateView(parent, name, context, attrs);
} else if (factory != null) {
return (T)factory.onCreateView(name, context, attrs);
}
return null;
}
public static View inflate(Context context) {
try {
LayoutInflater inflater = LayoutInflater.from(context);
LayoutInflater.Factory factory = inflater.getFactory();
LayoutInflater.Factory2 factory2 = inflater.getFactory2();
Resources res = context.getResources();
XmlResourceParser xml = res.getLayout(is.shortcut.R.layout.flurry_hero);
AttributeSet attrs = Xml.asAttributeSet(xml);
xml.next(); // start document
xml.next(); // <FrameLayout>
FrameLayout view0 = createView(context, attrs, null, "FrameLayout", factory, factory2);
if (view0 == null) view0 = new FrameLayout(context, attrs);
xml.next(); // <LinearLayout>
LinearLayout view2 = createView(context, attrs, view0, "LinearLayout", factory, factory2);
if (view2 == null) view2 = new LinearLayout(context, attrs);
ViewGroup.LayoutParams layout3 = view0.generateLayoutParams(attrs);
xml.next(); // <TextView>
TextView view4 = createView(context, attrs, view2, "TextView", factory, factory2);
if (view4 == null) view4 = new TextView(context, attrs);
ViewGroup.LayoutParams layout5 = view2.generateLayoutParams(attrs);
xml.next(); // </TextView>
view2.addView(view4, layout5);
xml.next(); // <TextView>
TextView view6 = createView(context, attrs, view2, "TextView", factory, factory2);
if (view6 == null) view6 = new TextView(context, attrs);
ViewGroup.LayoutParams layout7 = view2.generateLayoutParams(attrs);
xml.next(); // </TextView>
view2.addView(view6, layout7);
xml.next(); // <com.custom.RoundedFrameLayout>
com.custom.RoundedFrameLayout view8 = createView(context, attrs, view2, "com.custom.RoundedFrameLayout", factory, factory2);
if (view8 == null) view8 = new com.custom.RoundedFrameLayout(context, attrs);
ViewGroup.LayoutParams layout9 = view2.generateLayoutParams(attrs);
xml.next(); // <com.facebook.drawee.view.SimpleDraweeView>
com.facebook.drawee.view.SimpleDraweeView view10 = createView(context, attrs, view8, "com.facebook.drawee.view.SimpleDraweeView", factory, factory2);
if (view10 == null) view10 = new com.facebook.drawee.view.SimpleDraweeView(context, attrs);
ViewGroup.LayoutParams layout11 = view8.generateLayoutParams(attrs);
xml.next(); // </com.facebook.drawee.view.SimpleDraweeView>
view8.addView(view10, layout11);
xml.next(); // </com.custom.RoundedFrameLayout>
view2.addView(view8, layout9);
xml.next(); // <com.custom.RoundedFrameLayout>
com.custom.RoundedFrameLayout view12 = createView(context, attrs, view2, "com.custom.RoundedFrameLayout", factory, factory2);
if (view12 == null) view12 = new com.custom.RoundedFrameLayout(context, attrs);
ViewGroup.LayoutParams layout13 = view2.generateLayoutParams(attrs);
xml.next(); // <RelativeLayout>
RelativeLayout view14 = createView(context, attrs, view12, "RelativeLayout", factory, factory2);
if (view14 == null) view14 = new RelativeLayout(context, attrs);
ViewGroup.LayoutParams layout15 = view12.generateLayoutParams(attrs);
xml.next(); // </RelativeLayout>
view12.addView(view14, layout15);
xml.next(); // </com.custom.RoundedFrameLayout>
view2.addView(view12, layout13);
xml.next(); // <RelativeLayout>
RelativeLayout view16 = createView(context, attrs, view2, "RelativeLayout", factory, factory2);
if (view16 == null) view16 = new RelativeLayout(context, attrs);
ViewGroup.LayoutParams layout17 = view2.generateLayoutParams(attrs);
xml.next(); // <TextView>
TextView view18 = createView(context, attrs, view16, "TextView", factory, factory2);
if (view18 == null) view18 = new TextView(context, attrs);
ViewGroup.LayoutParams layout19 = view16.generateLayoutParams(attrs);
xml.next(); // </TextView>
view16.addView(view18, layout19);
xml.next(); // <Button>
Button view20 = createView(context, attrs, view16, "Button", factory, factory2);
if (view20 == null) view20 = new Button(context, attrs);
ViewGroup.LayoutParams layout21 = view16.generateLayoutParams(attrs);
xml.next(); // </Button>
view16.addView(view20, layout21);
xml.next(); // </RelativeLayout>
view2.addView(view16, layout17);
xml.next(); // </LinearLayout>
view0.addView(view2, layout3);
return view0;
} catch (Exception e) {
return null;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/tilesBackground"
android:foreground="?android:attr/selectableItemBackground"
>
<LinearLayout
android:id="@+id/content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="@dimen/ss_margin_side">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
style="@style/SSHeroTileTitleTextAppearance"
android:maxLines="2"
android:ellipsize="end"
tools:text="The Latest: Lorem ipsum declared 'inappropriate'"
/>
<TextView
android:id="@+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
style="@style/SSTileSubtitleTextAppearance"
android:maxLines="2"
android:ellipsize="end"
tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor..."
/>
<com.custom.RoundedFrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:vxl_radius="4dp"
>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/ad_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:actualImageScaleType="centerCrop"
app:viewAspectRatio="1.77777"
/>
</com.custom.RoundedFrameLayout>
<com.custom.RoundedFrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:vxl_radius="4dp"
>
<RelativeLayout
android:id="@+id/ad_video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</com.custom.RoundedFrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<TextView
android:id="@+id/sponsored"
style="@style/SSTileHeaderTextAppearance"
android:textColor="#9b9b9b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="@string/sponsored"
/>
<Button
android:id="@+id/ad_cta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/cta_button"
style="@style/SSTextStyleMedium"
android:minHeight="0dp"
android:minWidth="@dimen/ss_cta_button_minimum_width"
android:paddingLeft="@dimen/ss_cta_button_padding_horizontal"
android:paddingRight="@dimen/ss_cta_button_padding_horizontal"
android:paddingTop="@dimen/ss_cta_button_padding_vertical"
android:paddingBottom="@dimen/ss_cta_button_padding_vertical"
android:textAllCaps="false"
android:textColor="#222428"
android:textSize="13sp"
tools:text="Learn More"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:elevation="0px"/>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment