User touch on buttons to flip the ImageView forward/backward alternatively, around X-axis and Y-axis.
package com.example.androidflipview;
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();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
a
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com"
android:textSize="28dp"
android:textStyle="bold" />
<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>
No comments:
Post a Comment