Why Folivora
It is common for Android developers to refer to a drawable file in a layout file to set the background of the View or the SRC of the ImageView. You need to write an XML file in the Drawable folder to apply it. But there are many drawable files that might only be used once, or maybe we’re just trying to implement a simple rounded background. Folivora provides you with the ability to create a drawable directly from a layout file. Folivora provides you with the ability to create a drawable directly from a layout file.
A Folivora is a slow, monkeylike sloth that hangs upside down on branches for hours without moving.
What can Folivora do
Folivora allows you to set a background or ImageView SRC for your View
- shape (GradientDrawable)
- selector (StateListDrawable)
- ripple (RippleDrawable)
- layerlist (LayerListDrawable)
- levellist (LevelListDrawable)
- inset (InsetDrawable)
- clip (ClipDrawable)
- scale (ScaleDrawable)
- animation (AnimationDrawable)
Method of use
Just add a custom attribute to layout. XML that tells Folivora how to create a drawable
Let’s try creating a simple rounded corner effect:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="shape1"
app:drawableType="shape"
app:shapeSolidColor="@android:color/holo_blue_light"
app:shapeSolidCorner="5dp"/>
Copy the code
The effect is this
Let’s create a selector that only has the normal state and the pressed state:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="selector"
app:drawableType="selector"
app:selectorStateNormal="@android:color/holo_blue_light"
app:selectorStatePressed="@android:color/holo_blue_dark"/>
Copy the code
It looks something like this
Let’s try the ripple effect:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="ripple"
app:drawableType="ripple"
app:rippleColor="@android:color/white"
app:rippleContent="@color/colorAccent"/>
Copy the code
It is certainly cool to use Ripple, but ripple effect was introduced after 5.0. What about devices prior to 5.0? Folivora provides you with the RippleFallback interface to create a Drawable instance that replaces RippleDrawable.
Folivora.setRippleFallback(new RippleFallback()){
@Override
public Drawable onFallback(ColorStateList ripple, Drawable content, Drawable mask, Context ctx){
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(ripple.getDefaultColor()));
sld.addState(new int[0], content);
returnsld; }}Copy the code
More available drawable types and corresponding properties can be clickedhereTo view
- Injection Folivora:
After we set these properties in the layout file, running the app will have no effect. The View does not know the custom properties, but Folivora does. Therefore, for the properties to take effect, we need to enable Folivora in the context, using the following two methods:
public class MainActivity extends AppCompatActivity {
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(Folivora.wrap(newBase)); }}Copy the code
or
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Folivra.installViewFactory(this); setContentView(R.layout.your_layout_xml_name); }}Copy the code
Gradle rely on
dependencies {
implementation 'hundreds' cn. Cricin: folivora.
}
Copy the code
Support tools
The live preview edit Layout file is available in Android Studio, but the IDE does not recognize custom properties, and the preview window does not render custom View backgrounds or use property hints
To solve this problem, Folivora provides support tools that can be used as follows:
- Download jar package Click Download.
- Copy the downloaded files to plugins/ Android /lib/ in the Android Studio installation directory
- Restart the IDE. If you have Folivora in your project dependencies, open the Layout file to preview in real time
Note: The support tools depend on the order in which the Java classloader loads the classes (replacing LayoutLibraryLoader and AndroidDomExtender), so please do not rename the downloaded JAR packages, just copy them
preview