I want to animate my AnimatedVectorDrawable at runtime without using .xml files. Actually I'm using .xml files same way as documentation's samples shows:
AnimatedVectorDrawable
So, I have vector_drawable.xml contains<vector> with nested <group> and <path> which defines a shape.
For this vector I have animated_vector_drawable.xml contains <animated-vector> with android:animation assinged to <target>.
Last step is define an animation file rotation.xml using <objectAnimator> which is used by animated_vector_drawable.xml
Everything works fine, but the problem appears, when I need to create many different shapes (vectors) with many different or similar animations, because this generate many .xml files.
I can't include ready and prepared <vector> from one .xml file to another (some kind of <include> tag) so i need to copy the same code to another files. It is very annoying.
If I want to use the same animation for few <target> elements but each animation must have f.e. different delay or any property value (alpha, rotation, interpolator...) , I must create new .xml file contains <objectAnimator> with changed one property value instead of use the same, one file with changed property value. It's also annoying.
I discovered that I can use ObjectAnimator and set alpha & fillColor for AnimatedVectorDrawable but there is a problem when I want to change it's translateX, translateY, rotation or any other properties. Is there a way to do this without .xml. I just want to have access to <group>
Constructor that you used creates animation with absolute values (pixels).
TranslateAnimation in = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);
Try use Animation.RELATIVE_TO_PARENT to fit your needs.
Related
I have to do this in xml of an item of a recycler view (I'm using databinding and the viewholder pattern). Based on the value of the variable that is bound to the view, I need to rotate a drawable and set it as the src of an ImageView.
I've checked many options online but haven't found any, rotating the original xml drawable 45 degrees cuts of some parts of the shape which is a curved rectangle. This results in a shape that does not match the requirements.
I need suggestions on how to get this done from inside the xml or adapter without rewriting it to use getView.
Hello #staa99 try following code may it will help you.
ImageView imageView = findViewById(R.id.imageView);
RotateAnimation anim = new RotateAnimation(0, 45,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
anim.setInterpolator(new LinearInterpolator());
anim.setDuration(1000);
anim.setFillEnabled(true);
anim.setFillAfter(true);
imageView.startAnimation(anim);
The solution I eventually used was to create four different vector variables based of the original with rotation info included. I then used a variable bound to the view to store the data that determines the vector drawable to display.
This solution is not good when you need a large or unknown number of possible rotations, or if the angles are calculated at runtime. However, I've not seen a way to do that from xml so you still need to do it from java/kotlin code
I can not to set alpha parameter for my GlSurfaceView. XML-attribute android:alpha= not working, call of method from code mView.setAlpha() too not working. I read about a similar problem, for example, - how to make a transparent background on this View, but unfortunately solution did not help to solve my problem - to set alpha channel the contains of GlSurfaceView. I tried to move GlSurfaceView inside FrameLayout and yet to set alpha of FrameLayout, but this solution did not help.
Now i am still see only a one variant - to move a sample View above GlSurfaceView and to set a transparent of a sample View. This solution to be last case for me!
I want to ask - Is there any way to solve this problem differently?
The problem is solved!
Only one variant is correct - to set parametr 'alpha' in 'gl_FragColor' of your shader! For example: gl_FragColor.a=0.5;
That to have acces to your shader from code - need to make parameter alpha in shader as global ('uniform' type), and to link him across methods 'glGetUniformLocation', 'glUniform1f'. For example:
// View-code
int mAlpha;
float alpha = 0.5f;
mAlpha = GLES20.glGetUniformLocation(mProgrammerHandle, "u_alpha");
GLES20.glUniform1f(mAlpha, alpha);
// Shader-code
...
uniform float u_alpha;
...
gl_FragColor = vec4(rgb,u_alpha);
But that is not all!
Need to set parameters GlSurfaceView and Render. After initialization GlSurfaceView should be allowed a transparent Drawing:
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
getHolder().setFormat(PixelFormat.RGBA_8888);
In render object too must to set a transparent color cleaning and correct color mixing.
GLES20.glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
GLES20.glEnable(GL_BLEND);
Color mixing can be different. Default - colors mix i.e. GLES20.glBlendEquation(GL_FUNC_ADD); - not necessary write.
But there are other options of mixing:
GL_FUNC_SUBTRACT
GL_FUNC_REVERSE_SUBTRACT
I was not write 'glBlendEquation' in my code.
Next important moment - alpha mixing. Method 'glBlendFunc' or 'glBlendFuncSeparate'. More information you can read in OpenGL tutorial. For correct alpha mixing i took advantage of more detailed setup:
GLES20.glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
The last thing to do - overriding methods 'setAlpa' and 'getAlpa' in your GlSurfaceView.
P.S. If in your View a transparent artifact appear, then you may tried to set parameters setZOrderOnTop(true); and/or android:windowIsTranslucent="true" inside XML. But for me it was not required - everything works fine!
We can use the Translate animation to produce the sliding animation like:
TranslateAnimation moveAnim = new TranslateAnimation(0, 0, 0,100 );
moveAnim.setDuration(1000);
q.startAnimation(enterAnim);
Not able to reproduce similar effect using the Transition manager beginDelayedTransition.
com.transitionseverywhere.TransitionManager.beginDelayedTransition(((ViewGroup)q.getParent()));
q.setTranslationX(q.setTranslationX+100);
With this we would be able to translate all the children
Two years too late, but in case anyone else needs this:
TranslateAnimation and other animation subclasses aren't intended to be used with TransitionManager. You want to just supply the type of animations you want the manager to use and then just do the translation:
Transition transition = new ChangeBounds().setDuration(1000);
TransitionManager.beginDelayedTransition(binding.fragmentMyVideosRoot, transition);
q.setTranslationX(100);
That's it! You can use a TransitionSet to combine different types of transitions.
I am starting to play around with Property Animations over view animations as I have a view that needs to scale and push others out of the way as it does. I've seen some examples but I'm just wondering if there is anywhere that provides a list of the properties that can be altered using these classes. For example, I saw one tutorial that did a quick rotation using:
ObjectAnimator.ofFloat(aniView, "rotation", 360)
Which is quite cool, but I wouldn't have known the rotation property if not for that exact tutorial, is there any comprehensive list of what can be done? The particular property I want to animate is the weight of a view within a LinearLayout, if anyone has any advice on that specifically.
Better late than never, so here is the comprehensive list of the properties that can be animated with ObjectAnimator.
http://developer.android.com/guide/topics/graphics/prop-animation.html#views
The Docs imply that any value can be used with ObjectAnimator as long as you follow a naming convention:
The object property that you are animating must have a setter function (in camel case) in the form of set<propertyName>(). Because
the ObjectAnimator automatically updates the property during
animation, it must be able to access the property with this setter
method. For example, if the property name is foo, you need to have a
setFoo() method. If this setter method does not exist, you have three
options:
Add the setter method to the class if you have the rights to
do so.
Use a wrapper class that you have rights to change and have
that wrapper receive the value with a valid setter method and forward
it to the original object.
Use ValueAnimator instead.
[...]
With respect to your question, View has the method setRotation(float) -- that gives you a hint it can be used. In particular here's how you would do it with a particular TimeInterpolator:
ObjectAnimator anim = ObjectAnimator.ofFloat(myView, "rotation", 0f, 90f);
anim.setDuration(2000); // Duration in milliseconds
anim.setInterpolator(timeInterpolator); // E.g. Linear, Accelerate, Decelerate
anim.start() // Begin the animation
You can read the docs for more details on the expectations of ObjectAnimator.
Here is the comprehensive list of property names that you are looking for:
rotation
rotationX
rotationY
translationX
translationY
scaleX
scaleY
pivotX
pivotY
alpha
x
y
Source: Docs
Use "translateX" or "transalteY" to move a <group>
Take a look at vectorDrawable
You can check it at the View.java file in Properties block. Now it starts with the line of:
public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha")
I'd like to create an indeterminate animation that simply fades from one color to another (a pulse, if you will). I don't think this should require the use of images but despite my best efforts, I'm not sure I understand how to use something like AlphaAnimation with a Shape to accomplish this.
Could someone please provide some insight as to how to accomplish this? I have a feeling I'm missing something pretty straightforward here. (Examples are always appreciated!)
Thanks!
This is a trivial task in 3.0 - you can set up an ObjectAnimator to change the "color" or "backgroundColor" of an object (View, ColorDrawable, whatever has the property) between two values. See the ApiDemo animations/BouncingBalls for an example of this.
But assuming you're using pre-3.0 APIs, there are a couple of approaches. First, you could set up your own handler to give you the timing events you need, then calculate the new color at each point.
It's probably slightly easier (if not entirely intuitive) to use an AlphaAnimation. All you really want from the animation is percentage values, not to fade anything. So you don't set the animation on a view, but just set it up to run internally from a value of 0 to 1, then get the current animated value in your onDraw() method and set the current color appropriately.
For example, this will set up and start the alpha animation to run for one second:
Transformation transform = new Transformation();
AlphaAnimation anim = new AlphaAnimation(0f, 1f);
anim.setDuration(1000);
anim.start();
Then in your drawing loop, you grab the current animated value:
long time = getDrawingTime();
anim.getTransformation(time, transform);
float elapsedFraction = transform.getAlpha();
Once you have the elapsedFraction (a value between 0 and 1), you can calculate the appropriate in-between color value.
The code above may not match your situation exactly, but you should be able to do something similar to get what you want.