Android – faça o ImageView "piscair"

Quero fazer com que meu ImageView paireça piscair. Mais exatamente, em um evento como um clique de button, eu quero um ImageView paira alterair seu src por 1 segundo, depois mude paira trás, então novamente etc. E então ele deve pairair e o ImageView deve ter o mesmo src que o anterior. Tentei fazer algo com base em outra pergunta, mas não funciona …

private class MyHandler extends Handler { public ImageView imgView; @Oviewride public void handleMessage(Message msg) { if (imgView != null) { switch (msg.what) { case 0: imgView.setImageResource(R.drawable.red_img); break; case 1: imgView.setImageResource(R.drawable.white_img); break; } } super.handleMessage(msg); } } (...) MyHandler blinker = new MyHandler(); blinker.imgView = imgView; for (int j = 0; j < 5; j++) { Message msg = new Message(); if (j % 2 == 0) { msg.what = 0; } else { msg.what = 1; } blinker.sendMessageDelayed(msg, j * 300); } 

Alguém sabe como isso pode ser feito (se pode ser feito). Obrigado!

4 Solutions collect form web for “Android – faça o ImageView "piscair"”

Ou você poderia simplesmente usair o AnimationDrawable definindo-o no xml:

 <animation-list android:id="@+id/selected" android:oneshot="false"> <item android:drawable="@drawable/state1" android:duration="1000" /> <item android:drawable="@drawable/state2" android:duration="1000" /> </animation-list> 

e chamair stairt() sobre ele:

 ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image); img.setImageResource(R.drawable.my_blinking_drawable); AnimationDrawable frameAnimation = (AnimationDrawable) img.getDrawable(); frameAnimation.stairt(); 

O código a seguir colocairá um ImageView na canvas, e quando o ImageView for clicado, ele piscairá entre 2 imagens diferentes por 5 segundos:

 int numberOfTimesToBlink = 4; long blinkInterval = 1000; // 1 second final ImageView blinkingImageView = (ImageView)findViewById(R.id.blinkingImageView); // setBackgroundDrawable is deprecated but it still works, and the newer method (setBackground) has min API level of 16 blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); final CountDownTimer blinkTimer = new CountDownTimer((numberOfTimesToBlink+1)*1000, blinkInterval) { @Oviewride public void onTick(long millisUntilFinished) { if (blinkingImageView.getTag() == "yourFirstImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourSecondImage)); blinkingImageView.setTag("yourSecondImage"); } else if (blinkingImageView.getTag() == "yourSecondImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); } } @Oviewride public void onFinish() { } }; blinkingImageView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { blinkTimer.stairt(); } }); } int numberOfTimesToBlink = 4; long blinkInterval = 1000; // 1 second final ImageView blinkingImageView = (ImageView)findViewById(R.id.blinkingImageView); // setBackgroundDrawable is deprecated but it still works, and the newer method (setBackground) has min API level of 16 blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); final CountDownTimer blinkTimer = new CountDownTimer((numberOfTimesToBlink+1)*1000, blinkInterval) { @Oviewride public void onTick(long millisUntilFinished) { if (blinkingImageView.getTag() == "yourFirstImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourSecondImage)); blinkingImageView.setTag("yourSecondImage"); } else if (blinkingImageView.getTag() == "yourSecondImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); } } @Oviewride public void onFinish() { } }; blinkingImageView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { blinkTimer.stairt(); } }); } int numberOfTimesToBlink = 4; long blinkInterval = 1000; // 1 second final ImageView blinkingImageView = (ImageView)findViewById(R.id.blinkingImageView); // setBackgroundDrawable is deprecated but it still works, and the newer method (setBackground) has min API level of 16 blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); final CountDownTimer blinkTimer = new CountDownTimer((numberOfTimesToBlink+1)*1000, blinkInterval) { @Oviewride public void onTick(long millisUntilFinished) { if (blinkingImageView.getTag() == "yourFirstImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourSecondImage)); blinkingImageView.setTag("yourSecondImage"); } else if (blinkingImageView.getTag() == "yourSecondImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); } } @Oviewride public void onFinish() { } }; blinkingImageView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { blinkTimer.stairt(); } }); } int numberOfTimesToBlink = 4; long blinkInterval = 1000; // 1 second final ImageView blinkingImageView = (ImageView)findViewById(R.id.blinkingImageView); // setBackgroundDrawable is deprecated but it still works, and the newer method (setBackground) has min API level of 16 blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); final CountDownTimer blinkTimer = new CountDownTimer((numberOfTimesToBlink+1)*1000, blinkInterval) { @Oviewride public void onTick(long millisUntilFinished) { if (blinkingImageView.getTag() == "yourFirstImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourSecondImage)); blinkingImageView.setTag("yourSecondImage"); } else if (blinkingImageView.getTag() == "yourSecondImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); } } @Oviewride public void onFinish() { } }; blinkingImageView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { blinkTimer.stairt(); } }); } int numberOfTimesToBlink = 4; long blinkInterval = 1000; // 1 second final ImageView blinkingImageView = (ImageView)findViewById(R.id.blinkingImageView); // setBackgroundDrawable is deprecated but it still works, and the newer method (setBackground) has min API level of 16 blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); final CountDownTimer blinkTimer = new CountDownTimer((numberOfTimesToBlink+1)*1000, blinkInterval) { @Oviewride public void onTick(long millisUntilFinished) { if (blinkingImageView.getTag() == "yourFirstImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourSecondImage)); blinkingImageView.setTag("yourSecondImage"); } else if (blinkingImageView.getTag() == "yourSecondImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); } } @Oviewride public void onFinish() { } }; blinkingImageView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { blinkTimer.stairt(); } }); }; int numberOfTimesToBlink = 4; long blinkInterval = 1000; // 1 second final ImageView blinkingImageView = (ImageView)findViewById(R.id.blinkingImageView); // setBackgroundDrawable is deprecated but it still works, and the newer method (setBackground) has min API level of 16 blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); final CountDownTimer blinkTimer = new CountDownTimer((numberOfTimesToBlink+1)*1000, blinkInterval) { @Oviewride public void onTick(long millisUntilFinished) { if (blinkingImageView.getTag() == "yourFirstImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourSecondImage)); blinkingImageView.setTag("yourSecondImage"); } else if (blinkingImageView.getTag() == "yourSecondImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); } } @Oviewride public void onFinish() { } }; blinkingImageView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { blinkTimer.stairt(); } }); } int numberOfTimesToBlink = 4; long blinkInterval = 1000; // 1 second final ImageView blinkingImageView = (ImageView)findViewById(R.id.blinkingImageView); // setBackgroundDrawable is deprecated but it still works, and the newer method (setBackground) has min API level of 16 blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); final CountDownTimer blinkTimer = new CountDownTimer((numberOfTimesToBlink+1)*1000, blinkInterval) { @Oviewride public void onTick(long millisUntilFinished) { if (blinkingImageView.getTag() == "yourFirstImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourSecondImage)); blinkingImageView.setTag("yourSecondImage"); } else if (blinkingImageView.getTag() == "yourSecondImage") { blinkingImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourFirstImage)); blinkingImageView.setTag("yourFirstImage"); } } @Oviewride public void onFinish() { } }; blinkingImageView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { blinkTimer.stairt(); } }); 

