Question: Lab Assignment #13 Exercise #1 Create the AndroidDoveAnimator2 Android App that has four buttons go on two rows, and a Dove above and below the
| Lab Assignment #13 Exercise #1 Create the AndroidDoveAnimator2 Android App that has four buttons go on two rows, and a Dove above and below the buttons. The Dove above the buttons is controlled by a Timer initially and the Dove below is controlled by a animation-list object: Get rid of the Timer and use a Handler to animate the dove on the top, and have that Dove actually move as it flaps its wings in whatever direction it is facing in. When it goes off of the screen have it come back on the screen from the other side, so that it wraps around. When you reverse the image direction, then it goes in the reverse direction. Your strings file is at: strings.xml, and use the following Zip for your Dove images: pngdoves.zip Below are the arrays for your Dove images for your Handler:
private int [] rightImages = { R.drawable.rdove1, R.drawable.rdove2, R.drawable.rdove3, R.drawable.rdove4, R.drawable.rdove5, R.drawable.rdove6, R.drawable.rdove7, R.drawable.rdove8 }; private int [] leftImages = { R.drawable.ldove1, R.drawable.ldove2, R.drawable.ldove3, R.drawable.ldove4, R.drawable.ldove5, R.drawable.ldove6, R.drawable.ldove7, R.drawable.ldove8 }; Below is the animation-list XML code that will go in a file called "anim_dove.xml", inside the "drawables" folder inside of the "res" folder:
The following is the XML code for your new "anim_dove.xml" layout resource file:
And lastly, your DoveActivity code can be found at the following link:import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.ImageView; import android.view.View; import java.util.Timer; import java.util.TimerTask; import android.graphics.drawable.AnimationDrawable; public class DoveActivity extends AppCompatActivity { private Button slowerBtn, fasterBtn, reverseBtn, pauseBtn; private ImageView doveView; private ImageView myAnimation; private AnimationDrawable myAnimationDrawable; private AnimationDrawable rightDrawable, leftDrawable; private Timer myTimer; private TimerTask myTimerTask; private int delay = 40; private int doveNum = 0; private boolean isRight = true, isPaused = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dove); doveView = (ImageView) findViewById(R.id.doveView); myAnimation = (ImageView) findViewById(R.id.myanimation); myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable(); initAnimDrawables(myAnimationDrawable.getDuration(0)); updateAnimDirection(myAnimationDrawable.getDuration(0)); if (isPaused) { setPauseView(); } myAnimation.post( new Runnable() { @Override public void run() { if (!isPaused) myAnimationDrawable.start(); else myAnimationDrawable.stop(); } }); myTimer = new Timer(); myTimerTask = new TimerTask() { @Override public void run() { TimerMethod(); } }; myTimer.schedule(myTimerTask, 0, delay); slowerBtn = (Button) findViewById(R.id.slowerButton); slowerBtn.setTransformationMethod(null); slowerBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { delay += 20; updateTimer(); int dur = myAnimationDrawable.getDuration(0); dur += 20; updateAnimSpeed(dur); } }); fasterBtn = (Button) findViewById(R.id.fasterButton); fasterBtn.setTransformationMethod(null); fasterBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { delay -= 20; if (delay = 8) doveNum = 0; if (isRight) doveView.setImageResource(rightImages[doveNum]); else doveView.setImageResource(leftImages[doveNum]); } }; private void updateTimer() { myTimerTask.cancel(); myTimerTask = new TimerTask() { @Override public void run() { TimerMethod(); } }; myTimer.schedule(myTimerTask, 0, delay); } private void updateAnimSpeed(int dur) { if (!isPaused) { myAnimationDrawable.stop(); } myAnimationDrawable.unscheduleSelf(myAnimationDrawable); initAnimDrawables(dur); if (!isPaused) { unsetPauseView(); myAnimationDrawable.start(); } } private void updateAnimDirection(int dur) { if (!isPaused) { myAnimationDrawable.stop(); } myAnimationDrawable = (isRight) ? rightDrawable : leftDrawable; if (!isPaused) { myAnimationDrawable.start(); } myAnimationDrawable.unscheduleSelf(myAnimationDrawable); if (!isPaused) { myAnimationDrawable.scheduleSelf(myAnimationDrawable, SystemClock.uptimeMillis() + dur); myAnimation.setImageDrawable(myAnimationDrawable); } } private void initAnimDrawables(int dur) { rightDrawable = new AnimationDrawable(); leftDrawable = new AnimationDrawable(); for (int i = 0; i |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
