Can't android MotionLayout apply Spring animation? - android

I want to implement Spring animation using MotionLayout.
Also, I want to give stiffness and damping as custom values.
By the way, I saw examples where you need to use the OnSwipe tag to use Spring animation in MotionLayout.
What I want is for animation to work without user's touch.
Is there any way?

It is a little unclear what you mean by spring animation.
If you want the progress to move in the form of a Spring.
So typically you do:
progress.animateTo(
1f,
animationSpec = tween(800)
)
MotionLayout(
...
progress = progress.value
)
This can be changed to
val progress by animateFloatAsState(
targetValue = 1f,
animationSpec = spring(
dampingRatio = Spring.DampingRatioHighBouncy,
stiffness = Spring.StiffnessMedium
)
)
MotionLayout(
...
progress = progress.value
)
You can achieve spring like effects inside of MotionLayout by creating custom interpolation curves.

Related

Issue with jetpack compose animation performance

I am using animateDpAsState animation, but there is an issue with performance. The animation is very laggy, lot of frames are dropped. When I build application with release mode, animation is running more smoothly, but it is not as good as it needs to be.
Strange thing is that when I play animation several times (for example 15 times), animation is then very smooth and everything is ok.
What could be a problem?
My code:
val contextMenuAnimation = animateDpAsState(
targetValue =
if (contextMenuOpened.value) -contextMenuWidth
else 0.dp,
animationSpec = tween()
)
.
.
.
Box(
modifier = Modifier
.offset(x = maxWidth + contextMenuAnimation.value)
.size(contextMenuWidth, height = maxHeight)
) {
SidePanel()
}
One improvement you can have over your current setup is using Modifier.offset{IntOffset} over Modifier.offset(). Jetpack Compose has three phases Composition, Layout and Draw if you use Modifiers with lambda you can skip or defer reads as you can see in official document. Using Modifiers with lambdas are advised with animations.
https://stackoverflow.com/a/73274631/5457853
You can also check out scoped or smart recomposition to limit recomposition amount to Composable scopes.
Jetpack Compose Smart Recomposition
And you still in need to improve performance that you think might happen due to recompositions you can check out these articles about stability/immutability
https://chris.banes.dev/posts/composable-metrics/
https://medium.com/androiddevelopers/jetpack-compose-stability-explained-79c10db270c8

How do I create a radial rainbow gradient?

I'm trying to add a rainbow gradient to an existing button. I'm using this brush value
val brush = Brush.radialGradient(
listOf(
Color(0xFFFF685D),
Color(0xFFFF64F0),
Color(0xFF5155FF),
Color(0xFF54EDFF),
Color(0xFF5BFF7B),
Color(0xFFFDFF59),
Color(0xFFFFCA55),
),
tileMode = TileMode.Repeated
)
This is not achieving the intended effect. It won't even represent a rainbow.
I'm trying to add as a button background gradient.
iterate the first element of the float array from 0 to 360 and keep both other elements at 1
https://developer.android.com/reference/android/graphics/Color#HSVToColor(float[])
background info: https://en.wikipedia.org/wiki/HSL_and_HSV

How to give a RotationAnimation a steady speed?

In my Android app, I have a fan image whose rotation speed is set by regulator.
This is the animation part:
private fun setFanSpeed(toggle:Boolean, speed:Float){
var speed:Float = speed
if (!toggle)
speed = 0F
val animation = RotateAnimation(0F, 720F,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
)
animation.duration = speedToDuration(speed)*1000
animation.repeatCount = Animation.INFINITE
fan.animation = animation
fan.startAnimation(animation)
}
Unfortunately, the rotating fan feels weird because, instead of rotating at a constant speed i.e 720 rotation in a constant time it accelerates during the start of the animation, and decelerates to a stop during the end. Then speeds up again for the next rotation.
How can I remove the speed up and the speed down, and just make the animation work at a constant speed?
It seems that AccelerateDecelerateInterpolator is the default interpolator for Animation, against the caption in documentation. Specifying LinearInterpolator explicitly, you'll be able to make it rotate uniformly.
animation.interpolator = LinearInterpolator()

Invisible/transparent material for ShapeFactory renderable in Sceneform and ARCore

I'm trying to create a completely transparent material for a cube renderable created with ShapeFactory. I use this cube renderable as a large rectangular surface to make an infinite floor, and need it to be completely transparent.
I tried using MaterialFactory's makeTransparentWithColor() with an alpha of 0.0 in order to achieve that. However, the cube does not become invisible, even though it is a little bit transparent. Below is the code I use:
MaterialFactory.makeTransparentWithColor(context, Color(0f, 0f, 255f, 0f)).thenAccept { material ->
val size = Vector3(100f,0.001f,100f)
val center = Vector3(0f,0f,0f)
val floorRenderable = ShapeFactory.makeCube(size,center,material)
floorRenderable.isShadowCaster = false
floorRenderable.isShadowReceiver = false
floorAnchorNode.renderable = floorRenderable
}
Any idea how to make an invisible material for the ShapeFactory cube? I saw this Github issue which might indicate I could somehow create a dummy-renderable containing a custom material with an unlit shading model, and then get that renderables material to apply in the makeCube()? Surely there must be a better way, similar to ARKit/SceneKit's SCNNode opacity. Please, if you know anything about this I appreciate any help I can get.
It can't be fully transparent simply because of lighting and material used here.
If you need to make something invisible, don't set any renderable. And if you simply want to intercept touch, use collision instead :
floorAnchorNode.collisionShape = Box(size, center)

How to draw border around a bar in MPAndroidChart BarCart?

I am using MPAndroidChart for drawing a bar chart.
How can I draw border around each bar in the graph that is displayed?
This is not possible by default.
You will have to modify the library, specifically the BarChartRenderer class.
Simply take the RectF object that represents the bar that is to be drawn and draw it once again over the original bar whilst changing the Paint mode to STROKE instead of FILL.
Of course you will also have to change the Paint color to distinguish the border from the bar.
So, after a rigorous searching and checking the Demo App the Library Developer used to demonstrate the Library functionalities, I got this.
BarChart barChart = new BarChart();
BarData barData = new BarData(barDataSet);
//set the appropriate properties of object **barData**
//then set the data property of the chart
barChart.setData(barData);
.
.
.
.
//add border to chart bars
for (IBarDataSet set : barChart.getData().getDataSets())
((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
.
.
.
//set up other properties as well
Intent
barData.sliceSpace = 2f
and for mas styling
eneficioChar.holeRadius = 1f
val set1 = BarDataSet(entries,seriesLabel)
set1.color = Color.YELLOW
set1.barBorderWidth = 1f
set1.barBorderColor=Color.RED

Categories

Resources