溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android開(kāi)發(fā)中使用View實(shí)現(xiàn)一個(gè)旋轉(zhuǎn)音樂(lè)專輯功能

發(fā)布時(shí)間:2020-11-19 14:43:45 來(lái)源:億速云 閱讀:235 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

今天就跟大家聊聊有關(guān)Android開(kāi)發(fā)中使用View實(shí)現(xiàn)一個(gè)旋轉(zhuǎn)音樂(lè)專輯功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

具體內(nèi)容如下

Android開(kāi)發(fā)中使用View實(shí)現(xiàn)一個(gè)旋轉(zhuǎn)音樂(lè)專輯功能

二.思路

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

Android開(kāi)發(fā)中使用View實(shí)現(xiàn)一個(gè)旋轉(zhuǎn)音樂(lè)專輯功能

三.關(guān)鍵代碼

1. 圓形背景

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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)畫(huà)(屬性動(dòng)畫(huà))
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à)圓環(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));
  // 畫(huà)圓線
  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>

看完上述內(nèi)容,你們對(duì)Android開(kāi)發(fā)中使用View實(shí)現(xiàn)一個(gè)旋轉(zhuǎn)音樂(lè)專輯功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI