Convert Figma logo to code with AI

wnafee logovector-compat

A support library for VectorDrawable and AnimatedVectorDrawable classes introduced in Lollipop

1,225
161
1,225
25

Top Related Projects

Material Design icons by Google (Material Symbols)

Android integration of multiple icon providers such as FontAwesome, Entypo, Typicons,...

😍 A beautiful, fluid, and extensible dialogs API for Kotlin & Android.

Android-Iconics - Use any icon font, or vector (.svg) as drawable in your application.

A circular ImageView for Android

Android Floating Action Button based on Material Design specification

Quick Overview

The vector-compat repository by wnafee is a JavaScript library that provides a compatibility layer for the Vector data structure, allowing developers to use it in a consistent way across different JavaScript environments, including modern browsers and Node.js.

Pros

  • Cross-platform Compatibility: The library ensures that the Vector data structure behaves consistently across various JavaScript environments, including modern browsers and Node.js.
  • Familiar API: The library provides a familiar API for working with Vector objects, making it easy for developers to integrate it into their existing codebase.
  • Lightweight: The library is relatively small in size, adding minimal overhead to the overall project.
  • Actively Maintained: The project is actively maintained, with regular updates and bug fixes.

Cons

  • Dependency on Polyfills: The library may require the use of polyfills for certain features, which can increase the overall project size and complexity.
  • Limited Functionality: The library primarily focuses on providing a compatibility layer for the Vector data structure, and may not offer advanced features or functionality beyond that.
  • Potential Performance Impact: Depending on the specific use case, the compatibility layer provided by the library may have a slight performance impact compared to using the native Vector implementation.
  • Lack of Comprehensive Documentation: The project's documentation could be more comprehensive, making it harder for new users to get started.

Code Examples

Here are a few examples of how to use the vector-compat library:

import { Vector2 } from 'vector-compat';

// Creating a new Vector2 instance
const v1 = new Vector2(1, 2);

// Performing vector operations
const v2 = new Vector2(3, 4);
const sum = v1.add(v2);
const difference = v1.subtract(v2);
const product = v1.multiply(2);
const magnitude = v1.magnitude();

// Normalizing a vector
const normalized = v1.normalize();
import { Vector3 } from 'vector-compat';

// Creating a new Vector3 instance
const v1 = new Vector3(1, 2, 3);

// Performing vector operations
const v2 = new Vector3(4, 5, 6);
const cross = v1.cross(v2);
const dot = v1.dot(v2);
const distance = v1.distanceTo(v2);
import { Vector4 } from 'vector-compat';

// Creating a new Vector4 instance
const v1 = new Vector4(1, 2, 3, 4);

// Performing vector operations
const v2 = new Vector4(5, 6, 7, 8);
const homogenized = v1.homogenize();
const transformed = v1.transform(new Matrix4());

Getting Started

To use the vector-compat library in your project, follow these steps:

  1. Install the library using npm or yarn:
npm install vector-compat
  1. Import the desired vector class (e.g., Vector2, Vector3, Vector4) from the vector-compat package:
import { Vector2 } from 'vector-compat';
  1. Create instances of the vector class and perform the desired operations:
const v1 = new Vector2(1, 2);
const v2 = new Vector2(3, 4);
const sum = v1.add(v2);
  1. Refer to the project's documentation for more detailed information on the available methods and functionality.

Competitor Comparisons

Material Design icons by Google (Material Symbols)

Pros of material-design-icons

  • Comprehensive collection of official Material Design icons
  • Regular updates and maintenance by Google
  • Wide range of icon formats (SVG, PNG, WebP) and sizes

Cons of material-design-icons

  • Large repository size due to numerous icon formats and sizes
  • Requires more setup and integration effort for Android projects
  • Limited to Material Design style, less flexibility for custom designs

Code comparison

vector-compat:

VectorDrawableCompat vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_vector, getTheme());
imageView.setImageDrawable(vectorDrawable);

