简单的音乐播放,旋转动画

将所需要旋转的图片导入到 drawable 中,记住文件名全小写,只能有下划线、字母,数字,编写文件布局:
<?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>

应用简单的相对布局创建


如图所示,然后设置 xml 文件,即旋转条件,参数

<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 为旋转,fromDegrees 代表旋转的起始角度,toDegrees为终止角度359°(总共360°),pivotX与pivotY代表图片的旋转中心,repeatCount 返回值为-1则为无限循环,duration 代表旋转时间 

代码如下:

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;
        }


    }

}
两个按钮设置监听器,声明 Animation,设置动画的格式内容,setInterpolator 匀速运动

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值