Simple music playback, rotation animation

Import the picture to be rotated into the drawable, remember that the file name is all lowercase, only underscores, letters, numbers, and write the file layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.wangban.yzbbanban.myapplicationturnaround.MainActivity">

    <ImageView
        android:id="@+id/iv_revolve"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/default_play_activity_bg1"
        android:layout_centerInParent="true"
        />
    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"
        android:layout_alignParentBottom="true"
        />
    <Button
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        />
</RelativeLayout>

Apply simple relative layout creation


As shown in the figure, then set the xml file, that is, the rotation condition, the parameter

<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:fromDegrees="0"
        android:toDegrees="359"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:duration="9000" />
</se



 rotate is rotation, fromDegrees represents the starting angle of rotation, toDegrees is the end angle of 359° (360° in total), pivotX and pivotY represent the rotation center of the image, repeatCount return value -1 means infinite loop, and duration represents the rotation time 
 

code show as below:

package com.wangban.yzbbanban.myapplicationturnaround;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btnStart;
    private Button btnStop;
    private ImageView ivRevolve;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnStart= (Button) findViewById(R.id.btn_start);
        btnStop= (Button) findViewById(R.id.btn_stop);
        ivRevolve= (ImageView) findViewById(R.id.iv_revolve);
        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_start:
                Animation animation;
                animation = AnimationUtils.loadAnimation(this, R.anim.set_revolve);
                LinearInterpolator  i= new LinearInterpolator();
                animation.setInterpolator(i);
                if (animation != null) {
                    ivRevolve.startAnimation(animation);
                }
            break;
            case R.id.btn_stop:
                ivRevolve.clearAnimation();
            break;
        }


    }

}
Two buttons set the listener, declare Animation, set the format content of the animation, setInterpolator uniform motion

Guess you like

Origin blog.csdn.net/u013377003/article/details/51412667