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

2023-10-17 13:50

本文主要是介绍android自定义view旋转,Android自定义View实现叶子飘动旋转效果(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

15059007401.gif?2017223101054

要实现这个效果,要在之前的功能上添加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,width,height);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

bgRect = new RectF(0,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改小

15059007402.gif?2017223101226

2、实现旋转

主要通过matrix.postRotate(float degrees,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,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,height);

//添加黄色白金

canvas.drawRect(bgRect,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;

}

}

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

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

小编个人微信号 jb51ccc

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

这篇关于android自定义view旋转,Android自定义View实现叶子飘动旋转效果(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/225810

相关文章

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

自定义注解SpringBoot防重复提交AOP方法详解

《自定义注解SpringBoot防重复提交AOP方法详解》该文章描述了一个防止重复提交的流程,通过HttpServletRequest对象获取请求信息,生成唯一标识,使用Redis分布式锁判断请求是否... 目录防重复提交流程引入依赖properties配置自定义注解切面Redis工具类controller

Nginx更新SSL证书的实现步骤

《Nginx更新SSL证书的实现步骤》本文主要介绍了Nginx更新SSL证书的实现步骤,包括下载新证书、备份旧证书、配置新证书、验证配置及遇到问题时的解决方法,感兴趣的了解一下... 目录1 下载最新的SSL证书文件2 备份旧的SSL证书文件3 配置新证书4 验证配置5 遇到的http://www.cppc

Nginx之https证书配置实现

《Nginx之https证书配置实现》本文主要介绍了Nginx之https证书配置的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录背景介绍为什么不能部署在 IIS 或 NAT 设备上?具体实现证书获取nginx配置扩展结果验证

SpringBoot整合 Quartz实现定时推送实战指南

《SpringBoot整合Quartz实现定时推送实战指南》文章介绍了SpringBoot中使用Quartz动态定时任务和任务持久化实现多条不确定结束时间并提前N分钟推送的方案,本文结合实例代码给大... 目录前言一、Quartz 是什么?1、核心定位:解决什么问题?2、Quartz 核心组件二、使用步骤1

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作