Animation Drawable with Sound in android? - android

I use the animation drawable functionality in my class.
I need to generate the sound while each frame of animation is loaded.Whether it is possible or not. I use the following code for animation:
<?xml version="1.0" encoding="utf-8"?>
<animation-list android:id="#+id/handimation" android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/count3" android:duration="650" />
<item android:drawable="#drawable/count2" android:duration="650" />
<item android:drawable="#drawable/count1" android:duration="650" />
<item android:drawable="#drawable/go" android:duration="650" />
</animation-list>
My Java:
AnimationDrawable frameAnimation;
ImageView animatedimage ;
animatedimage = (ImageView)findViewById(R.id.rcount);
final MyAnimationRoutineStart animationStart =
new MyAnimationRoutineStart();
final MyAnimationRoutineStop animationStop =
new MyAnimationRoutineStop();
animatedimage.setBackgroundResource(R.drawable.spin);
frameAnimation = (AnimationDrawable) animatedimage.getBackground();
Timer animateTimerStart = new Timer(false);
animateTimerStart.schedule(animationStart, 100);
Timer animateTimerStop = new Timer(false);
animateTimerStop.schedule(animationStop, 2500);
class MyAnimationRoutineStart extends TimerTask
{
MyAnimationRoutineStart()
{
}
public void run()
{
// Start the animation (looped playback by default).
frameAnimation.start();
}
}
class MyAnimationRoutineStop extends TimerTask
{
MyAnimationRoutineStop()
{
}
public void run()
{
frameAnimation.stop();
// stop the animation (looped playback by default).
videoName=camcorderView.startRecording();
counter = 0;
counterFlag = true;
counterThread.start();
}
}
I don't know where to generate the sound.
Any one suggest some idea.
Thanks.

I achieved that using a handler. Inside the handler, I start and play the sound and delay the handler message for the duration of each animation frame. It is working fine.

Related

Transition Drawable with more than 2 layers

Can there be more than 2 items in transition drawable? I need to change background so second frames fades in than on top of it third does and so on to fourth...
for now I have this:
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/voting_button_not1"/>
<item android:drawable="#drawable/voting_button_not2"/>
<item android:drawable="#drawable/voting_button_not3"/>
<item android:drawable="#drawable/voting_button_not4"/>
<item android:drawable="#drawable/voting_button_not5"/>
<item android:drawable="#drawable/voting_button_not1"/>
</transition>
And I got the button:
<ImageButton android:id="#+id/skipButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/coldf1f2"
android:scaleType="fitCenter"
android:adjustViewBounds="true"/>
P.S. never mind that its an ImageButton, that doesn't make any difference.
And in my code I got smth like this:
TransitionDrawable vote_not = (TransitionDrawable)skip.getBackground();
vote_not.startTransition(1000);
It plays transition from first item to second. But I need the full list be played.
It seems like TransitionDrawable is meant to operate only with two layers. Taken from the Android documentation for the class:
An extension of LayerDrawables that is intended to cross-fade between
the first and second layer.
I think you can specify more than two layers, because this is extension of layered drawable, but in the case of the TransitionDrawable actually only the first two are used.
you can try this option using a handler
mAnimateImage is a imageView
and DrawableImage is an array with drawables
int DrawableImage[] = {R.drawable.back_red , R.drawable.back_green, R.drawable.back_purple};
final Handler handler = new Handler();
final int[] i = {0};
final int[] j = {1};
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
Resources res = getApplicationContext().getResources();
TransitionDrawable out = new TransitionDrawable(new Drawable[]{res.getDrawable(DrawableImage[i[0]]), res.getDrawable(DrawableImage[j[0]])});
out.setCrossFadeEnabled(true);
mAnimateImage.setImageDrawable(out);
out.startTransition(4000);
i[0]++;
j[0]++;
if (j[0] == DrawableImage.length) {
j[0] = 0;
}
if (i[0] == DrawableImage.length) {
i[0] = 0;
}
handler.postDelayed(this, 8000);
}
});
}
}, 0);
You can do this with a combination of using a Handler and re-applying the TransitionDrawable for the elements of the array.
See my answer at https://stackoverflow.com/a/54584103/114549
Sounds like you might want AnimationDrawable:
http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

How to create a Button, that blinks after clicked?

So far I learned how animations work and how to set the background of a button according to its state as described here.
Well, I defined an animation:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="50"
android:repeatMode="reverse"
android:repeatCount="6"/>
I start the animation in the onClick(View v) method. The problem now is, that the actual click action gets processed before the animation finishes. I know I could use an AnimationListener, but this would not look very nice to me since I'd have to call the actual click processes within the AnimationListener then.
Does anyone know a more skilful way to let a button blink after it gets clicked?
You can use the Selector tag like Follows: Create a New xml file and place it in your drawable folder and name it as shadow_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/ask_footer"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/askfooter_hover" />
<item android:drawable="#drawable/ask_footer" />
</selector>
And then go to that xml in which your Button is declared: And write one attribute in Button
android:background="#drawable/shadow_color"
and you are done.
Mark the answer if you find it usefull..
Define the onclick for the button
button1.setOnClickListener(
new Button.OnClickListener() {
public void onClick (View v){ calcular(1,v); }
}
);
Make your button blink
This makes the images defined in the XML alternate between each other.
public void calcular(final int p,final View v){
MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
mp.start();
//v.setBackgroundResource(R.drawable.dia1btn_stl2);
final TransitionDrawable transition1 =
(TransitionDrawable) v.getBackground();
Handler blinkHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
transition1.startTransition(70);
break;
case 1:
transition1.resetTransition();
break;
}
super.handleMessage(msg);
}
};
for (int i=0; i<6; i++)
{
Message msg = new Message();
if(i % 2 == 0){
msg.what = 0;
}
else{
msg.what=1;
}
blinkHandler.sendMessageDelayed(msg, i*100);
}
/*mCurrentSeries.clear();
if(calcularctrl == 0){
calcularctrl = 1;
dtdodo = new DownloadImageTask(this , p , codacaovalue);
dtdodo.execute("wwwkjhdijdh");
}*/
Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
public void run() {
//v.setBackgroundResource(R.drawable.dia1btn_stl2);
mCurrentSeries.clear();
if(calcularctrl == 0){
calcularctrl = 1;
dtdodo = new DownloadImageTask(outer() , p , codacaovalue);
dtdodo.execute("wwwkjhdijdh");
}
try {
this.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public acoesdetalhes outer(){
return acoesdetalhes.this;
}
}, 1000);
}
The XML of the button background
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
<item android:drawable="#drawable/mes1btn_stl2" />
<item android:drawable="#drawable/mes1btn_prssd2" />
</transition>
This code provided in part by user Alin.

