Interactive flip ImageView using ObjectAnimator

User touch on buttons to flip the ImageView forward/backward alternatively, around X-axis and Y-axis.

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

Button buttonFlipX, buttonFlipY;
ImageView imageView;
boolean dirX = true;
boolean dirY = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.image);
buttonFlipX = (Button)findViewById(R.id.buttonflipX);
buttonFlipY = (Button)findViewById(R.id.buttonflipY);

buttonFlipX.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(dirX){
dirX = false;
buttonFlipX.setText(“Flip X Backward”);
ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, “rotationX”, 0f, 180f);
flip.setDuration(500);
flip.start();
}else{
dirX = true;
buttonFlipX.setText(“Flip X Forward”);
ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, “rotationX”, 180f, 0f);
flip.setDuration(1000);
flip.start();
}
}
});

buttonFlipY.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(dirY){
dirY = false;
buttonFlipY.setText(“Flip Y Backward”);
ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, “rotationY”, 0f, 180f);
flip.setDuration(2000);
flip.start();
}else{
dirY = true;
buttonFlipY.setText(“Flip Y Forward”);
ObjectAnimator flip = ObjectAnimator.ofFloat(imageView, “rotationY”, 180f, 0f);
flip.setDuration(3000);
flip.start();
}
}
});
}
}

 

<Button
android:id=”@+id/buttonflipX”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Flip X Forward” />
<Button
android:id=”@+id/buttonflipY”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Flip Y Forward” />

<ImageView
android:id=”@+id/image”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:src=”@mipmap/ic_launcher” />
</LinearLayout>

 

Leave A Reply

Your email address will not be published.