material-design-icons:

<ImageView
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:src="@drawable/ic_action_name" />

Summary

vector-compat focuses on providing backward compatibility for vector drawables in Android, while material-design-icons offers a vast collection of pre-designed icons. vector-compat is more suitable for projects requiring custom vector graphics and compatibility with older Android versions. material-design-icons is ideal for projects adhering to Material Design guidelines and needing a wide variety of ready-to-use icons across different platforms and formats.

Android integration of multiple icon providers such as FontAwesome, Entypo, Typicons,...

Pros of android-iconify

  • Supports multiple icon fonts, including FontAwesome, Material Icons, and more
  • Allows easy customization of icons, including size, color, and animations
  • Provides a simple API for adding icons to TextViews and Buttons

Cons of android-iconify

  • Requires adding icon fonts to the project, which may increase app size
  • Limited to vector icons from predefined icon sets
  • May have performance overhead when rendering many icons simultaneously

Code Comparison

android-iconify:

IconDrawable iconDrawable = new IconDrawable(context, FontAwesomeIcons.fa_android)
    .colorRes(R.color.icon_color)
    .sizeDp(24);
imageView.setImageDrawable(iconDrawable);

vector-compat:

VectorDrawableCompat vectorDrawable = VectorDrawableCompat.create(
    context.getResources(), R.drawable.ic_android, null);
imageView.setImageDrawable(vectorDrawable);

Summary

android-iconify offers a wide range of icon fonts and easy customization, but may increase app size and have performance limitations. vector-compat provides native support for vector drawables with backward compatibility, but lacks the extensive icon library and customization options of android-iconify. The choice between the two depends on specific project requirements and the desired balance between flexibility and performance.

😍 A beautiful, fluid, and extensible dialogs API for Kotlin & Android.

Pros of material-dialogs

  • More comprehensive dialog solution with a wider range of customization options
  • Actively maintained with frequent updates and bug fixes
  • Extensive documentation and community support

Cons of material-dialogs

  • Larger library size, potentially increasing app size
  • Steeper learning curve due to more complex API

Code Comparison

vector-compat:

AnimatedVectorDrawable drawable = AnimatedVectorDrawable.getDrawable(context, R.drawable.vector_drawable);
imageView.setImageDrawable(drawable);
drawable.start();

material-dialogs:

MaterialDialog(this).show {
    title(R.string.dialog_title)
    message(R.string.dialog_message)
    positiveButton(R.string.agree)
    negativeButton(R.string.disagree)
}

Summary

While vector-compat focuses specifically on animated vector drawables, material-dialogs offers a more comprehensive solution for creating and customizing dialogs in Android applications. material-dialogs provides a wider range of features and customization options, but this comes at the cost of a larger library size and potentially more complex implementation. vector-compat, on the other hand, is more lightweight and focused on a specific use case. The choice between the two depends on the project's requirements and the developer's preference for simplicity versus feature richness.

Android-Iconics - Use any icon font, or vector (.svg) as drawable in your application.

Pros of Android-Iconics

  • Supports a wider range of icon fonts and custom icons
  • Offers more customization options for icons (size, color, padding, etc.)
  • Provides a unified API for handling various icon types

Cons of Android-Iconics

  • Larger library size due to extensive features
  • Steeper learning curve for advanced customizations
  • May require more setup for simple use cases

Code Comparison

vector-compat:

VectorDrawableCompat vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_vector, getTheme());
imageView.setImageDrawable(vectorDrawable);

Android-Iconics:

new IconicsDrawable(this)
    .icon(FontAwesome.Icon.faw_android)
    .color(Color.RED)
    .sizeDp(24)
    .paddingDp(1);
imageView.setImageDrawable(iconDrawable);

Both libraries aim to simplify the use of vector graphics in Android applications, but they take different approaches. vector-compat focuses on compatibility with older Android versions for vector drawables, while Android-Iconics provides a more comprehensive solution for using and customizing various icon types, including icon fonts and SVGs. The choice between the two depends on the specific requirements of your project and the level of customization needed for icons.

