Sunday, February 10, 2019

image animated in android studio.


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imgbart"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="fade"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/bart_simpsons" />
<ImageView
android:id="@+id/imgHumer"
android:layout_width="0dp"
android:layout_height="0dp"
android:alpha="0"
android:onClick="fade"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/homer_simpson" />
</android.support.constraint.ConstraintLayout>


Now java class


public class MainActivity extends AppCompatActivity {
ImageView imgBart;
ImageView imgHumer;
boolean animatedCheck = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgBart = (ImageView)findViewById(R.id.imgbart);
imgHumer = (ImageView)findViewById(R.id.imgHumer);
// amimateImage(); /*Image up and down */
//animateTwoImage();
}
/**
* fading image animate when image is clicked
* @param view
*/
public void fade(View view){
if (animatedCheck){
animatedCheck = false;
imgBart.animate().translationY(1000f).rotation(1360f).setDuration(2000);
}else {
animatedCheck = true;
imgBart.animate().translationY(-1000f).rotation(-1360f).setDuration(2000);
}
/**
* Image x and y axis
*/
/* if (animatedCheck){
animatedCheck = false;
*//*image are going to small .5 , half of the main image*//*
imgBart.animate().scaleY(.5f).scaleX(.5f).alpha(.5f).setDuration(2000);
}else {
animatedCheck = true;
imgBart.animate().scaleY(1f).scaleX(1f).alpha(1).setDuration(2000);
}*/
/*fading Image */
/* if (animatedCheck){
animatedCheck = false;
imgBart.animate().alpha(0).setDuration(2000);
imgHumer.animate().alpha(1).setDuration(2000);
}else {
animatedCheck = true;
imgBart.animate().alpha(1).setDuration(2000);
imgHumer.animate().alpha(0).setDuration(2000);
}*/
}
private void animateTwoImage() {
imgBart.animate().alpha(0).setDuration(2000);
}
/**
* Image Animated TOP to Bottom
*/
private void amimateImage() {
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0){
imgBart.animate().translationY(1000f).translationY(-1000f).setDuration(2000).alpha(0.5f);
}else {
imgBart.animate().translationY(1000f).translationY(1000f).setDuration(2000).alpha(1f);
}
}
};
Thread thread = new Thread(){
@Override
public void run() {
super.run();
for (int i = 0; i < 100; i++){
if (i%2==0){
try {
Thread.sleep(i*100);
handler.sendEmptyMessage(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
try {
Thread.sleep(i*100);
handler.sendEmptyMessage(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
thread.start();
}
}