ad728

Thursday, October 12, 2017

AIDE Tutorial - 41 Animation Fade In And Fade Out

AIDE Tutorial - 41 Animation Fade In And Fade Out

Watch the below video and try yourself

Create an ImageView with id and image sources and add two buttons with onclick method
Main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/mainImageView"
android:src="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"/>
<Button
android:text="Fade In"
android:onClick="fadein"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="Fade Out"
android:onClick="fadeout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Then Create a new folder anim and inside anim folder create two xml file i.e. fade in and fade out. Copy the code below for both xml file.
Fade_in.xml
<set android:interpolator="@android:anim/accelerate_interpolator" xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:duration="5000" android:fromalpha="0" android:toalpha="1"></alpha>
</set>
Fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >
<alpha
android:fromAlpha="1"
android:toAlpha="0"
android:duration="5000" ></alpha>
</set>

Add onClick method in Java connect it with animation
package com.kcgroup.AFIO;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.animation.*;
public class MainActivity extends Activity
{@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.main);}
public void fadein(View v){
ImageView image=(ImageView)findViewById(R.id.mainImageView);
Animation an= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);
image.startAnimation(an);}
public void fadeout(View v){
ImageView image=(ImageView)findViewById(R.id.mainImageView);
Animation an= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_out);
image.startAnimation(an);}}
Now build the app and it should work fine.
Share:

0 comments:

Post a Comment

Total Pageviews

Sponsor

Sponsor

ad300

Blog Archive