Android版网易云音乐唱片机唱片磁盘旋转及唱片机机械臂动画关键代码实现思路
Android版网易云音乐唱片机唱片磁盘旋转及唱片机机械臂动画关键代码实现思路
先看一看我的代码运行结果。
代码运行起来初始化状态:
点击开始按钮,唱片机的机械臂匀速接近唱片磁盘,同时唱片磁盘也开始匀速顺时针旋转:
点击停止按钮,唱片机的机械臂匀速抬离唱片磁盘,同时唱片磁盘停止旋转:
实现思路:
(一)旋转唱片磁盘。在附录文章12的基础上,实现网易云音乐风格的唱片磁盘。核心代码:
//最外部的半透明边线
OvalShape ovalShape0 = new OvalShape();
ShapeDrawable drawable0 = new ShapeDrawable(ovalShape0);
drawable0.getPaint().setColor(0x10000000);
drawable0.getPaint().setStyle(Paint.Style.FILL);
drawable0.getPaint().setAntiAlias(true); //黑色唱片边框
RoundedBitmapDrawable drawable1 = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.disc));
drawable1.setCircular(true);
drawable1.setAntiAlias(true); //内层黑色边线
OvalShape ovalShape2 = new OvalShape();
ShapeDrawable drawable2 = new ShapeDrawable(ovalShape2);
drawable2.getPaint().setColor(Color.BLACK);
drawable2.getPaint().setStyle(Paint.Style.FILL);
drawable2.getPaint().setAntiAlias(true); //最里面的图像
RoundedBitmapDrawable drawable3 = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil));
drawable3.setCircular(true);
drawable3.setAntiAlias(true); Drawable[] layers = new Drawable[4];
layers[0] = drawable0;
layers[1] = drawable1;
layers[2] = drawable2;
layers[3] = drawable3; LayerDrawable layerDrawable = new LayerDrawable(layers); int width = 10;
//针对每一个图层进行填充,使得各个圆环之间相互有间隔,否则就重合成一个了。
//layerDrawable.setLayerInset(0, width, width, width, width);
layerDrawable.setLayerInset(1, width , width, width, width );
layerDrawable.setLayerInset(2, width * 11, width * 11, width * 11, width * 11);
layerDrawable.setLayerInset(3, width * 12, width * 12, width * 12, width * 12); final View discView = findViewById(R.id.myView);
discView.setBackgroundDrawable(layerDrawable);
唱片磁盘旋转,则通过属性动画ObjectAnimator实现唱片磁盘的顺时针旋转:
discObjectAnimator = ObjectAnimator.ofFloat(discView, "rotation", 0, 360);
discObjectAnimator.setDuration(20000);
//使ObjectAnimator动画匀速平滑旋转
discObjectAnimator.setInterpolator(new LinearInterpolator());
//无限循环旋转
discObjectAnimator.setRepeatCount(ValueAnimator.INFINITE);
discObjectAnimator.setRepeatMode(ValueAnimator.INFINITE);
(二)唱片机的机械臂。在相对布局的顶部放置一个ImageView,这个ImageView就是机械臂的素材,当点击开始按钮时候,就使用Android属性动画操作该ImageView以左上角坐标位置(x=0,y=0)向下顺时针匀速旋转一定角度使得机械臂刚好落入到唱片磁盘内。当点击停止按钮时候,一方面控制唱片磁盘停止旋转,另一方面要操纵机械臂匀速逆时针向上旋转离开唱片磁盘。
我写的这个例子需要布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"> <View
android:id="@+id/myView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true" /> <ImageView
android:id="@+id/needle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:adjustViewBounds="true"
android:maxHeight="180dp"
android:src="@drawable/needle" /> <Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="开始" /> <Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="停止" /> </RelativeLayout>
上层全部完整Java代码:
package zhangphil.app; import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView; public class MainActivity extends Activity {
private ObjectAnimator discObjectAnimator,neddleObjectAnimator; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //最外部的半透明边线
OvalShape ovalShape0 = new OvalShape();
ShapeDrawable drawable0 = new ShapeDrawable(ovalShape0);
drawable0.getPaint().setColor(0x10000000);
drawable0.getPaint().setStyle(Paint.Style.FILL);
drawable0.getPaint().setAntiAlias(true); //黑色唱片边框
RoundedBitmapDrawable drawable1 = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.disc));
drawable1.setCircular(true);
drawable1.setAntiAlias(true); //内层黑色边线
OvalShape ovalShape2 = new OvalShape();
ShapeDrawable drawable2 = new ShapeDrawable(ovalShape2);
drawable2.getPaint().setColor(Color.BLACK);
drawable2.getPaint().setStyle(Paint.Style.FILL);
drawable2.getPaint().setAntiAlias(true); //最里面的图像
RoundedBitmapDrawable drawable3 = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil));
drawable3.setCircular(true);
drawable3.setAntiAlias(true); Drawable[] layers = new Drawable[4];
layers[0] = drawable0;
layers[1] = drawable1;
layers[2] = drawable2;
layers[3] = drawable3; LayerDrawable layerDrawable = new LayerDrawable(layers); int width = 10;
//针对每一个图层进行填充,使得各个圆环之间相互有间隔,否则就重合成一个了。
//layerDrawable.setLayerInset(0, width, width, width, width);
layerDrawable.setLayerInset(1, width , width, width, width );
layerDrawable.setLayerInset(2, width * 11, width * 11, width * 11, width * 11);
layerDrawable.setLayerInset(3, width * 12, width * 12, width * 12, width * 12); final View discView = findViewById(R.id.myView);
discView.setBackgroundDrawable(layerDrawable); ImageView needleImage= (ImageView) findViewById(R.id.needle); findViewById(R.id.startButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
discObjectAnimator.start();
neddleObjectAnimator.start();
}
}); findViewById(R.id.stopButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
discObjectAnimator.cancel();
neddleObjectAnimator.reverse();
}
}); discObjectAnimator = ObjectAnimator.ofFloat(discView, "rotation", 0, 360);
discObjectAnimator.setDuration(20000);
//使ObjectAnimator动画匀速平滑旋转
discObjectAnimator.setInterpolator(new LinearInterpolator());
//无限循环旋转
discObjectAnimator.setRepeatCount(ValueAnimator.INFINITE);
discObjectAnimator.setRepeatMode(ValueAnimator.INFINITE); neddleObjectAnimator = ObjectAnimator.ofFloat(needleImage, "rotation", 0, 25);
needleImage.setPivotX(0);
needleImage.setPivotY(0);
neddleObjectAnimator.setDuration(800);
neddleObjectAnimator.setInterpolator(new LinearInterpolator());
}
}
遗留问题:
(1)实际上,右上角机械臂这个ImageView以(x=0,y=0)的坐标位置旋转不是很恰当,因为这个机械臂作为图像素材,它本身的左上角肯定有部分空间位置,使得旋转时候不能够刚好以图中机械臂左上角那个螺丝衔接的位置旋转,也许是把机械臂衔接螺丝那个位置遮挡起来就像网易云音乐的做法,但是更完美的处理方法,我个人感觉勇应该分段设计这个机械臂:在素材设计上,把这个机械臂分为两段设计,最上的那个衔接螺丝单独抽出来,固定在布局上,然后螺丝下面把机械臂的尾部遮挡起来,旋转只选择机械臂,这样旋转就非常逼真。
(2)从代码中可以看出里面很多位置、角度、距离的值都是写死的,比如机械臂旋转的角度,机械臂的最大高度等等,作为demo说明解决方案没问题,但是在涉及到千百种Android屏幕尺寸各异的设备时候,情况变得比较复杂,要精心计算这些空间位置距离。
(3)发现一个Android自身的问题。在本例中,如果把背景图片background.jpg换成更高质量和分辨率的图如1080 X 1920,则动画执行非常之卡。但是如果把背景图片质量换成低分辨率图540 X 960则不影响动画执行效率。
我写的这个例子中用到的一些素材。
整个背景图background.jpg:
唱片机的机械臂needle.png
唱片磁盘包括头像的外部黑色外圈disc.png:
头像是我csdn的博客头像:
附录:
1,《Android动画Animation的两种加载执行方式》链接地址:http://blog.csdn.net/zhangphil/article/details/47394225
2,《Android AnimationDrawable动画与APP启动引导页面》链接地址:http://blog.csdn.net/zhangphil/article/details/47416915
3,《Android layer-list(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/517209244
4,《Android layer-list:联合shape(2)》链接地址:http://blog.csdn.net/zhangphil/article/details/51721283
5,《Android layer-list(3)》链接地址:http://blog.csdn.net/zhangphil/article/details/51721816
6,《Android Property Animation属性动画初识:透明渐变(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/50484224
7,《Android Property Animation属性动画:rotation旋转(2)》链接地址:http://blog.csdn.net/zhangphil/article/details/50495555
8,《Android Property Animation属性动画初识:位移translation(3)》链接地址:http://blog.csdn.net/zhangphil/article/details/50495844
9,《Android Property Animation属性动画:scale缩放动画(4)》链接地址:http://blog.csdn.net/zhangphil/article/details/50497404
10,《Android Property Animation属性动画集:AnimatorSet(5)》链接地址:http://blog.csdn.net/zhangphil/article/details/50498531
11,《Android ImageView加载圆形图片且同时绘制圆形图片的外部边缘边线及边框》链接地址:http://blog.csdn.net/zhangphil/article/details/51944262
12,《Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现》链接:http://blog.csdn.net/zhangphil/article/details/52035255
Android版网易云音乐唱片机唱片磁盘旋转及唱片机机械臂动画关键代码实现思路的更多相关文章
- android仿网易云音乐引导页、仿书旗小说Flutter版、ViewPager切换、爆炸菜单、风扇叶片效果等源码
Android精选源码 复现网易云音乐引导页效果 高仿书旗小说 Flutter版,支持iOS.Android Android Srt和Ass字幕解析器 Material Design ViewPage ...
- Android开发——网易云音乐使用的开源组件集合
前言 网易云音乐Android版从第一版使用到现在,全新的 Material Design 界面,更加清新.简洁.同样也是音乐播放器开发者,我们确实需要思考,相同的功能,会如何选择.感谢开源,让我们有 ...
- 由 UWP 版网易云音乐闪退引发的博文
今天,不知怎么的.网易云音乐出现了一打开就闪退的情况.百度了好些时候未果,就直接 Windows + i 打开 Windows 设置 > 应用 在应用和功能列表中找到网易云音乐,在展开的 高级选 ...
- Linux版网易云音乐播放音乐时无限显示“网络错误”的解决办法
安装 gstreamer0.10-plugins-good debian类系统: -plugins-good
- 实现一个网易云音乐的 BottomSheetDialog
作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...
- 用RotateDrawable实现网易云音乐唱片机效果
imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="唱片机" title=""> ...
- [转]网易云音乐Android版使用的开源组件
原文链接 网易云音乐Android版从第一版使用到现在,全新的 Material Design 界面,更加清新.简洁.同样也是音乐播放器开发者,我们确实需要思考,相同的功能,会如何选择.感谢开源,让我 ...
- 【第二版】高仿Android网易云音乐企业级项目实战课程介绍
这是一门付费Android项目课程,我们只做付费课程:同时也感谢大家的支持. 这一节,对本课程做一个简单介绍,以及放一些项目效果图,如果想直接查看项目视频演示,可以直接在腾讯课堂查看[高仿Androi ...
- 高仿Android网易云音乐OkHttp+Retrofit+RxJava+Glide+MVC+MVVM
简介 这是一个使用Java(以后还会推出Kotlin版本)语言,从0开发一个Android平台,接近企业级的项目(我的云音乐),包含了基础内容,高级内容,项目封装,项目重构等知识:主要是使用系统功能, ...
随机推荐
- H5页面快速搭建之高级字体应用实践
原文出处: 淘宝前端团队(FED)- 龙驭 背景 最近在开发一个 H5 活动页快速搭建平台,可以通过拖拽编辑图片,文字等元素组件,快速搭建出一个移动端的活动页面,基本交互和成品效果类似 PPT 软件. ...
- springboot 配置Ehcache
Ehcache的基本配置说明我就不说了.小编记录一下在springboot中使用Ehcache的使用方法. 第一步:在classpath下引入配置文件ehcache.xml 代码如下: <ehc ...
- CF962E Byteland, Berland and Disputed Cities
思路: http://codeforces.com/blog/entry/58869. 实现: #include <bits/stdc++.h> using namespace std; ...
- python实现: protobuf解释器
之前项目为了自动化,所以写一个protobuf的解释器,用来生成项目所需的格式. 当然现在通过以下链接的指导,跳过手工分析,直接生成代码了. https://developers.google.com ...
- Android(java)学习笔记166:上下文的区分
1.两种上下文: (1)Activity.this 界面的上下文 (2)getApplicationContext() 整 ...
- dmesg -检测和控制内核环缓冲
NAME dmesg - print or control the kernel ring buffer 总览 dmesg [ -c ] [ -n 级别 ] [ -s 缓冲区大小 ] 描述 dmesg ...
- iview 的 Carousel 走马灯 焦点图 不能用 建议换/vue-awesome-swiper
https://www.npmjs.com/package/vue-awesome-swiper
- 【整理】iview中刷新页面的时候更新导航菜单的active-name
iview中刷新页面的时候更新导航菜单的active-name https://blog.csdn.net/lhjuejiang/article/details/83212070
- Modal 下面的 v-model 就是显示不显示 true 或 false
Modal 下面的 v-model 就是显示不显示 true 或 false
- Oracle————存储过程与函数
存储过程存储过程参数模式包括IN.OUT. IN OUT. IN(默认参数模式):表示当存储过程别调用时,实参值被传递给形参:形参起变量作用,只能读该参数,而不能修改该参数.IN模式参数可以是变量或表 ...