Android實現(xiàn)網(wǎng)易云音樂的旋轉(zhuǎn)專輯View
本文實例為大家分享了Android實現(xiàn)網(wǎng)易云音樂的旋轉(zhuǎn)專輯View,供大家參考,具體內(nèi)容如下
一.前言
最近做過 類似網(wǎng)易云音樂專輯唱片的效果,這里記錄下開發(fā)思路,僅供參考。但需求不完全與網(wǎng)易云音樂一樣,這個只有圖片會旋轉(zhuǎn)(網(wǎng)易云是整個磁盤都會旋轉(zhuǎn)),沒有唱片機械臂。

二.思路
如下圖,我這里是分為 圓形背景+旋轉(zhuǎn)的方形圖片+漸變圓環(huán)

三.關(guān)鍵代碼
1. 圓形背景
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="1dp" android:height="1dp" /> <solid android:color="#1AFFFFFF" /> </shape>
2.旋轉(zhuǎn)的方形圖片
// 設(shè)置旋轉(zhuǎn)動畫(屬性動畫)
private void init(Context context) {
View.inflate(context, R.layout.view_rotate_album, this);
ivAlbumPic = (ImageView) findViewById(R.id.view_pic);
animator = ObjectAnimator.ofFloat(ivAlbumPic, "rotation", 0.0F, 360.0F);
animator.setDuration(10 * 1000);
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.RESTART);
setPlaying(true);
}
// 更新播放狀態(tài)
public void setPlaying(boolean isPlaying) {
Log.d(TAG, "update RotateAlbumView: isPlaying = " + isPlaying);
if (isPlaying) {
if (!animator.isRunning()) {
animator.start();
} else {
animator.resume();
}
} else {
if (!animator.isStarted() || !animator.isRunning()) {
animator.cancel();
}
animator.pause();
}
}
3.漸變圓環(huán)
public class WidgetAlbumBgView extends View {
private Paint paint;
// 圓環(huán)半徑
private int ringWidth;
// 漸變色
private int[] colors;
private SweepGradient gradient;
// 圓線距圓環(huán)內(nèi)邊的距離
private int[] ringLinesMarginOut = {
dp2px(3.78F),
dp2px(7.03F),
dp2px(10.27F),
dp2px(12.97F)
};
// 圓線高度
private int ringLineWidth;
public WidgetAlbumBgView(Context context) {
this(context, null);
}
public WidgetAlbumBgView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public WidgetAlbumBgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeCap(Paint.Cap.ROUND);
colors = new int[]{getColor(R.color.widget_album_ring_color1), getColor(R.color.widget_album_ring_color2),
getColor(R.color.widget_album_ring_color1), getColor(R.color.widget_album_ring_color2),
getColor(R.color.widget_album_ring_color1)};
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WidgetAlbumBgView);
ringWidth = (int) typedArray.getDimension(R.styleable.WidgetAlbumBgView_ring_width, getResources().getDimension(R.dimen.widget_album_ring_width));
ringLineWidth = (int) typedArray.getDimension(R.styleable.WidgetAlbumBgView_ring_line_width, getResources().getDimension(R.dimen.widget_album_ring_line_width));
typedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStrokeWidth(ringWidth);
paint.setColor(getColor(R.color.widget_album_ring_color1));
if (gradient == null) {
gradient = new SweepGradient(getWidth() * 0.5F, getHeight() * 0.5F, colors, new float[]{
0F, 0.25F, 0.5F, 0.75F, 1F
});
}
paint.setShader(gradient);
// 畫圓環(huán)
canvas.drawCircle(getWidth() * 0.5F, getHeight() * 0.5F, (getWidth() - ringWidth) * 0.5F, paint);
paint.setShader(null);
paint.setStrokeWidth(ringLineWidth);
paint.setColor(getColor(R.color.widget_album_ring_line_color));
// 畫圓線
for (int marginOut : ringLinesMarginOut) {
canvas.drawCircle(getWidth() * 0.5F, getHeight() * 0.5F, getWidth() * 0.5F - marginOut - ringLineWidth * 0.5F, paint);
}
}
}
4.整體布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="@dimen/widget_album_size_vertical" android:layout_height="@dimen/widget_album_size_vertical" android:background="@drawable/rotate_album_bg"> <ImageView android:id="@+id/view_pic" android:layout_width="@dimen/widget_album_pic_size" android:layout_height="@dimen/widget_album_pic_size" android:layout_centerInParent="true" android:scaleType="centerInside" android:src="@mipmap/ic_qifenle" /> <com.example.musicalbumview.view.WidgetAlbumBgView android:layout_width="86.49dp" android:layout_height="86.49dp" android:layout_centerInParent="true" /> </RelativeLayout>
四.github地址
點擊鏈接
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程實現(xiàn)長按Button按鈕連續(xù)響應(yīng)功能示例
這篇文章主要介紹了Android編程實現(xiàn)長按Button按鈕連續(xù)響應(yīng)功能,涉及Android自定義按鈕及事件響應(yīng)操作相關(guān)技巧,需要的朋友可以參考下2017-01-01
微前端架構(gòu)ModuleFederationPlugin源碼解析
這篇文章主要為大家介紹了微前端架構(gòu)ModuleFederationPlugin源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android開發(fā)之ViewFlipper自動播放圖片功能實現(xiàn)方法示例
這篇文章主要介紹了Android開發(fā)之ViewFlipper自動播放圖片功能實現(xiàn)方法,結(jié)合實例形式分析了Android使用ViewFlipper實現(xiàn)圖片播放的相關(guān)界面布局及功能實現(xiàn)技巧,需要的朋友可以參考下2019-03-03
Android中新引進的Google Authenticator驗證系統(tǒng)工作原理淺析
這篇文章主要介紹了Android中新引進的Google Authenticator驗證系統(tǒng)工作原理淺析,需要的朋友可以參考下2014-10-10

