Here i have some code that i edited from http://www.viralandroid.com/ They got me lined up with the correct code concepts and i modified it to fit my needs.
This code is different from the original code in that you click the image not a button to rotate the image.
The following code allows you to click on the Android image and it will rotate 360 degrees. This code uses xml to create the animation.
The code was written and tested in Android Studio 2.1.1 so i know i works.
Thank you for reading and i hope i helped. If you questions follow the instructions at the original website.
This code is different from the original code in that you click the image not a button to rotate the image.
The following code allows you to click on the Android image and it will rotate 360 degrees. This code uses xml to create the animation.
The code was written and tested in Android Studio 2.1.1 so i know i works.
Thank you for reading and i hope i helped. If you questions follow the instructions at the original website.
Java Code for MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity { ImageButton rotateImage; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
public void startRotatingImage(View view) { rotateImage = (ImageButton) findViewById(R.id.start_rotating_button); Animation startRotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.android_rotate_animation);
rotateImage.startAnimation(startRotateAnimation); } }
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity { ImageButton rotateImage; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
public void startRotatingImage(View view) { rotateImage = (ImageButton) findViewById(R.id.start_rotating_button); Animation startRotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.android_rotate_animation);
rotateImage.startAnimation(startRotateAnimation); } }
Create in folder res/anim/android_rotate_animation.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:duration="1500" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:startOffset="0" android:toDegrees="360" /> </set>
This is the activity_main.xml file
I have it in a relative layout.
<ImageButton android:id="@+id/start_rotating_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:src="@drawable/ic_action_name" android:onClick="startRotatingImage" />
No comments:
Post a Comment