FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Enable hardware acceleration for views animations in Android by hamorphis · Pull Request #2222 · NativeScript/NativeScript · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
46 changes: 32 additions & 14 deletions tns-core-modules/ui/animation/animation.android.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import types = require("utils/types");
import enums = require("ui/enums");
import styleModule = require("ui/styling/style");
import lazy from "utils/lazy";
import { CacheLayerType } from "utils/utils";

global.moduleMerge(common, exports);

Expand Down Expand Up @@ -41,29 +42,22 @@ export class Animation extends common.Animation implements definition.Animation
public play(): definition.AnimationPromise {
let animationFinishedPromise = super.play();

let i: number;
let length: number;

this._animators = new Array<android.animation.Animator>();
this._propertyUpdateCallbacks = new Array<Function>();
this._propertyResetCallbacks = new Array<Function>();

i = 0;
length = this._propertyAnimations.length;
for (; i < length; i++) {
for (let i = 0, length = this._propertyAnimations.length; i < length; i++) {
this._createAnimators(this._propertyAnimations[i]);
}

this._nativeAnimatorsArray = (<any>Array).create(android.animation.Animator, this._animators.length);
i = 0;
length = this._animators.length;
for (; i < length; i++) {
for (let i = 0, length = this._animators.length; i < length; i++) {
this._nativeAnimatorsArray[i] = this._animators[i];
}

this._animatorSet = new android.animation.AnimatorSet();
this._animatorSet.addListener(this._animatorListener);
if (length > 0) {
if (this._animators.length > 0) {
if (this._playSequentially) {
this._animatorSet.playSequentially(this._nativeAnimatorsArray);
}
Expand All @@ -72,14 +66,16 @@ export class Animation extends common.Animation implements definition.Animation
}
}

this._enableHardwareAcceleration();

if (trace.enabled) {
trace.write("Starting " + this._nativeAnimatorsArray.length + " animations " + (this._playSequentially ? "sequentially." : "together."), trace.categories.Animation);
}
this._animatorSet.setupStartValues();
this._animatorSet.start();
return animationFinishedPromise;
}

public cancel(): void {
super.cancel();
if (trace.enabled) {
Expand Down Expand Up @@ -133,6 +129,7 @@ export class Animation extends common.Animation implements definition.Animation
for (; i < length; i++) {
this._propertyUpdateCallbacks[i]();
}
this._disableHardwareAcceleration();
this._resolveAnimationFinishedPromise();
}

Expand All @@ -142,11 +139,11 @@ export class Animation extends common.Animation implements definition.Animation
for (; i < length; i++) {
this._propertyResetCallbacks[i]();
}
this._disableHardwareAcceleration();
this._rejectAnimationFinishedPromise();
}

private _createAnimators(propertyAnimation: common.PropertyAnimation): void {

if (!propertyAnimation.target._nativeView) {
return;
}
Expand All @@ -168,7 +165,7 @@ export class Animation extends common.Animation implements definition.Animation
}

let nativeArray;
let nativeView: android.view.View = (<android.view.View>propertyAnimation.target._nativeView);
let nativeView = <android.view.View>propertyAnimation.target._nativeView;
let animators = new Array<android.animation.Animator>();
let propertyUpdateCallbacks = new Array<Function>();
let propertyResetCallbacks = new Array<Function>();
Expand Down Expand Up @@ -375,6 +372,27 @@ export class Animation extends common.Animation implements definition.Animation
private static _getAndroidRepeatCount(iterations: number): number {
return (iterations === Number.POSITIVE_INFINITY) ? android.view.animation.Animation.INFINITE : iterations - 1;
}

private _enableHardwareAcceleration() {
for (let i = 0, length = this._propertyAnimations.length; i < length; i++) {
let cache = <CacheLayerType>this._propertyAnimations[i].target._nativeView;
let layerType = cache.getLayerType();
if (layerType !== android.view.View.LAYER_TYPE_HARDWARE) {
cache.layerType = layerType;
cache.setLayerType(android.view.View.LAYER_TYPE_HARDWARE, null);
}
}
}

private _disableHardwareAcceleration() {
for (let i = 0, length = this._propertyAnimations.length; i < length; i++) {
let cache = <CacheLayerType>this._propertyAnimations[i].target._nativeView;
if (cache.layerType !== undefined) {
cache.setLayerType(cache.layerType, null);
cache.layerType = undefined;
}
}
}
}

let easeIn = lazy(() => new android.view.animation.AccelerateInterpolator(1));
Expand Down Expand Up @@ -422,4 +440,4 @@ export function _resolveAnimationCurve(curve: any): any {
}
return curve;
}
}
}
18 changes: 7 additions & 11 deletions tns-core-modules/ui/styling/background.android.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import view = require("ui/core/view");
import types = require("utils/types");
import * as styleModule from "./style";
import * as buttonModule from "ui/button";
import { CacheLayerType } from "utils/utils";

//@private
declare module "ui/styling/background" {
Expand Down Expand Up @@ -238,10 +239,6 @@ export module ad {

var _defaultBackgrounds = new Map<string, android.graphics.drawable.Drawable>();

interface CacheLayerType {
layerType: number;
}

export function onBackgroundOrBorderPropertyChanged(v: view.View) {
var nativeView = <android.view.View>v._nativeView;
var cache = <CacheLayerType>v._nativeView;
Expand All @@ -266,8 +263,8 @@ export module ad {
let backgroundColor = bkg.backgroundColor = v.style._getValue(style.backgroundColorProperty).android;
bkg.setColorFilter(backgroundColor, android.graphics.PorterDuff.Mode.SRC_IN);
bkg.backgroundColor = backgroundColor;
} else if (v.borderWidth !== 0 || v.borderRadius !== 0 || !backgroundValue.isEmpty() || clipPathValue) {

}
else if (v.borderWidth || v.borderRadius || clipPathValue || !backgroundValue.isEmpty()) {
if (!(bkg instanceof BorderDrawableClass)) {
bkg = new BorderDrawableClass();
let viewClass = types.getClass(v);
Expand All @@ -284,11 +281,11 @@ export module ad {
bkg.background = backgroundValue;
bkg.clipPath = clipPathValue;

if ((v.borderWidth !== 0 || v.borderRadius !== 0 || clipPathValue) && getSDK() < 18) {
if ((v.borderWidth || v.borderRadius || clipPathValue) && getSDK() < 18) {
// Switch to software because of unsupported canvas methods if hardware acceleration is on:
// http://developer.android.com/guide/topics/graphics/hardware-accel.html
cache.layerType = nativeView.getLayerType();
nativeView.setLayerType(android.view.View.LAYER_TYPE_SOFTWARE, null);
cache.layerType = cache.getLayerType();
cache.setLayerType(android.view.View.LAYER_TYPE_SOFTWARE, null);
}
}
else {
Expand All @@ -305,8 +302,7 @@ export module ad {
}

if (cache.layerType !== undefined) {
// Reset layer type
nativeView.setLayerType(cache.layerType, null);
cache.setLayerType(cache.layerType, null);
cache.layerType = undefined;
}
}
Expand Down
9 changes: 9 additions & 0 deletions tns-core-modules/utils/utils.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
interface Owned {
owner: any;
}

/**
* Used to cache and restore Android views' layer type, i.e. android.view.View.getLayerType and android.view.View.setLayerType.
*/
interface CacheLayerType {
layerType: number;
setLayerType(layerType: number, paint: any): void;
getLayerType(): number;
}
//@endprivate

/**
Expand Down

Back | FazBrowse Home | New Git URL