How can I change animated drawable(ball image) with other drawable when I met condition?
When ball hits bottom edge of display I want to change one (red ball image) with other (yellow ball image). Precisely, when ball hits bottom I want to change color(drawable) with yellow ball, but when bounce back(goes up) I want to change back to red. So I want ball change drawables when goes up and down until it stops.
I wrote code in android studio(java).
public class MainActivity extends AppCompatActivity {
private static final String TAG = "AnimationStarter";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bounceBallButton = (Button) findViewById(R.id.bounceBallButton);
final ImageView bounceBallImage = (ImageView) findViewById(R.id.bounceBallImage);
bounceBallButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
bounceBallImage.clearAnimation();
TranslateAnimation transAnim = new TranslateAnimation(0, 0, 0,
getDisplayHeight() - 530);
transAnim.setStartOffset(500);
transAnim.setDuration(5000);
transAnim.setFillAfter(true);
transAnim.setInterpolator(new BounceInterpolator());
transAnim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.i(TAG, "Starting button dropdown animation");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
Log.i(TAG,
"Ending button dropdown animation. Clearing animation and setting layout");
bounceBallImage.clearAnimation();
final int left = bounceBallImage.getLeft();
final int top = bounceBallImage.getTop();
final int right = bounceBallImage.getRight();
final int bottom = bounceBallImage.getBottom();
bounceBallImage.layout(left, top, right, bottom);
}
});
bounceBallImage.startAnimation(transAnim);
}
});
}
private int getDisplayHeight() {
return this.getResources().getDisplayMetrics().heightPixels;
}
This is the code:
//These are the animations of the Menu button getting bigger and smaller
final ScaleAnimation MenuScaleBigger = new ScaleAnimation(1f, 1.1f, 1f, 1.1f,1,0.5f,1,0.5f);
MenuScaleBigger.setDuration(400);
final ScaleAnimation MenuScaleSmaller = new ScaleAnimation(1.1f, 1f, 1.1f, 1f,1,0.5f,1,0.5f);
MenuScaleSmaller.setDuration(400);
MenuScaleBigger.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Menu.setScaleX(1.1f);
Menu.setScaleY(1.1f);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
MenuScaleSmaller.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Menu.setScaleX(1f);
Menu.setScaleY(1f);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
//This is the click listener that starts the animation of getting bigger
Menu.setOnClickListener(new View.OnClickListener() {
int counter = 0;
#Override
public void onClick(View v) {
if(counter == 0){
Menu.startAnimation(MenuScaleBigger);
counter++;
}
else{
Menu.startAnimation(MenuScaleSmaller);
counter--;
}
}
});
I think that the bouncing happens because of the Menu.ScaleX(); and Menu.ScaleY(); , but I need some way of making the animation stay in the last frame.
What I want is that if I click the Menu Imageview it becomes bigger, and stays bigger, and if I press it again it becomes smaller going back to the original size.
Remove the AnimationListeners and add these two lines
MenuScaleBigger.setFillAfter(true);
MenuScaleSmaller.setFillAfter(true);
setFillAfter(true); makes sure that the View stays in the position after animation and doesn't come back to it's base position
I have been working in an app where i have to play a media with seekbar. The layout consumes recyclerview. The code i used is
private class ViewHolder2 extends RecyclerView.ViewHolder {
public TextView name, filename;
ImageView img, play;
SeekBar seekBar;
public ViewHolder2(View view) {
super(view);
name = (TextView) view.findViewById(R.id.name);
filename = (TextView) view.findViewById(R.id.filename);
img = (ImageView) view.findViewById(R.id.image);
play = (ImageView) view.findViewById(R.id.play);
seekBar = (SeekBar) view.findViewById(R.id.seekbar);
}
}
And after initialising the class like above in my adapter class, in my play button onclick i coded like below.
vh2.play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (isFilePresent("Bombay.mp3")) {
final MediaPlayer mediaPlayer1 = new MediaPlayer();
try {
if (mediaPlayer1 != null) {
mediaPlayer1.setDataSource(audioPath);
mediaPlayer1.prepare();
mediaPlayer1.start();
}
int mediaPlayerDuration = mediaPlayer1.getDuration();
vh2.seekBar.setMax(mediaPlayerDuration);
vh2.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mediaPlayer1 != null && fromUser) {
mediaPlayer1.seekTo(progress * 1000);
}
}
});
handler = new Handler();
//Make sure you update Seekbar on UI thread
((Activity) context).runOnUiThread(new Runnable() {
#Override
public void run() {
if (mediaPlayer1 != null) {
int mCurrentPosition = mediaPlayer1.getCurrentPosition() / 1000;
vh2.seekBar.setProgress(mCurrentPosition);
}
handler.postDelayed(this, 1000);
}
});
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(context, "Oops! File not found!", Toast.LENGTH_SHORT).show();
}
}
});
where vh2 is nothing but the ViewHolder2 class instance. I got my song playing on button click, but seek bar not working.
My seekbar initialisation in xml file
<SeekBar
android:id="#+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Can you guys please help on what i am missing in this code?
I am using Android Studio, and I want to open a class page with an intent.
My code currently looks like this
public class StartUpScreen extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_up_page);
final ImageView MyImageView =(ImageView) findViewById(R.id.imageView);
final Animation Animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.startup);
final Animation Animation2 = AnimationUtils.loadAnimation(getBaseContext(), R.anim.abc_fade_out);
MyImageView.startAnimation(Animation);
Animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
MyImageView.startAnimation(Animation2);
finish();
Intent a = new Intent(this,MainPage.class);
startActivity(a);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
All looks good, but my Intent has a red line from (this,MainPage.class); I have looked and tried changing it with no luck.
Can anyone please help?
I am making some kind of puzzle game where there will be a lot of blocks that are made of ImageView(s). Now in this case, I have only setOnClickListener to imageView1, now rather than me writing the same block of setOnClickListener for each ImageView, how do I write the setOnclickListener once for all the ImageView(s) ??
Here's the sample code
MainActivity.java
public class MainActivity extends Activity {
ImageView imageView1, imageView2, imageView3;
int x, y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x = 0;
y = 0;
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener());
imageView1.startAnimation(animation);
x += 0;
y += 50;
}
});
}
private class MyAnimationListener implements Animation.AnimationListener {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
imageView1.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageView1.getWidth(),
imageView1.getHeight());
lp.setMargins(x, y, 0, 0);
imageView1.setLayoutParams(lp);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
}
}
All suggestions and improvements of my codes are greatly welcomed. Thanks in advance.
You can try to create your listener in a variable and then assign it to each ImageView. Something like this:
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
View.OnClickListener listener= new View.OnClickListener() {
#Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener());
if(v==imageView1)
imageView1.startAnimation(animation);
else if(v==imageView2)
imageView2.startAnimation(animation);
else if(v==imageView3)
imageView3.startAnimation(animation);
x += 0;
y += 50;
}
};
imageView1.setOnClickListener(listener);
imageView2.setOnClickListener(listener);
imageView3.setOnClickListener(listener);
Try to use this:
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
View.OnClickListener listener= new View.OnClickListener() {
#Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
ImageView vista=(ImageView) v;
animation.setAnimationListener(new MyAnimationListener(vista));
vista.startAnimation(animation);
x += 0;
y += 50;
}
};
imageView1.setOnClickListener(listener);
imageView2.setOnClickListener(listener);
imageView3.setOnClickListener(listener);
private class MyAnimationListener implements Animation.AnimationListener {
ImageView selected;
public MyAnimationListener(ImageView vista)
{
selected=vista;
}
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
selected.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(selected.getWidth(),
selected.getHeight());
lp.setMargins(x, y, 0, 0);
selected.setLayoutParams(lp);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
}
You can implement your custom OnClickListener with the Activity like below,
public class MainActivity extends Activity implements View.OnClickListener{
ImageView imageView1, imageView2, imageView3;
int x, y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x = 0;
y = 0;
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener(v));
v.startAnimation(animation);
x += 0;
y += 50;
}
private class MyAnimationListener implements Animation.AnimationListener {
private View mView;
public MyAnimationListener(View v)
{
this.mView = v;
}
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mView.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(mView.getWidth(),
mView.getHeight());
lp.setMargins(x, y, 0, 0);
mView.setLayoutParams(lp);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
}
}
Create a distinct listener:
View.OnclickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener());
v.startAnimation(animation);
x += 0;
y += 50;
}
};
then set the listener to your views:
imageView1.setOnClickListener(listener);
imageView2.setOnClickListener(listener);
imageView3.setOnClickListener(listener);
You can always extend your class as activity and implement OnClickListener. Then you should override the OnClick function. It takes a view as parameter that you can check if it is your intended view (i.e. your Imageviews)
eg:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
/**
* Created by Rohan on 15-09-2015.
*/
public class MyActivity extends Activity implements View.OnClickListener{
ImageView imageView1, imageView2, imageView3;
int x, y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x = 0;
y = 0;
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.image1 || v.getId() == R.id.image2 || v.getId() == R.id.image3 ){
//Do your stuff
}
}
}
Let MainActivity implements OnClickListener and call
public class MainActivity extends Activity implements OnClickListener {
ImageView imageView1, imageView2, imageView3;
int x, y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x = 0;
y = 0;
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.image1:
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener());
imageView1.startAnimation(animation);
x += 0;
y += 50;
break;
case R.id.image2:
break;
case R.id.image3:
break;
}
}
}
Although your code is in Java, there is an easy way in kotlin that someone could find useful. You can create an extension function for a list of views.
Extension function:
fun List<View>.setOnClickListener(onClickView: () -> Unit) {
this.forEach { it.setOnClickListener { onClickView.invoke() } }
}
How to use it:
listOf(ImageView, ImageView2, ImageView3).setOnClickListener {
// put your action here
}