AnimationDrawable duration attribute

I have a problem with android:duration attribute in my xml file.
That is my code:
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="#drawable/eye_1"
android:duration="150" />
<item
.
.
.
</animation-list>
There are 8 images.
Here is my Activity code:
public class MyActivity extends Activity {
AnimationDrawable frameAnimation;
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sauron);
mp = MediaPlayer.create(this, R.raw.sound);
mp.setLooping(true);
mp.start();
ImageView imgView = (ImageView) findViewById(R.id.animationImage);
// imgView.setVisibility(ImageView.VISIBLE);
imgView.setBackgroundResource(R.drawable.animation);
frameAnimation = (AnimationDrawable) imgView
.getBackground();
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(10000);
}
#Override public void onWindowFocusChanged(boolean hasFocus) {
frameAnimation.start();
super.onWindowFocusChanged(hasFocus); }
#Override
protected void onStop()
{
// Stop play
super.onStop();
mp.stop();
}
}
Everything works fine but there is a problem with duration between images. No matter what number i put in android:duration attribute, animation runs very fast. Does anyone know where is a problem?
Thank you
the duration is in "milliseconds" meaning 150 is is VERY VERY fast
when 1500 is 1.5 seconds and 150 means 0.15 seconds which is just over 1 tenth of a second
try putting a number in thousands, like 1500 or 3000 and see if that works

How to replay a one shot animation

I want to make a one shot animation but be able to play it as many times as I want. Right now it plays only the first time.
.xml:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/p1_ch1" android:oneshot="true">
<item
android:drawable="#drawable/p1_ch1_5"
android:duration="500"/>
<item
android:drawable="#drawable/p1_ch1_4"
android:duration="1000"/>
<item
android:drawable="#drawable/p1_ch1_5"
android:duration="500"/>
</animation-list>
.java:
public void handler_p1_ch1_5 (View target){
ImageView iv = (ImageView)findViewById(R.id.p1_ch1_5);
AnimationDrawable aw = (AnimationDrawable)iv.getBackground();
aw.start();
}
Just call aw.stop() before aw.start()
Use this:
frameAnimation.setOneShot(true);
Change
android:oneShot="true" to
android:oneShot="false"
If you want to replay a one shot animation after previous animation finish you can do like
final AnimationDrawable animationDrawable = (AnimationDrawable) image.getDrawable();
if (!animationDrawable.isRunning()) {
int totalFrameDuration = 0;
for (int i = 0; i < animationDrawable.getNumberOfFrames(); i++) {
totalFrameDuration += animationDrawable.getDuration(i);
}
animationDrawable.start();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
animationDrawable.stop();
}
}, duration);
}

Spinning wheel in Android

How can I spin a image wheel in an activity on android with the help of touch event? I need some guideline or link of any tutorial.
This is typically done with a couple pieces. This is how I do it in one of my apps. *Note: This is not a smooth wheel, so much as it starts and stops at the top (which was intentional). You can lookup more about Animation on the dev site.
Main XML that has an image:
<ImageView
android:id="#+id/anim_example"
android:src="#drawable/loading_circle"
android:layout_width="30sp"
android:layout_height="30sp"
android:onClick="RunAnimation" />
Here are the parts in code that run the animation
public void RunAnimation(View v)
{
//The onClick method has to be present and must take the above parameter.
StartLoading();
//This will delay the stop for 5 seconds
//Normally you would want to actually have this run based on some other input/data.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
StopLoading();
}
}, 5000);
}
public void StartLoading() {
ImageView refreshImage = (ImageView) this.findViewById(R.id.anim_example);
refreshImage.setImageDrawable(getResources().getDrawable(R.drawable.loading_circle));
Animation rotateLoading = AnimationUtils.loadAnimation(this, R.anim.rotate);
refreshImage.clearAnimation();
refreshImage.setAnimation(rotateLoading);
}
public void StopLoading() {
ImageView refreshImage = (ImageView) this.findViewById(R.id.anim_example);
if (refreshImage.getAnimation() != null)
{
refreshImage.clearAnimation();
refreshImage.setImageDrawable(getResources().getDrawable(R.drawable.loading_circle));
}
}
anim.rotate:
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="359"
android:duration="2000"
android:repeatMode="restart"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%">
</rotate>

Categories

Resources