全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Android自定义View实现叶子飘动旋转效果(四)

上一篇实现了叶子飘动功能,《Android自定义叶子飘动》 现在实现旋转效果

要实现这个效果,要在之前的功能上添加2个功能

1、通过matrix.postTranslate(int x, int y)在添加在Y轴上滑动

2、通过matrix.postRotate(float degrees, float px, float py)实现叶子旋转

代码实现

1、获取Y坐标

 private float getMatrixY() {
    float w = (float) ((float) 2 * Math.PI / width);
    int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2;
    return y;
  }

math.sin(Math.PI....)的取值范围貌似是-1~1之间。通过X坐标在整个width的比例,获取到Y坐标的比例。

这里方法有很多,我这边叶子Y坐标默认在Y轴中间,然后上下+-18px实现在Y轴的滑动,18滑动浮动比较大了

public LeafView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mResources = getResources();
    bgBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf_kuang, null)).getBitmap();
    leafBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf, null))).getBitmap();
   mLeafHeight = leafBitmap.getWidht();   

    bgPaint = new Paint();
    bgPaint.setColor(mResources.getColor(R.color.bg_color));
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    width = w;
    height = h;
    bgDestRect = new Rect(0, 0 , width, height);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    bgRect = new RectF(0, 0 , width, height);
    //添加黄色背景
    canvas.drawRect(bgRect, bgPaint);
    //添加背景图片
    canvas.drawBitmap(bgBitmap, null, bgDestRect, null);
    //添加叶子
    Matrix matrix = new Matrix();
    matrix.postTranslate(getMatriX(), getMatrixY);
    canvas.drawBitmap(leafBitmap, new Matrix(), new Paint());
    //重复调用onDraw()
    postInvalidate();
  }

  long cycleTime = 5000;  //叶子滑动一周的时间5秒
  long startTime = 0;   //叶子滑动开始时间
  private float getMatriX() {
    float betweenTime = startTime - System.currentTimeMillis();
    //周期结束再加一个cycleTime
    if(betweenTime < 0) {
      startTime = System.currentTimeMillis() + cycleTime;
      betweenTime = cycleTime;
    }
    //通过时间差计算出叶子的坐标
    float fraction = (float) betweenTime / cycleTime;
    float x = (int)(width * fraction);
    return x;
  }
  
  private float getMatrixY() {
    float w = (float) ((float) 2 * Math.PI / width);
    int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2;
    return y;
  }

看下效果就这样,在Y轴上实现上下浮动,如果要浮动小点,可以把18改小

2、实现旋转

主要通过matrix.postRotate(float degrees, float px, float py)

degrees就是角度(0~360),px,py就是图片的中心点

  private int getRotate() {
    float scale = ((startTime - System.currentTimeMillis())%cycleTime)/ (float)cycleTime;
    int rotate = (int)(scale * 360);
    return rotate;
  }

同样,通过当前叶子在X轴的比例,来计算出旋转的角度(0~360)

完整代码:

public class LeafView extends View {
  private Resources mResources;
  private Bitmap mLeafBitmap, bgBitmap;
  private int width, height;
  private int mLeafWidth,mLeafHeight;
  private Paint bgPaint;
  private RectF bgRect;
  private Rect bgDestRect;

  public LeafView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mResources = getResources();
    mLeafBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf, null)).getBitmap();
    mLeafWidth = mLeafBitmap.getWidht();
    mLeafHeight = mLeafBitmap.getHeight();
    bgBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf_kuang, null)).getBitmap();

    bgPaint = new Paint();
    bgPaint.setColor(mResources.getColor(R.color.bg_color));
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    width = w;
    height = h;
    bgDestRect = new Rect(0, 0 , width, height);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    bgRect = new RectF(0, 0 , width, height);
    //添加黄色白金
    canvas.drawRect(bgRect, bgPaint);
    //添加背景图片
    canvas.drawBitmap(bgBitmap, null, bgDestRect, null);

    canvas.save();
    Matrix matrix = new Matrix();
    //添加滑动
    matrix.postTranslate(getMatrixX(), getMatrixY());
    //添加旋转
    matrix.postRotate(getRotate(), getMatrixX() + mLeafWidth / 2, getMatrixY() + mLeafHeight / 2);
    canvas.drawBitmap(mLeafBitmap, matrix, new Paint());
    canvas.restore();
    postInvalidate();

  }
  long cycleTime = 5000;  //叶子滑动一周的时间5秒
  long startTime = 0;
  private float getMatrixX() {
    float betweenTime = startTime - System.currentTimeMillis();
    //周期结束再加一个cycleTime
    if(betweenTime < 0) {
      startTime = System.currentTimeMillis() + cycleTime;
      betweenTime = cycleTime;
    }
    //通过时间差计算出叶子的坐标
    float fraction = (float) betweenTime / cycleTime;
    float x = (int)(width * fraction);
    return x;
  }
  private float getMatrixY() {
    float w = (float) ((float) 2 * Math.PI / width);
    int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2;
    return y;
  }
  private int getRotate() {
    float scale = ((startTime - System.currentTimeMillis())%cycleTime)/ (float)cycleTime;
    int rotate = (int)(scale * 360);
    return rotate;
  }
}


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


# Android  # View  # 飘动  # 旋转  # Android自定义View实现QQ运动积分转盘抽奖功能  # Android 自定View实现仿QQ运动步数圆弧及动画效果  # Android自定义View仿微博运动积分动画效果  # Android UI之ImageView实现图片旋转和缩放  # Android使用RotateImageView 旋转ImageView  # Android UI设计系列之ImageView实现ProgressBar旋转效果(1)  # Android自定义View叶子旋转完整版(六)  # Android自定义View实现QQ音乐中圆形旋转碟子  # Android中imageView图片放大缩小及旋转功能示例代码  # Android自定义View图片按Path运动和旋转  # 计算出  # 再加  # 中心点  # 有很多  # 要在  # 自定义  # 大了  # 上一篇  # 大家多多  # 我这边  # 实现了  # 改小  # 在整个  # height  # return  # getHeight  # math  # mLeafHeight  # width  # sin 


相关文章: 免费ppt制作网站,有没有值得推荐的免费PPT网站?  建站之家VIP精选网站模板与SEO优化教程整合指南  制作网站的网址是什么,请问后缀为.com和.com.cn还有.cn的这三种网站是分别是什么类型的网站?  如何快速打造个性化非模板自助建站?  魔毅自助建站系统:模板定制与SEO优化一键生成指南  常州企业建站如何选择最佳模板?  如何在西部数码注册域名并快速搭建网站?  江苏网站制作公司有哪些,江苏书法考级官方网站?  如何高效配置IIS服务器搭建网站?  免费网站制作模板下载,除了易企秀之外还有什么H5平台可以制作H5长页面,最好是免费的?  企业微网站怎么做,公司网站和公众号有什么区别?  如何做静态网页,sublimetext3.0制作静态网页?  建站主机类型有哪些?如何正确选型  专业网站制作企业网站,如何制作一个企业网站,建设网站的基本步骤有哪些?  官网网站制作腾讯审核要多久,联想路由器newifi官网  香港网站服务器数量如何影响SEO优化效果?  javascript中对象的定义、使用以及对象和原型链操作小结  如何在阿里云完成域名注册与建站?  建站之星如何助力企业快速打造五合一网站?  如何通过主机屋免费建站教程十分钟搭建网站?  济南网站建设制作公司,室内设计网站一般都有哪些功能?  如何在自有机房高效搭建专业网站?  如何用AWS免费套餐快速搭建高效网站?  网站制作公司排行榜,抖音怎样做个人官方网站  网站制作公司排行榜,四大门户网站排名?  ,交易猫的商品怎么发布到网站上去?  中山网站推广排名,中山信息港登录入口?  武汉网站设计制作公司,武汉有哪些比较大的同城网站或论坛,就是里面都是武汉人的?  如何选择CMS系统实现快速建站与SEO优化?  定制建站模板如何实现SEO优化与智能系统配置?18字教程  ,南京靠谱的征婚网站?  如何用PHP快速搭建高效网站?分步指南  如何配置IIS站点权限与局域网访问?  三星网站视频制作教程下载,三星w23网页如何全屏?  中山网站制作网页,中山新生登记系统登记流程?  广东专业制作网站有哪些,广东省能源集团有限公司官网?  天津个人网站制作公司,天津网约车驾驶员从业资格证官网?  寿县云建站:智能SEO优化与多行业模板快速上线指南  建站中国官网:模板定制+SEO优化+建站流程一站式指南  建站之星下载版如何获取与安装?  ,如何利用word制作宣传手册?  学校免费自助建站系统:智能生成+拖拽设计+多端适配  定制建站是什么?如何实现个性化需求?  上海网站制作开发公司,上海买房比较好的网站有哪些?  音响网站制作视频教程,隆霸音响官方网站?  建站中国必看指南:CMS建站系统+手机网站搭建核心技巧解析  如何在阿里云香港服务器快速搭建网站?  建站ABC备案流程中有哪些关键注意事项?  建站主机系统SEO优化与智能配置核心关键词操作指南  如何在Mac上搭建Golang开发环境_使用Homebrew安装和管理Go版本 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。