Create a Button Animation in Android Studio - java

I wanted to create a button that changes it's colour when clicked on it . Also the button should pop out a little and then pop back in or bounce .
I already checked out the post related to this but I'm sorry to say that it didn't work out for me.
Also it will be really helpful if you mention the entire part of the code and not the necessary parts. Thanks a lot in Advance :)
I can't post the image of the code I typed cos of less reputation . So sorry about that.

for just button color change only you can always use Selector drawable as a back ground for example like this!
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimary"></solid>
<corners android:radius="20dp"></corners>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#000"></solid>
<corners android:radius="10dp"></corners>
</shape>
</item>
//--------------------------------------------------------------------------//
for animation like to raise up you can give a feel of size animation or for free form moving your should use Translate anim
here is the way!
1: put a new res folder called anim and right click on it which will let you make the anim resource and there you make one like "translate_anim.xml" and put this in it
In order to change position of object use tag. It uses fromXDelta, fromYDelta for X-direction and toXDelta, toYDelta attributes for Y-direction. move.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="75%p"
android:duration="800" />
</set>
And then in java you have to go and set up the animation using this
Animation translater = AnimUtils.loadAnimation(getApplicationContext,R.anim.translate_anim);
Button btnMoving = findViewbyId(R.id.btn_moving);
btnMoving.startAnimation(btnMoving);
here is the resource anim file for scale UP.
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="#android:anim/linear_interpolator"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>

Related

How can I merge pictures Android?

I have 2 pictures, one is a background and the other is a complete picture.
I want to create one image from both of them as if one is above the other and save them on your cell phone as one image
Which JAVA library should I study and research?
I would be happy if you would refer me to the same library I develop in Android Studio.
Yes you can, this is what you have to do:
Folder drawable -> new -> drawable resource file -> choose the name of the file and click ok
and this is what you get
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
</selector>
Inside the tags selector put the tag <layer-list> (remember to close the tag) and inside the layer-list put the tag <item>
your result can be this for example:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
</item>
<item>
</item>
</layer-list>
</item>
</selector>
Now you can customize your 2 items, if you want to put an image inside your item you can write your code like this:
<item
android:gravity="center_vertical|center_horizontal"
android:drawable="#drawable/YOUR_IMAGE"
android:width="96dp"
android:height="96dp"
tools:ignore="UnusedAttribute" />
if you want to put a shape of an object like a rectangle you can write your code like this:
<item>
<shape
android:shape="rectangle">
<solid
android:color="#000000" />
<size
android:width="1080px"
android:height="1920px" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
</item>
Now you have your image composed with 2 images (or other things) and if you want to use it you can just call android:background="#drawable/YOUR_FILE_NAME" inside your layout_activity.xml for example
with this informations I think you can solve your problem, cya :)

Using resources to set the size of an item in Layer-list

I have defined a custom background to RadioButton in my project so that I can change it in the java code. The background is a Layer-list that includes 2 items: XML drawable and image.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorAccent"/>
<size android:width="20dp" android:height="20dp"/>
<corners android:radius="3dp"/>
</shape>
</item>
<item
android:id="#+id/correct"
android:drawable="#drawable/correct_image"
android:height="15dp"
android:width="15dp"
android:gravity="center"/>
</layer-list>
The design will break because the height and width are not supported prior to API 23. Is there any way that I can set the size of the image programatically using dimension value?
I don't have any prior coding experience or knowledge, so any explanation provided with possible solution will be very helpful.
Thank you

Why Drawable background disappears in large Views?

I have a problem and I really do not know what's going on.
It is the following, I have a drawable.xml as background of a textview that is element of a listview, I set it and everything OK but I have the action that when they touch it expands (changing the number of visible lines), at that moment it is that the background is lost. Its happens only in very large views
Before it was done with background images.9 and it worked well
Here are the elements mentioned and a picture :D
sample
<img src="https://i.stack.imgur.com/0wIaX.jpg" alt="Sample" height="50%" width="50%">
Regards
fondo.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingRight="25dp"
android:paddingLeft="25dp">
<solid android:color="#color/mailUnReadBack" />
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
</shape>
Activity.java
...
private void expand(View view) {
((TextView) view).setMaxLines(reduceLines?minVal: Integer.MAX_VALUE);
}
...
myView.java
...
setBackground(getResources().getDrawable(R.drawable.fondo));
...
Try the following
drawable/fondo.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:bottom="5dp"
android:top="5dp"
android:right="25dp"
android:left="25dp">
<solid android:color="#color/mailUnReadBack" />
<corners
android:radius="15dp" />
</shape>
Java
Drawable background = ContextCompat.getDrawable(context, R.drawable.fondo);
ViewCompat.setBackground(yourView, background);

How to draw multi colored line using xml in android

Is it possible to draw a line like shown in the fig. using android xml ? I need it for setting a background for view pager indicator
If you need hard edges like that, I'd suggest using a 9-patch: http://developer.android.com/tools/help/draw9patch.html
If you're ok with soft edges, you can use a gradient (which I got from using this online tool):
<gradient
android:angle="0"
android:centerX="50%"
android:centerColor="#000000"
android:startColor="#FF0000"
android:endColor="#FF0000"
android:type="linear"
/>
create a new drawable like this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-100dp"
android:top="-100dp">
<shape android:shape="rectangle">
<stroke
android:width="100dp"
android:color="#FF0000" />
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>
and then add this as background.

Point of origin animation android

I am currently doing an animation from the material spec design, now I want to enable to do a point of origin animation which is showed here:
Sample Animation Link
How can I do that animation of pre lollipop? Is there a way doing it in pre-lollipop devices?
I've achieved this point of Origin creating a XML based approach and applying it to your custom Theme.
Create anim/anim_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:fillAfter="false"
android:startOffset="200"
android:duration="200"
android:pivotX = "100%"
android:pivotY = "100%"
/>
<translate
android:fromYDelta="50%"
android:toYDelta="0"
android:startOffset="200"
android:duration="200"
/>
</set>
Then Create anim/anim_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:fillAfter="false"
android:duration="200"
android:pivotX = "100%"
android:pivotY = "100%"
/>
<translate
android:fromYDelta="0"
android:toYDelta="50%"
android:duration="200"
/>
</set>
This animation origin pops a window or dialog fragment from right bottom corner of the screen. Modify pivotX and pivotY to change the location of the origin
Define this windows animation in your style.xml
<style name="InOut.Window" parent="#android:style/Animation.Activity">
<item name="android:windowEnterAnimation">#anim/anim_in</item>
<item name="android:windowExitAnimation">#anim/anim_out</item>
</style>
Lastly apply this animation to all the window for your custom Theme that you would have created.
<style ....>
...
<item name="android:windowAnimationStyle">#style/InOut.Window</item>
</style>

Categories

Resources