Android自定义View实现叶子飘动旋转效果(四)
发布时间 - 2026-01-11 00:19:20 点击率:次上一篇实现了叶子飘动功能,《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
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南
Laravel怎么生成URL_Laravel路由命名与URL生成函数详解
PHP正则匹配日期和时间(时间戳转换)的实例代码
Laravel如何使用Vite进行前端资源打包?(配置示例)
Laravel如何实现API版本控制_Laravel API版本化路由设计策略
Laravel怎么集成Vue.js_Laravel Mix配置Vue开发环境
Win10如何卸载预装Edge扩展_Win10卸载Edge扩展教程【方法】
浅析上传头像示例及其注意事项
利用vue写todolist单页应用
Laravel如何实现多级无限分类_Laravel递归模型关联与树状数据输出【方法】
如何在 Telegram Web View(iOS)中防止键盘遮挡底部输入框
微信小程序 五星评分(包括半颗星评分)实例代码
Android中AutoCompleteTextView自动提示
Gemini手机端怎么发图片_Gemini手机端发图方法【步骤】
如何在云主机上快速搭建网站?
香港服务器网站推广:SEO优化与外贸独立站搭建策略
Laravel Vite是做什么的_Laravel前端资源打包工具Vite配置与使用
JavaScript中如何操作剪贴板_ClipboardAPI怎么用
如何撰写建站申请书?关键要点有哪些?
Laravel如何记录自定义日志?(Log频道配置)
实例解析angularjs的filter过滤器
如何使用 Go 正则表达式精准提取括号内首个纯字母标识符(忽略数字与嵌套)
Laravel Eloquent:优雅地将关联模型字段扁平化到主模型中
如何在阿里云域名上完成建站全流程?
Windows家庭版如何开启组策略(gpedit.msc)?(安装方法)
Laravel如何实现图片防盗链功能_Laravel中间件验证Referer来源请求【方案】
如何有效防御Web建站篡改攻击?
免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?
javascript如何操作浏览器历史记录_怎样实现无刷新导航
实现点击下箭头变上箭头来回切换的两种方法【推荐】
如何在阿里云通过域名搭建网站?
Laravel如何处理CORS跨域问题_Laravel项目CORS配置与解决方案
如何快速搭建FTP站点实现文件共享?
音乐网站服务器如何优化API响应速度?
Laravel如何清理系统缓存命令_Laravel清除路由配置及视图缓存的方法【总结】
Laravel如何实现多语言支持_Laravel本地化与国际化(i18n)配置教程
Laravel如何实现全文搜索_Laravel Scout集成Algolia或Meilisearch教程
HTML5段落标签p和br怎么选_文本排版常用标签对比【解答】
网站制作价目表怎么做,珍爱网婚介费用多少?
Laravel软删除怎么实现_Laravel Eloquent SoftDeletes功能使用教程
html5如何设置样式_HTML5样式设置方法与CSS应用技巧【教程】
Firefox Developer Edition开发者版本入口
如何快速上传自定义模板至建站之星?
Laravel与Inertia.js怎么结合_使用Laravel和Inertia构建现代单页应用
原生JS实现图片轮播切换效果
非常酷的网站设计制作软件,酷培ai教育官方网站?
Python文件流缓冲机制_IO性能解析【教程】
Python并发异常传播_错误处理解析【教程】
网站制作免费,什么网站能看正片电影?
如何在建站之星网店版论坛获取技术支持?

