Advertisement
eranseg

Animation Assignment

Dec 2nd, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.49 KB | None | 0 0
  1. //-------------- strings.xml -----------------------
  2.  
  3. <resources>
  4.     <string name="app_name">Animation Project</string>
  5.     <string name="my_walker">My Walker</string>
  6.     <string name="bye_walker">Bye Bye!</string>
  7.     <string name="go">Go!</string>
  8.     <string name="animation">animation</string>
  9. </resources>
  10.  
  11. //----------------- simple_animation.xml ---------------------
  12.  
  13. <?xml version="1.0" encoding="utf-8"?>
  14. <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
  15.     <item android:drawable="@drawable/a1" android:duration="100" />
  16.     <item android:drawable="@drawable/a2" android:duration="100" />
  17.     <item android:drawable="@drawable/a3" android:duration="100" />
  18.     <item android:drawable="@drawable/a4" android:duration="100" />
  19.     <item android:drawable="@drawable/a5" android:duration="100" />
  20.     <item android:drawable="@drawable/a6" android:duration="100" />
  21.     <item android:drawable="@drawable/a7" android:duration="100" />
  22.     <item android:drawable="@drawable/a8" android:duration="100" />
  23.     <item android:drawable="@drawable/a9" android:duration="100" />
  24.     <item android:drawable="@drawable/a10" android:duration="100" />
  25.     <item android:drawable="@drawable/a11" android:duration="100" />
  26.     <item android:drawable="@drawable/a12" android:duration="100" />
  27.     <item android:drawable="@drawable/a13" android:duration="100" />
  28.     <item android:drawable="@drawable/a14" android:duration="100" />
  29.     <item android:drawable="@drawable/a15" android:duration="100" />
  30.     <item android:drawable="@drawable/a16" android:duration="100" />
  31.     <item android:drawable="@drawable/a17" android:duration="100" />
  32.     <item android:drawable="@drawable/a18" android:duration="100" />
  33.     <item android:drawable="@drawable/a19" android:duration="100" />
  34.     <item android:drawable="@drawable/a20" android:duration="100" />
  35.     <item android:drawable="@drawable/a21" android:duration="100" />
  36.     <item android:drawable="@drawable/a22" android:duration="100" />
  37.     <item android:drawable="@drawable/a23" android:duration="100" />
  38.     <item android:drawable="@drawable/a24" android:duration="100" />
  39. </animation-list>
  40.  
  41. //---------------------- animate.xml ----------------------------------
  42.  
  43. <?xml version="1.0" encoding="utf-8"?>
  44. <set xmlns:android="http://schemas.android.com/apk/res/android">
  45.     <translate
  46.         android:duration="5000"
  47.         android:fromXDelta="80%p"
  48.         android:toXDelta="-5%p" />
  49.  
  50.     <scale
  51.         android:duration="1000"
  52.         android:fromXScale="1"
  53.         android:fromYScale="1"
  54.         android:pivotX="50%"
  55.         android:pivotY="50%"
  56.         android:toXScale="-1"
  57.         android:toYScale="1"
  58.         android:startOffset="4000" />
  59.  
  60.     <translate
  61.         android:duration="5000"
  62.         android:fromXDelta="-5%p"
  63.         android:toXDelta="120%p"
  64.         android:startOffset="5000" />
  65. </set>
  66.  
  67. //------------------------- activity_main.xml ----------------------------
  68.  
  69. <?xml version="1.0" encoding="utf-8"?>
  70. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  71.     xmlns:app="http://schemas.android.com/apk/res-auto"
  72.     xmlns:tools="http://schemas.android.com/tools"
  73.     android:layout_width="match_parent"
  74.     android:layout_height="match_parent"
  75.     tools:context=".MainActivity">
  76.  
  77.     <LinearLayout
  78.         android:layout_width="match_parent"
  79.         android:layout_height="match_parent"
  80.         android:orientation="vertical">
  81.  
  82.         <TextView
  83.             android:id="@+id/title"
  84.             android:layout_width="match_parent"
  85.             android:layout_height="wrap_content"
  86.             android:textSize="32sp"
  87.             android:gravity="center"
  88.             android:layout_marginTop="50dp"
  89.             android:text="@string/my_walker" />
  90.  
  91.         <TextView
  92.             android:id="@+id/bye"
  93.             android:layout_width="match_parent"
  94.             android:layout_height="wrap_content"
  95.             android:textSize="44sp"
  96.             android:gravity="center"
  97.             android:layout_marginTop="50dp"
  98.             android:textColor="@color/colorPrimary"
  99.             android:text="@string/bye_walker" />
  100.  
  101.         <ImageView
  102.             android:id="@+id/imageView"
  103.             android:layout_width="150dp"
  104.             android:layout_height="150dp"
  105.             android:layout_marginTop="100dp"
  106.             android:layout_gravity="start"
  107.             android:contentDescription="@string/animation"
  108.  
  109.             app:srcCompat="@drawable/simple_animation" />
  110.  
  111.         <Button
  112.             android:id="@+id/btnRun"
  113.             android:layout_width="match_parent"
  114.             android:layout_height="wrap_content"
  115.             android:textSize="28sp"
  116.             android:layout_margin="40dp"
  117.             style="@style/Widget.AppCompat.Button.Colored"
  118.             android:layout_marginBottom="50dp"
  119.             android:text="@string/go" />
  120.     </LinearLayout>
  121. </androidx.constraintlayout.widget.ConstraintLayout>
  122.  
  123. // ------------------------ MainActivity.java ------------------------
  124.  
  125. package com.sgl.animationproject;
  126.  
  127. import androidx.annotation.InterpolatorRes;
  128. import androidx.appcompat.app.AppCompatActivity;
  129.  
  130. import android.content.Context;
  131. import android.graphics.Interpolator;
  132. import android.graphics.drawable.AnimationDrawable;
  133. import android.os.Bundle;
  134. import android.view.View;
  135. import android.view.animation.AlphaAnimation;
  136. import android.view.animation.Animation;
  137. import android.view.animation.AnimationUtils;
  138. import android.widget.ImageView;
  139. import android.widget.TextView;
  140. import android.widget.Toast;
  141.  
  142. public class MainActivity extends AppCompatActivity implements Animation.AnimationListener {
  143.  
  144.     ImageView imageView;
  145.     Context context;
  146.     AnimationDrawable animationDrawable;
  147.     AnimationDrawable textDrawable;
  148.     TextView textView;
  149.     Animation animation;
  150.  
  151.     @Override
  152.     protected void onCreate(Bundle savedInstanceState) {
  153.         super.onCreate(savedInstanceState);
  154.         setContentView(R.layout.activity_main);
  155.         setPointer();
  156.     }
  157.  
  158.     private void setPointer() {
  159.         this.context = this;
  160.         imageView = findViewById(R.id.imageView);
  161.         textView = findViewById(R.id.bye);
  162.         animationDrawable = (AnimationDrawable)imageView.getDrawable();
  163.         textView.setVisibility(View.INVISIBLE);
  164.         imageView.setVisibility(View.INVISIBLE);
  165.  
  166.         findViewById(R.id.btnRun).setOnClickListener(new View.OnClickListener() {
  167.             @Override
  168.             public void onClick(View v) {
  169.                 imageView.setVisibility(View.INVISIBLE);
  170.                 animationDrawable.start();
  171.  
  172.                 imageView.startAnimation(animation);
  173.             }
  174.         });
  175.         animation = AnimationUtils.loadAnimation(context, R.anim.animate);
  176.         animation.setAnimationListener(this);
  177.     }
  178.  
  179.     @Override
  180.     public void onAnimationStart(Animation animation) {
  181.         Toast.makeText(context, "Animation Started", Toast.LENGTH_LONG).show();
  182.     }
  183.  
  184.     @Override
  185.     public void onAnimationEnd(Animation animation) {
  186.         textView.setVisibility(View.VISIBLE);
  187.         AlphaAnimation fadeIn = new AlphaAnimation(0.0f, 1.0f);
  188. //        AlphaAnimation fadeOut = new AlphaAnimation(1.0f, 0.0f);
  189.         textView.startAnimation(fadeIn);
  190.         fadeIn.setDuration(1200);
  191.         fadeIn.setFillAfter(true);
  192.         Toast.makeText(context, "Animation Ended", Toast.LENGTH_LONG).show();
  193.     }
  194.  
  195.     @Override
  196.     public void onAnimationRepeat(Animation animation) {
  197.  
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement