Android自定义View实现QQ音乐中圆形旋转碟子

所属分类: 软件编程 / Android 阅读数: 67
收藏 0 赞 0 分享

QQ音乐中圆形旋转碟子的具体实现代码,供大家参考,具体内容如下

思路分析:

1、在onMeasure中测量整个View的宽和高后,设置宽高
2、获取我们res的图片资源后,在ondraw方法中进行绘制圆形图片
3、通过Handler发送Runnable来启动旋转线程(如果只想做圆形头像的话,这步可以去掉)
4、在布局中使用我们的View

效果图:

贴出我们的变量信息:

//view的宽和高 
int mHeight = 0; 
int mWidth = 0; 
//圆形图片 
Bitmap bitmap = null; 
//圆形图片的真实半径 
int radius = 0; 
//旋转动画的矩形 
Matrix matrix = new Matrix(); 
//旋转动画的角度 
int degrees = 0; 

步骤一:测量整个View的宽和高后,设置宽高

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
 //测量整个View的宽和高 
 mWidth = measuredWidth(widthMeasureSpec); 
 mHeight= measuredHeight(heightMeasureSpec); 
 setMeasuredDimension(mWidth, mHeight); 
} 
 
private int measuredWidth(int widthMeasureSpec) { 
 int Mode = MeasureSpec.getMode(widthMeasureSpec); 
 int Size = MeasureSpec.getSize(widthMeasureSpec); 
 if (Mode == MeasureSpec.EXACTLY) { 
  mWidth = Size; 
 } else { 
  //由图片决定宽度 
  int value = getPaddingLeft() + getPaddingRight() + bitmap.getWidth(); 
  if (Mode == MeasureSpec.AT_MOST) { 
   //由图片和Padding决定宽度,但是不能超过View的宽 
   mWidth = Math.min(value, Size); 
  } 
 } 
 return mWidth; 
} 
 
private int measuredHeight(int heightMeasureSpec) { 
 int Mode = MeasureSpec.getMode(heightMeasureSpec); 
 int Size = MeasureSpec.getSize(heightMeasureSpec); 
 if (Mode == MeasureSpec.EXACTLY) { 
  mHeight = Size; 
 } else { 
  //由图片决定高度 
  int value = getPaddingTop() + getPaddingBottom() + bitmap.getHeight(); 
  if (Mode == MeasureSpec.AT_MOST) { 
   //由图片和Padding决定高度,但是不能超过View的高 
   mHeight = Math.min(value, Size); 
  } 
 } 
 return mHeight; 
} 

步骤二:获取我们res的图片资源后,在ondraw方法中进行绘制圆形图片

//获取res的图片资源 
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); 
@Override 
protected void onDraw(Canvas canvas) { 
 super.onDraw(canvas); 
 canvas.concat(matrix); 
 //真实的半径必须是View的宽高最小值 
 radius = Math.min(mWidth, mHeight); 
 //如果图片本身宽高太大,进行相应的缩放 
 bitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false); 
 //画圆形图片 
 canvas.drawBitmap(createCircleImage(bitmap, radius), 0, 0, null); 
 matrix.reset(); 
} 
 
private Bitmap createCircleImage(Bitmap source, int radius) { 
 Paint paint = new Paint(); 
 paint.setAntiAlias(true); 
 Bitmap target = Bitmap.createBitmap(radius, radius, Bitmap.Config.ARGB_8888); 
 //产生一个同样大小的画布 
 Canvas canvas = new Canvas(target); 
 //首先绘制圆形 
 canvas.drawCircle(radius / 2, radius / 2, radius / 2, paint); 
 //使用SRC_IN模式显示后画图的交集处 
 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
 //绘制图片,从(0,0)画 
 canvas.drawBitmap(source, 0, 0, paint); 
 return target; 
} 

步骤三:通过Handler发送Runnable来启动旋转线程

//开始旋转 
mHandler.post(runnable); 
[java] view plain copy 在CODE上查看代码片派生到我的代码片
//-----------旋转动画----------- 
Handler mHandler = new Handler(); 
Runnable runnable = new Runnable() { 
 @Override 
 public void run() { 
  matrix.postRotate(degrees++, radius / 2, radius / 2); 
  //重绘 
  invalidate(); 
  mHandler.postDelayed(runnable, 50); 
 } 
}; 