A circular ImageView for Android

Pros of CircleImageView

  • Specifically designed for creating circular image views, offering a more focused and optimized solution
  • Provides built-in border and shadow options for enhanced customization
  • Actively maintained with regular updates and bug fixes

Cons of CircleImageView

  • Limited to circular image views, lacking support for other vector graphics or shapes
  • May require additional libraries or custom code for more complex vector animations
  • Potentially higher memory usage compared to vector-based solutions

Code Comparison

CircleImageView:

<de.hdodenhof.circleimageview.CircleImageView
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:src="@drawable/profile"
    app:civ_border_width="2dp"
    app:civ_border_color="#FF000000"/>

vector-compat:

<com.wnafee.vector.MorphButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:vci_fromDrawable="@drawable/vector_drawable_1"
    app:vci_toDrawable="@drawable/vector_drawable_2"/>

CircleImageView focuses on creating circular image views with customizable borders, while vector-compat provides support for vector drawables and animations across different Android versions. The choice between the two depends on the specific requirements of your project, such as the need for circular images versus general vector graphics support.

Android Floating Action Button based on Material Design specification

Pros of FloatingActionButton

  • Specifically designed for implementing Material Design floating action buttons
  • Offers more customization options for FAB appearance and behavior
  • Includes built-in animations and shadow effects

Cons of FloatingActionButton

  • Limited to FAB functionality, less versatile than vector-compat
  • May require more setup and configuration for basic usage
  • Potentially larger library size due to specialized features

Code Comparison

FloatingActionButton:

<com.github.clans.fab.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add"
    fab:fab_size="normal"
    fab:fab_colorNormal="@color/colorPrimary"/>

vector-compat:

<com.wnafee.vector.MorphButton
    android:id="@+id/morphButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:morphStartDrawable="@drawable/ic_play"
    app:morphEndDrawable="@drawable/ic_pause"/>

While FloatingActionButton provides a ready-to-use FAB implementation with various customization options, vector-compat offers a more general-purpose solution for vector drawables and animated icons. The choice between the two depends on the specific requirements of your project and whether you need specialized FAB functionality or broader vector graphic support.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

vector-compat

A support library for VectorDrawable and AnimatedVectorDrawable introduced in Lollipop with fully backwards compatible tint support (api 14+ so far)

vector-compat provides the necessary tools to make animated icons similar to the new drawer hamburger icon that morphs to a back arrow when clicked. Any other morph animation between icons can be defined purely in xml (no java code required) and the library takes care of the transformation animation. Because they are in vector format, these drawables can be of any height and width with no resulting pixelation.

Example Example 1 Example 2

The library will transparently fall back to the lollipop implementation of VectorDrawable and AnimatedVectorDrawable on api 21+ devices

##Commonly used animations The library packs some ready-made morph animations developers can use in their code with MorphButton. More will be added soon as this is a work-in-progress. The library has the following morph animations :

  • Play-Pause morph animation (bi-directional morph)
  • Play-Stop morph animation (bi-directional morph)
  • Arrow-Hamburger menu morph animation (bi-directional morph)

The goal is to, with time, create a repo of commonly used morph animations that lots of developers find useful.

If you have requests for particular morph animations, please open a new issue and I'll work on adding them to the library. You are also welcome to create a pull request if you've created some of your own. Please contribute :)

Download

Add the vector-compat dependency to your build.gradle file and make sure to use buildToolsVersion 22 or higher:

Maven Central

android {
    // use version 22 or higher
    buildToolsVersion "22.0.1"
    ...
}
dependencies {
    compile 'com.wnafee:vector-compat:1.0.5'
    ...
}

Proguard

If you're using proguard for code shrinking and obfuscation, make sure to add the following:

   -keep class com.wnafee.vector.** { *; }

Usage

