activity_liu_sheng_ji
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.test.liushengji.LiuShengJiActivity"> <com.bwie.test.liushengji.GramophoneView android:id="@+id/gramophone" app:src="@drawable/mqq" app:picture_radiu="80dp" app:disk_rotate_speed="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_marginTop="60dp" android:onClick="pauseOrstart" android:text="切换播放状态" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
values文件夹下attrs
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="GramophoneView"> <attr name="picture_radiu" format="dimension"/>//中间图片的半径 <attr name="src" format="reference"/>//图片 <attr name="disk_rotate_speed" format="float"/>//唱片旋转的速度 </declare-styleable> </resources>
LiuShengJiActivity
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class LiuShengJiActivity extends AppCompatActivity { private GramophoneView gramophoneView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_liu_sheng_ji); //隐藏原有标题 getSupportActionBar().hide(); //查找控件 gramophoneView = (GramophoneView) findViewById(R.id.gramophone); } public void pauseOrstart(View view) { gramophoneView.pauseOrstart(); } }
GramophoneView
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; public class GramophoneView extends View{ private final float DEFUALT_DISK_ROTATE_SPEED = 1f; //磁盘旋转的速度 private final float DEFUALT_PICTURE_RAUID = 200; //中间图片默认半径 private final float DEFUALT_PAUSE_NEEDLE_DEGREE = -45; //暂停状态时唱针的旋转角度 private final float DEFUALT_PLAYING_NEEDLE_DEGREE = -15; //播放状态时唱针的旋转角度 private int pictrueRadio; //中间图片的半径 //指针 private int smallCircleRadiu = 10; //唱针顶部小圆半径,减小了一半 private int bigCircleRadiu = 15; //唱针顶部大圆半径,减小了一半 private int shortArmLength; private int longArmleLength; // 唱针手臂,较长那段的长度 private int shortHeadLength; // 唱针的头,较短那段的长度 private int longHeadLength; private Paint needlePaint; //唱片 private float halfMeasureWidth; private int diskRingWidth; // 黑色圆环宽度 private float diskRotateSpeed; // 唱片旋转速度 private Bitmap pictureBitmap; private Paint diskPaint; //状态控制 private boolean isPlaying; private float currentDiskDegree; // 唱片旋转角度 private float currentNeddleDegree = DEFUALT_PLAYING_NEEDLE_DEGREE; public GramophoneView(Context context) { super(context, null); } public GramophoneView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); needlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿 diskPaint = new Paint(Paint.ANTI_ALIAS_FLAG); TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.GramophoneView); //拿到xml中的图片和图片半径和,旋转的度数 pictrueRadio = (int) typedArray.getDimension(R.styleable.GramophoneView_picture_radiu, DEFUALT_PICTURE_RAUID); diskRotateSpeed = typedArray.getFloat(R.styleable.GramophoneView_disk_rotate_speed, DEFUALT_DISK_ROTATE_SPEED); Drawable drawable = typedArray.getDrawable(R.styleable.GramophoneView_src); if (drawable == null) { pictureBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); } else { pictureBitmap = ((BitmapDrawable)drawable).getBitmap(); } //初始化唱片的变量 diskRingWidth = pictrueRadio >> 1; //图片半径和黑色圆环的和 等于 指针的总长度 shortHeadLength = (pictrueRadio + diskRingWidth) / 15; longHeadLength = shortHeadLength << 1; //左移相当于乘以2 shortArmLength = longHeadLength << 1; longArmleLength = shortArmLength << 1; } public GramophoneView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = (pictrueRadio+diskRingWidth)*2; int hight = (pictrueRadio+diskRingWidth)*2+longArmleLength; //根据我们理想的宽和高 和xml中设置的宽高,按resolveSize规则做最后的取舍 //resolveSize规则 1、精确模式,按 int measurewidth = resolveSize(width,widthMeasureSpec); int measurehight = resolveSize(hight,heightMeasureSpec); setMeasuredDimension(measurewidth,measurehight);//设置测量的长度 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); halfMeasureWidth = getMeasuredWidth() >> 1; drawDisk(canvas); //画唱片 drawNeedle(canvas);//画指针 if (currentNeddleDegree > DEFUALT_PAUSE_NEEDLE_DEGREE) { invalidate(); } } private void drawNeedle(Canvas canvas) { canvas.save();//保存 //移动坐标原点,画指针第一段 canvas.translate(halfMeasureWidth, 0); canvas.rotate(currentNeddleDegree); needlePaint.setColor(Color.parseColor("#C0C0C0")); needlePaint.setStrokeWidth(10); canvas.drawLine(0, 0, 0, longArmleLength, needlePaint); //画指针第二段 canvas.translate(0, longArmleLength); canvas.rotate(-30);// needlePaint.setStrokeWidth(10); canvas.drawLine(0, 0, 0, shortArmLength, needlePaint); //画指针第三段 canvas.translate(0, shortArmLength); needlePaint.setStrokeWidth(15); canvas.drawLine(0, 0, 0, longHeadLength, needlePaint); //画指针的第四段 canvas.translate(0, longHeadLength); needlePaint.setStrokeWidth(25); canvas.drawLine(0, 0, 0, shortHeadLength, needlePaint); canvas.restore(); //画指针的支点 canvas.save(); canvas.translate(halfMeasureWidth, 0); needlePaint.setColor(Color.parseColor("#8A8A8A")); needlePaint.setStyle(Paint.Style.FILL); canvas.drawCircle(0, 0, bigCircleRadiu, needlePaint); needlePaint.setColor(Color.parseColor("#C0C0C0")); canvas.drawCircle(0, 0, smallCircleRadiu, needlePaint); canvas.restore(); if (isPlaying) { if (currentNeddleDegree < DEFUALT_PLAYING_NEEDLE_DEGREE) { currentNeddleDegree += 3; } } else { if (currentNeddleDegree > DEFUALT_PAUSE_NEEDLE_DEGREE) { currentNeddleDegree -= 3; } } } private void drawDisk(Canvas canvas) { currentDiskDegree = currentDiskDegree % 360 + diskRotateSpeed; canvas.save(); canvas.translate(halfMeasureWidth, longArmleLength + diskRingWidth + pictrueRadio); canvas.rotate(currentDiskDegree); diskPaint.setColor(Color.BLACK); diskPaint.setStyle(Paint.Style.STROKE); diskPaint.setStrokeWidth(pictrueRadio / 2); //diskPaint.setStrokeWidth(20); canvas.drawCircle(0, 0, pictrueRadio + diskRingWidth / 2, diskPaint); Path path = new Path(); path.addCircle(0, 0, pictrueRadio, Path.Direction.CW); canvas.clipPath(path); Rect src = new Rect(); src.set(0, 0, pictureBitmap.getWidth(), pictureBitmap.getHeight()); Rect dst = new Rect(); dst.set(-pictrueRadio, -pictrueRadio, pictrueRadio, pictrueRadio); canvas.drawBitmap(pictureBitmap, src, dst, null); canvas.restore(); } public void pauseOrstart(){ isPlaying =!isPlaying; invalidate(); } /** * 设置图片半径 */ public void setPictureRadius(int pictureRadius) { this.pictrueRadio = pictureRadius; } /** * 设置唱片旋转速度 */ public void setDiskRotateSpeed(float diskRotateSpeed) { this.diskRotateSpeed = diskRotateSpeed; } /** * 设置图片资源id */ public void setPictureRes(int resId) { pictureBitmap = BitmapFactory.decodeResource(getContext().getResources(), resId); invalidate(); } }