Open nandantal opened 5 years ago
ObjectAnimator
can be used to animate any property that provides a getter and a setter. One way to animate alpha for instance would be to create a subclass of Node
with a getAlpha()
and a setAlpha()
. You can also use a Property
instead: https://developer.android.com/reference/android/util/Property.
Thank you for the reply.
I have created one Node
subclass but still not sure how to change the alpha
inside setAlpha()
method.
I have one more question, can we add any other animation like explosion effects while using Sceneform. I have read that Sceneform doesn't support FBX animation but is there any way we can implement it?
Provided you are using the material that comes from MaterialFactory.makeTransparentWithColor you can set the color with a call like: renderable.getMaterial().setFloat4(MaterialFactory.MATERIAL_COLOR, new Color(animatedRed, animatedGreen, animatedBlue, animatedAlpha));
If using an Animator as Romain suggested above this might look like:
MaterialFactory.makeTransparentWithColor(this, new Color(android.graphics.Color.RED))
.thenAccept(
material -> {
renderable =
ShapeFactory.makeSphere(0.1f, new Vector3(0.0f, 0.15f, 0.0f), material);
})
.exceptionally(
throwable -> {
DemoUtils.displayError(this, "Unable to build material.", throwable);
return null;
});
ValueAnimator alphaAnimator = ValueAnimator.ofFloat(0.0f, 1.0f, 0.0f);
alphaAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float animatedAlpha = (float) animation.getAnimatedValue();
if(sphereRenderable != null) {
sphereRenderable.getMaterial().setFloat4(MaterialFactory.MATERIAL_COLOR,
new Color(0.0f, 0.0f, 1.0f*animatedAlpha, animatedAlpha));
}
}
});
alphaAnimator.setRepeatCount(ValueAnimator.INFINITE);
alphaAnimator.setDuration(1000);
alphaAnimator.start();
Is there a solution?
We can rotate a node using object animator as explained in the example :