VectorDrawable and AnimatedVectorDrawable xml drawable syntax is exactly the same as the lollipop documentation (can be seen here and here respectively). With 2 caveats:

  • Some attributes under the <vector> nodes must be listed once for the android: namespace and once for the local namespace with a vc_ prefix (e.g. app:vc_fillColor). See example here. (For a complete list of vc_ prefixed attributes see attr.xml for )
  • Any pathType anim xml must have the android:valueType="pathType" in addition to app:vc_valueType="pathType" to allow for lollipop implementation fallback. See example here.

Inflation

VectorDrawable and AnimatedVectorDrawable in this support library can be inflated in one of 2 ways:

  • Calling static getDrawable() methods:
//This will only inflate a drawable with <vector> as the root element
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector);

//This will only inflate a drawable with <animated-vector> as the root element
AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector);

// This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices
ResourcesCompat.getDrawable(context, R.drawable.any_drawable);

If inflating the Drawable in java code, it is recommended to always use ResourcesCompat.getDrawable() as this handles Lollipop fallback when applicable. This allows the system to cache Drawable ConstantState and hence is more efficient

  • directly from the MorphButton view in xml:
<!-- Insert xmlns:app="http://schemas.android.com/apk/res-auto" in your root layout element -->
<com.wnafee.vector.MorphButton
    android:id="@+id/playPauseBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:vc_startDrawable="@drawable/ic_pause_to_play"
    app:vc_endDrawable="@drawable/ic_play_to_pause" /> 

MorphButton

MorphButton is a CompoundButton with 2 states: MorphState.START or MorphState.END. The attributes vc_startDrawable and vc_endDrawable define which foreground drawables to use for the button depending on the button's state. These can be any type of drawable (e.g. BitmapDrawable, ColorDrawable, VectorDrawable, AnimatedVectorDrawable etc.)

To use MorphButton in your app, make sure to include the morphButtonStyle item in your base app theme:

<style name="MyAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="morphButtonStyle">@style/Widget.MorphButton</item>
</style>

MorphButtons allow you to tint your foreground drawables (i.e. vc_startDrawable and vc_endDrawable) and background drawable separately in both xml and java. See the following examples for defining MorphButtons:

XML:

<com.wnafee.vector.MorphButton
    android:id="@+id/drawerBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    app:vc_backgroundTint="#f50057"
    app:vc_foregroundTint="#3F51B5"
    app:vc_startDrawable="@drawable/ic_arrow_to_drawer"
    app:vc_endDrawable="@drawable/ic_drawer_to_arrow"/>

Java:

    MorphButton mb = new MorphButton(this);
    mb.setBackgroundTintList(getResources().getColorStateList(R.color.background_tint_color));
    mb.setForegroundTintList(ColorStateList.valueOf(Color.RED));
    mb.setStartDrawable(R.drawable.ic_pause_to_play);
    mb.setEndDrawable(R.drawable.ic_play_to_pause);
    mb.setState(MorphState.END);

The scaleType attribute defines how to scale the foreground drawable to fill the button's background. This is the same as ImageView.ScaleType which you can take a look at here.

Button clicks will toggle between the foreground drawables. If the drawables happen to implement the Animatable interface (e.g. AnimatedVectorDrawable or AnimationDrawable) then start() will be automatically called to animate between the start and end drawables defined in xml.

MorphButton states can be set manually via setState() methods:

// transition with no animation
myMorphButton.setState(MorphState.END) 

// ... or transition with animation if drawable is Animatable
myMorphButton.setState(MorphState.START, true) 

If you need to be informed of button state changes you need to add an OnStateChangedListener:

MyMorphButton.setOnStateChangedListener(new OnStateChangedListener() {
    @Override
    public void onStateChanged(MorphState changedTo, boolean isAnimating) {
        // changeTo is the new state
        // isAnimating = true if the state changed with animation
        // Do something here
    }
});

License

Copyright 2015 Wael Nafee

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.