修改进度条
This commit is contained in:
parent
94016c9dfa
commit
9a569169da
@ -0,0 +1,102 @@
|
|||||||
|
package com.hi.music.player.helper;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Canvas;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.Paint;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
|
public class CircularProgressBar extends View {
|
||||||
|
private Paint progressPaint;
|
||||||
|
private Paint backgroundPaint;
|
||||||
|
private float progress = 0;
|
||||||
|
private float maxProgress = 100;
|
||||||
|
|
||||||
|
private int fixedSize = 47; // 固定圆环的直径(dp)
|
||||||
|
private int progressWidth = 5; // 固定进度条宽度(dp)
|
||||||
|
|
||||||
|
public CircularProgressBar(Context context) {
|
||||||
|
super(context);
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public CircularProgressBar(Context context, AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public CircularProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||||
|
super(context, attrs, defStyleAttr);
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init() {
|
||||||
|
// 初始化用于绘制进度条的画笔
|
||||||
|
progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||||
|
progressPaint.setStyle(Paint.Style.STROKE);
|
||||||
|
progressPaint.setStrokeWidth(dpToPx(progressWidth)); // 设置进度条宽度(10dp)
|
||||||
|
progressPaint.setColor(Color.WHITE); // 设置进度条颜色为白色
|
||||||
|
|
||||||
|
// 初始化用于绘制背景环的画笔
|
||||||
|
backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||||
|
backgroundPaint.setStyle(Paint.Style.STROKE);
|
||||||
|
backgroundPaint.setStrokeWidth(dpToPx(progressWidth));
|
||||||
|
backgroundPaint.setColor(Color.GRAY); // 设置背景环颜色为灰色
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||||
|
// Convert dp to pixels
|
||||||
|
int diameterPx = dpToPx(fixedSize);
|
||||||
|
int size = diameterPx + dpToPx(progressWidth) * 2;
|
||||||
|
setMeasuredDimension(size, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDraw(Canvas canvas) {
|
||||||
|
super.onDraw(canvas);
|
||||||
|
|
||||||
|
// 获取视图中心坐标
|
||||||
|
float centerX = getWidth() / 2f;
|
||||||
|
float centerY = getHeight() / 2f;
|
||||||
|
|
||||||
|
// 固定圆环的半径
|
||||||
|
float radius = (Math.min(getWidth(), getHeight()) - dpToPx(progressWidth)) / 2f;
|
||||||
|
|
||||||
|
// 绘制背景环
|
||||||
|
canvas.drawCircle(centerX, centerY, radius, backgroundPaint);
|
||||||
|
|
||||||
|
// 计算进度条的起始角度和扫过的角度
|
||||||
|
float startAngle = -90f; // 从顶部开始
|
||||||
|
float sweepAngle = 360f * progress / maxProgress; // 根据进度计算扫过的角度
|
||||||
|
// 绘制进度条
|
||||||
|
canvas.drawArc(
|
||||||
|
centerX - radius, // 左上角 X 坐标
|
||||||
|
centerY - radius, // 左上角 Y 坐标
|
||||||
|
centerX + radius, // 右下角 X 坐标
|
||||||
|
centerY + radius, // 右下角 Y 坐标
|
||||||
|
startAngle, // 起始角度
|
||||||
|
sweepAngle, // 扫过的角度
|
||||||
|
false, // 是否使用扇形
|
||||||
|
progressPaint // 使用的画笔
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProgress(float progress) {
|
||||||
|
this.progress = Math.max(0, Math.min(progress, maxProgress)); // 限制进度值在 0 到 maxProgress 之间
|
||||||
|
invalidate(); // 请求重新绘制视图
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxProgress(float maxProgress) {
|
||||||
|
this.maxProgress = maxProgress;
|
||||||
|
this.progress = Math.max(0, Math.min(this.progress, maxProgress)); // 调整当前进度值以适应新的最大值
|
||||||
|
invalidate(); // 请求重新绘制视图
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 dp 转换为 px
|
||||||
|
private int dpToPx(int dp) {
|
||||||
|
float density = getResources().getDisplayMetrics().density;
|
||||||
|
return Math.round(dp * density); // 将 dp 值乘以屏幕密度转换为 px
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -10,52 +10,10 @@ import android.view.ViewGroup;
|
|||||||
|
|
||||||
import com.hi.music.player.R;
|
import com.hi.music.player.R;
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple {@link Fragment} subclass.
|
|
||||||
* Use the {@link AHomeFragment#newInstance} factory method to
|
|
||||||
* create an instance of this fragment.
|
|
||||||
*/
|
|
||||||
public class AHomeFragment extends Fragment {
|
public class AHomeFragment extends Fragment {
|
||||||
|
|
||||||
// TODO: Rename parameter arguments, choose names that match
|
|
||||||
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
|
|
||||||
private static final String ARG_PARAM1 = "param1";
|
|
||||||
private static final String ARG_PARAM2 = "param2";
|
|
||||||
|
|
||||||
// TODO: Rename and change types of parameters
|
|
||||||
private String mParam1;
|
|
||||||
private String mParam2;
|
|
||||||
|
|
||||||
public AHomeFragment() {
|
|
||||||
// Required empty public constructor
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Use this factory method to create a new instance of
|
|
||||||
* this fragment using the provided parameters.
|
|
||||||
*
|
|
||||||
* @param param1 Parameter 1.
|
|
||||||
* @param param2 Parameter 2.
|
|
||||||
* @return A new instance of fragment AHomeFragment.
|
|
||||||
*/
|
|
||||||
// TODO: Rename and change types and number of parameters
|
|
||||||
public static AHomeFragment newInstance(String param1, String param2) {
|
|
||||||
AHomeFragment fragment = new AHomeFragment();
|
|
||||||
Bundle args = new Bundle();
|
|
||||||
args.putString(ARG_PARAM1, param1);
|
|
||||||
args.putString(ARG_PARAM2, param2);
|
|
||||||
fragment.setArguments(args);
|
|
||||||
return fragment;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
if (getArguments() != null) {
|
|
||||||
mParam1 = getArguments().getString(ARG_PARAM1);
|
|
||||||
mParam2 = getArguments().getString(ARG_PARAM2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
|
|||||||
@ -45,17 +45,12 @@
|
|||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent">
|
app:layout_constraintTop_toTopOf="parent">
|
||||||
|
|
||||||
<!-- 圆形进度条 -->
|
<!-- 自定义圆形进度条 -->
|
||||||
<ProgressBar
|
<com.hi.music.player.helper.CircularProgressBar
|
||||||
android:id="@+id/circular_progress_bar"
|
android:id="@+id/circular_progress_bar"
|
||||||
style="?android:attr/progressBarStyleHorizontal"
|
android:layout_width="wrap_content"
|
||||||
android:layout_width="52dp"
|
android:layout_height="wrap_content"
|
||||||
android:layout_height="52dp"
|
android:layout_gravity="center"/>
|
||||||
android:layout_gravity="center"
|
|
||||||
android:indeterminate="false"
|
|
||||||
android:max="100"
|
|
||||||
android:progress="0"
|
|
||||||
android:progressDrawable="@drawable/circular_progress_drawable" />
|
|
||||||
|
|
||||||
<!-- 图片视图 -->
|
<!-- 图片视图 -->
|
||||||
<ImageView
|
<ImageView
|
||||||
@ -66,7 +61,6 @@
|
|||||||
android:src="@mipmap/cover" />
|
android:src="@mipmap/cover" />
|
||||||
</FrameLayout>
|
</FrameLayout>
|
||||||
|
|
||||||
|
|
||||||
<!-- 中间的两排 TextView -->
|
<!-- 中间的两排 TextView -->
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/top_text"
|
android:id="@+id/top_text"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user