步骤四:在布局中使用我们的View

<com.handsome.cycle.MyCycleView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" /> 

下面是整个类的源码

public class MyCycleView extends View { 
 
 //view的宽和高 
 int mHeight = 0; 
 int mWidth = 0; 
 //圆形图片 
 Bitmap bitmap = null; 
 //圆形图片的真实半径 
 int radius = 0; 
 //旋转动画的矩形 
 Matrix matrix = new Matrix(); 
 //旋转动画的角度 
 int degrees = 0; 
 
 //-----------旋转动画----------- 
 Handler mHandler = new Handler(); 
 Runnable runnable = new Runnable() { 
  @Override 
  public void run() { 
   matrix.postRotate(degrees++, radius / 2, radius / 2); 
   //重绘 
   invalidate(); 
   mHandler.postDelayed(runnable, 50); 
  } 
 }; 
 
 public MyCycleView(Context context) { 
  super(context); 
  initView(); 
 } 
 
 public MyCycleView(Context context, AttributeSet attrs) { 
  super(context, attrs); 
  initView(); 
 } 
 
 public MyCycleView(Context context, AttributeSet attrs, int defStyleAttr) { 
  super(context, attrs, defStyleAttr); 
  initView(); 
 } 
 
 public void initView() { 
  //获取res的图片资源 
  bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); 
  //开始旋转 
  mHandler.post(runnable); 
 } 
 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
  //测量整个View的宽和高 
  mWidth = measuredWidth(widthMeasureSpec); 
  mHeight = measuredHeight(heightMeasureSpec); 
  setMeasuredDimension(mWidth, mHeight); 
 } 
 
 private int measuredWidth(int widthMeasureSpec) { 
  int Mode = MeasureSpec.getMode(widthMeasureSpec); 
  int Size = MeasureSpec.getSize(widthMeasureSpec); 
  if (Mode == MeasureSpec.EXACTLY) { 
   mWidth = Size; 
  } else { 
   //由图片决定宽度 
   int value = getPaddingLeft() + getPaddingRight() + bitmap.getWidth(); 
   if (Mode == MeasureSpec.AT_MOST) { 
    //由图片和Padding决定宽度,但是不能超过View的宽 
    mWidth = Math.min(value, Size); 
   } 
  } 
  return mWidth; 
 } 
 
 private int measuredHeight(int heightMeasureSpec) { 
  int Mode = MeasureSpec.getMode(heightMeasureSpec); 
  int Size = MeasureSpec.getSize(heightMeasureSpec); 
  if (Mode == MeasureSpec.EXACTLY) { 
   mHeight = Size; 
  } else { 
   //由图片决定高度 
   int value = getPaddingTop() + getPaddingBottom() + bitmap.getHeight(); 
   if (Mode == MeasureSpec.AT_MOST) { 
    //由图片和Padding决定高度,但是不能超过View的高 
    mHeight = Math.min(value, Size); 
   } 
  } 
  return mHeight; 
 } 
 
 @Override 
 protected void onDraw(Canvas canvas) { 
  super.onDraw(canvas); 
  canvas.concat(matrix); 
  //真实的半径必须是View的宽高最小值 
  radius = Math.min(mWidth, mHeight); 
  //如果图片本身宽高太大,进行相应的缩放 
  bitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false); 
  //画圆形图片 
  canvas.drawBitmap(createCircleImage(bitmap, radius), 0, 0, null); 
  matrix.reset(); 
 } 
 
 private Bitmap createCircleImage(Bitmap source, int radius) { 
  Paint paint = new Paint(); 
  paint.setAntiAlias(true); 
  Bitmap target = Bitmap.createBitmap(radius, radius, Bitmap.Config.ARGB_8888); 
  //产生一个同样大小的画布 
  Canvas canvas = new Canvas(target); 
  //首先绘制圆形 
  canvas.drawCircle(radius / 2, radius / 2, radius / 2, paint); 
  //使用SRC_IN模式显示后画图的交集处 
  paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
  //绘制图片,从(0,0)画 
  canvas.drawBitmap(source, 0, 0, paint); 
  return target; 
 } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多