Android實(shí)現(xiàn)網(wǎng)易云音樂的旋轉(zhuǎn)專輯View
本文實(shí)例為大家分享了Android實(shí)現(xiàn)網(wǎng)易云音樂的旋轉(zhuǎn)專輯View,供大家參考,具體內(nèi)容如下
一.前言
最近做過 類似網(wǎng)易云音樂專輯唱片的效果,這里記錄下開發(fā)思路,僅供參考。但需求不完全與網(wǎng)易云音樂一樣,這個(gè)只有圖片會(huì)旋轉(zhuǎn)(網(wǎng)易云是整個(gè)磁盤都會(huì)旋轉(zhuǎn)),沒有唱片機(jī)械臂。
二.思路
如下圖,我這里是分為 圓形背景+旋轉(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)動(dòng)畫(屬性動(dòng)畫) 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地址
點(diǎn)擊鏈接
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android系統(tǒng)按鍵音framework流程源碼詳細(xì)解析
這篇文章主要為大家詳細(xì)介紹了android系統(tǒng)按鍵音framework流程源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Android編程之TabWidget選項(xiàng)卡用法實(shí)例分析
這篇文章主要介紹了Android編程之TabWidget選項(xiàng)卡用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了TabWidget選項(xiàng)卡的具體實(shí)現(xiàn)技巧與使用注意事項(xiàng),需要的朋友可以參考下2015-12-12Android中通過外部程序啟動(dòng)App的三種方法
這篇文章主要介紹了Android中通過外部程序啟動(dòng)App的三種方法,本文講解了直接通過包名、通過自定義的Action、通過Scheme三種方法,并分別給出操作代碼,需要的朋友可以參考下2015-04-04Android編程簡(jiǎn)單設(shè)置ListView分割線的方法
這篇文章主要介紹了Android編程簡(jiǎn)單設(shè)置ListView分割線的方法,涉及Android布局簡(jiǎn)單操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02實(shí)例講解Android多線程應(yīng)用開發(fā)中Handler的使用
這篇文章主要介紹了Android多線程應(yīng)用開發(fā)中Handler的使用,Handle主要被用來更新UI和處理消息,需要的朋友可以參考下2016-01-01