Giter Site home page Giter Site logo

renderthread's Introduction

This library wraps the native RenderThread, providing a type-safe way to run custom animations off the UI thread.

Read about what it means in this Medium article.

It transparently falls back to using the standard drawing methods if the hidden APIs aren't available, or whenever hardware acceleration is not enabled on the specified Canvas.

By default it will automatically try to access the RenderThread on Android versions that are known to work (Lollipop and Marshmallow), but you can force it to try on any version (like the N preview) by calling this code once before using it:

RenderThread.init(true);

Capabilities

So far the RenderThread is capable of making the following drawing calls, which mirror the respective Canvas ones, but where each property can be animated:

  • drawCircle(centerX, centerY, radius, paint)
  • drawRoundRect(left, top, right, bottom, cornerRadiusX, cornerRadiusY, paint)

The paint values that can be animated are the following:

  • alpha
  • stroke width

Usage

To use it, there are generally 3 steps involved:

CanvasProperty<Float> centerXProperty;
CanvasProperty<Float> centerYProperty;
CanvasProperty<Float> radiusProperty;
CanvasProperty<Paint> paintProperty;

Animator radiusAnimator;
Animator alphaAnimator;

@Override
protected void onDraw(Canvas canvas) {

    if (!animationInitialised) {

      // 1. create as many CanvasProperty as needed with the initial animation values
      centerXProperty = RenderThread.createCanvasProperty(canvas, initialCenterX);
      centerYProperty = RenderThread.createCanvasProperty(canvas, initialCenterY);
      radiusProperty = RenderThread.createCanvasProperty(canvas, initialRadius);
      paintProperty = RenderThread.createCanvasProperty(canvas, paint);

      // 2. create one or more Animator with the properties you want to animate
      radiusAnimator = RenderThread.createFloatAnimator(this, canvas, radiusProperty, targetRadius);
      alphaAnimator = RenderThread.createPaintAlphaAnimator(this, canvas, paintProperty, targetAlpha);
      radiusAnimator.start();
      alphaAnimator.start();
    }

    // 3. draw to the Canvas
    RenderThread.drawCircle(canvas, centerXProperty, centerYProperty, radiusProperty, paintProperty);
}

Check the sample for a complete implementation.

Download

Gradle

repositories {
    maven {
        url 'https://dl.bintray.com/takhion/maven/'
    }
}

dependencies {
    compile 'me.eugeniomarletti:renderthread:1.0.0'
}

renderthread's People

Contributors

rock3r avatar takhion avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.