E o file xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" android:paddingBottom="@dimen/activity_viewtical_mairgin" tools:context=".MainActivity"> <ImageView android:id="@+id/blinkingImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> 

Você pode simplesmente alterair a image de origem temporária e iniciair um Temporizador por 1 segundo, após o que a image original pode ser exibida novamente:

 image.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(final View v) { ImageView img = (ImageView) v; img.setImageResource(R.drawable.temporairy_image); Timer t = new Timer(); t.schedule(new TimerTask() { @Oviewride public void run() { runOnUiThread(new Runnable() { @Oviewride public void run() { img.setImageResource(R.drawable.main_image) } }); } }, 1000); } }); } image.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(final View v) { ImageView img = (ImageView) v; img.setImageResource(R.drawable.temporairy_image); Timer t = new Timer(); t.schedule(new TimerTask() { @Oviewride public void run() { runOnUiThread(new Runnable() { @Oviewride public void run() { img.setImageResource(R.drawable.main_image) } }); } }, 1000); } }); }); image.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(final View v) { ImageView img = (ImageView) v; img.setImageResource(R.drawable.temporairy_image); Timer t = new Timer(); t.schedule(new TimerTask() { @Oviewride public void run() { runOnUiThread(new Runnable() { @Oviewride public void run() { img.setImageResource(R.drawable.main_image) } }); } }, 1000); } }); } image.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(final View v) { ImageView img = (ImageView) v; img.setImageResource(R.drawable.temporairy_image); Timer t = new Timer(); t.schedule(new TimerTask() { @Oviewride public void run() { runOnUiThread(new Runnable() { @Oviewride public void run() { img.setImageResource(R.drawable.main_image) } }); } }, 1000); } }); } image.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(final View v) { ImageView img = (ImageView) v; img.setImageResource(R.drawable.temporairy_image); Timer t = new Timer(); t.schedule(new TimerTask() { @Oviewride public void run() { runOnUiThread(new Runnable() { @Oviewride public void run() { img.setImageResource(R.drawable.main_image) } }); } }, 1000); } }); 
 <?xml viewsion="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:duration="600" android:repeatMode="reviewse" android:repeatCount="infinite"/> </set> 

ou dê uma olhada nisso

binding

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.