How to change ball image? - java

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;
}

Related

How can I reset the timer in a ViewFlipper back to 0

The app I'm trying to develop has a ViewFlipper and some buttons bellow corresponding to views, you click on the third button and the ViewFlipper changes views to the 3rd image, the problem is that once I click that button the timer doesn't reset to 0, so for example if I was on the second image for 4 secs and then if I change to the 3rd image, after 1 sec the ViewFlipper will change to the 4th image, I wanted the timer to reset back to 0 so that way it would stay on the view that I changed to for 5 secs.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_reservation_page);
super.onCreate(savedInstanceState);
viewFlipper=findViewById(R.id.viewflipper);
int images[]={R.drawable.icon_promotions,R.drawable.icon_promotions,R.drawable.icon_promotions};
final int numimages=images.length;
// ADD THE BUTTONS TO THE LINEARLAYOUT
LinearLayout linearLayout=findViewById(R.id.btn_slide_layout);
for(int f=0;f<numimages;f++){
final int num=f;
ImageView imageView2=new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(15, 0, 0, 0);
imageView2.setLayoutParams(params2);
imageView2.setId(f);
if (f==0){
imageView2.setBackgroundResource(R.drawable.icon_message_maincolor);
}
else{
imageView2.setBackgroundResource(R.drawable.icon_promotions);
}
imageView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewFlipper.setDisplayedChild(num);
viewFlipper.setFlipInterval(flippertime);
}
});
linearLayout.addView(imageView2);
}
for(int i=0;i<numimages;i++){
ImageView imageView=new ImageView(this);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(images[i]);
flipperimages(imageView);
}
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationRepeat(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationEnd(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void changeslidericon(int currentid){
for(int i=0;i<numimages;i++){
if(currentid!=i){
findViewById(i).setBackgroundResource(R.drawable.icon_promotions);
}
else{
findViewById(i).setBackgroundResource(R.drawable.icon_message_maincolor);
}
}
}
});
}
public void flipperimages(View image){
viewFlipper.addView(image);
viewFlipper.setFlipInterval(flippertime);
viewFlipper.setAutoStart(true);
viewFlipper.setInAnimation(this, R.anim.slide_in_right);
viewFlipper.setOutAnimation(this, R.anim.slide_out_left);
}
add this to your image2 click listener :
viewFlipper.setDisplayedChild(num);
viewFlipper.stopFlipping();
viewFlipper.setFlipInterval(flippertime);
viewFlipper.startFlipping();
Ok i found the answer, what i needed to do was use a Handler, the problem at first is that the Handler would overwrite other previous handlers and when i changed to one view and then changed to another both handlers would still be waiting, so all i needed to add was this line handler.removeCallbacks(runnable);
Here's my full current code:
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_reservation_page);
super.onCreate(savedInstanceState);
viewFlipper=findViewById(R.id.viewflipper);
int images[]={R.drawable.icon_promotions,R.drawable.icon_promotions,R.drawable.icon_promotions};
final int numimages=images.length;
// ADD THE BUTTONS TO THE LINEARLAYOUT
LinearLayout linearLayout=findViewById(R.id.btn_slide_layout);
for(int f=0;f<numimages;f++){
final int num=f;
ImageView imageView2=new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(15, 0, 0, 0);
imageView2.setLayoutParams(params2);
imageView2.setId(f);
if (f==0){
imageView2.setBackgroundResource(R.drawable.icon_message_maincolor);
}
else{
imageView2.setBackgroundResource(R.drawable.icon_promotions);
}
imageView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
handler.removeCallbacks(runnable);
} catch(Exception e){}
viewFlipper.setDisplayedChild(num);
viewFlipper.stopFlipping();
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
viewFlipper.showNext();
viewFlipper.startFlipping();
}
}; handler.postDelayed(runnable, flippertime);
}
});
linearLayout.addView(imageView2);
}
for(int i=0;i<numimages;i++){
ImageView imageView=new ImageView(this);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setBackgroundResource(images[i]);
flipperimages(imageView);
}
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationRepeat(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void onAnimationEnd(Animation animation) {
changeslidericon(viewFlipper.getDisplayedChild());
}
public void changeslidericon(int currentid){
for(int i=0;i<numimages;i++){
if(currentid!=i){
findViewById(i).setBackgroundResource(R.drawable.icon_promotions);
}
else{
findViewById(i).setBackgroundResource(R.drawable.icon_message_maincolor);
}
}
}
});
}
public void flipperimages(View image){
viewFlipper.addView(image);
viewFlipper.setFlipInterval(flippertime);
viewFlipper.setAutoStart(true);
viewFlipper.setInAnimation(this, R.anim.slide_in_right);
viewFlipper.setOutAnimation(this, R.anim.slide_out_left);
}

I want to change image with blink animation in android and images are directly loading from server with Glide. or any other option to do this?

This is where I want to change the image when animation blinks. I'm not able to set more than one Image in it, I'm new to Android development.
enter image description here
public class MainActivity extends AppCompatActivity {
RelativeLayout suggestion;
CircleImageView suggestionImage;
AnimationDrawable animation;
BitmapDrawable frame;
String[] imageUrl = new String[]{"https://i.picsum.photos/id/663/200/200.jpg", "https://i.picsum.photos/id/1026/200/200.jpg", "https://i.picsum.photos/id/1037/200/200.jpg"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
suggestion = (RelativeLayout) findViewById(R.id.suggestionContainer);
suggestionImage = (CircleImageView) findViewById(R.id.suggestionImage);
final Animation moveOut = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move_out);
final Animation blink = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink);
moveOut.setFillAfter(true);
final Handler slide = new Handler();
slide.postDelayed(new Runnable() {
#Override
public void run() {
suggestion.startAnimation(moveOut);
}
}, 2100);
suggestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"touched", Toast.LENGTH_SHORT).show();
}
});
Glide.with(this)
.asBitmap()
.load(imageUrl[1])
.into(new CustomTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
frame = new BitmapDrawable(resource);
animation.addFrame(frame, 1000);
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
animation = new AnimationDrawable();
animation.setOneShot(true);
suggestionImage.setBackgroundDrawable(animation);
suggestionImage.startAnimation(blink);
animation.start();
}
}
check if this can help.
image = (ImageView) findViewById(R.id.image);
Animation animation = new AlphaAnimation((float) 0.5, 0); // Change alpha from fully visible to invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter
// animation
// rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation
// infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the
// end so the button will
// fade back in
image.startAnimation(animation);

Android java scale animation bounces

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

How to make a button for a color

I'm creating a game app for android that when a green circle or (Button) is clicked then you get rewarded and if a red circle or (Button) then you lose the game. My problem is creating these buttons for the circles. I already have made the generator for the circles with the help of someone else on here and I have attempted to create the buttons. Here is all the code I have so far:
public class Main extends Activity
{
DrawingView v;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
v = new DrawingView(this);
setContentView(v);
Button redCircle = (Button) findViewById(Color.RED);
redCircle.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent s = new Intent(Main.this, ifRed());
startActivity(s);
}
private Class<?> ifRed()
{
// TODO Auto-generated method stub
String str = "You clicked on a red circle. You Loose!";
System.out.print(str);
return null;
}
});
}
}
and the circle generator below:
public class DrawingView extends View
{
public DrawingView(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
private int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
private final Runnable updateCircle = new Runnable()
{
#Override
public void run()
{
lastColor = random.nextInt(2) == 1 ? Color.RED : Color.GREEN;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 380);
}
};
#Override
protected void onAttachedToWindow()
{
super.onAttachedToWindow();
handler.post(updateCircle);
}
#Override
protected void onDetachedFromWindow()
{
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
// your other stuff here
canvas.drawCircle(random.nextInt(canvas.getWidth()-radius/2) + radius/2f, random.nextInt(canvas.getHeight()-radius/2) + radius/2f, radius, paint);
}
}

What is the mistake with my Android animation of my ImageView?

This code works only for the first animation. Though the final position of the ImageView is not exactly the point of the MotionEvent and the animation looks not smooth. The result of all other MotionEvents is actually no animation. The ImageView just appears at the point i touch the screen.
Here is the code.
ImageView image;
MyAnimationListener am;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
image = (ImageView)findViewById(R.id.imageView1);
am = new MyAnimationListener(image);
}
#Override
public boolean onTouchEvent(MotionEvent mEvent){
if(mEvent.getAction()==MotionEvent.ACTION_DOWN){
TranslateAnimation tAnimation = new TranslateAnimation(image.getX(),mEvent.getX(),image.getY(),mEvent.getY());
tAnimation.setDuration(500);
am.setCoordinates(mEvent.getX(), mEvent.getY());
tAnimation.setAnimationListener(am);
AnimationSet aSet = new AnimationSet(true);
aSet.addAnimation(tAnimation);
image.startAnimation(aSet);
}
return true;
}
And the interesting methods of MyAnimationListener.
ImageView i;
float x,y;
public MyAnimationListener(ImageView i){
this.i = i;
}
public void setCoordinates(float x, float y){
this.x = x;
this.y = y;
}
#Override
public void onAnimationEnd(Animation animation) {
i.setX(x);
i.setY(y);
}
Thanks in advance.
may be you need to do call a fillAfter so that the postion is not reset at the end of the animation. see link

Categories

Resources