上一篇实现了叶子飘动功能,《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
相关文章:
临沂网站制作企业,临沂第三中学官方网站?
车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?
小自动建站系统:AI智能生成+拖拽模板,多端适配一键搭建
如何高效完成自助建站业务培训?
存储型VPS适合搭建中小型网站吗?
如何选择长沙网站建站模板?H5响应式与品牌定制哪个更优?
公司门户网站制作公司有哪些,怎样使用wordpress制作一个企业网站?
专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?
已有域名和空间如何快速搭建网站?
C++中的Pimpl idiom是什么,有什么好处?(隐藏实现)
建站之星安装提示数据库无法连接如何解决?
广东专业制作网站有哪些,广东省能源集团有限公司官网?
攀枝花网站建设,攀枝花营业执照网上怎么年审?
我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?
如何在Golang中实现微服务服务拆分_Golang微服务拆分与接口管理方法
建站之星导航如何优化提升用户体验?
武汉外贸网站制作公司,现在武汉外贸前景怎么样啊?
天津个人网站制作公司,天津网约车驾驶员从业资格证官网?
常州自助建站:操作简便模板丰富,企业个人快速搭建网站
建站主机与服务器功能差异如何区分?
建站之星图片链接生成指南:自助建站与智能设计教程
头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?
免费网站制作模板下载,除了易企秀之外还有什么H5平台可以制作H5长页面,最好是免费的?
如何用虚拟主机快速搭建网站?详细步骤解析
C#如何使用XPathNavigator高效查询XML
黑客如何利用漏洞与弱口令入侵网站服务器?
建站之星安装后如何自定义网站颜色与字体?
网站制作公司排行榜,抖音怎样做个人官方网站
宠物网站制作html代码,有没有专门介绍宠物如何养的网站啊?
Python lxml的etree和ElementTree有什么区别
建站之星如何开启自定义404页面避免用户流失?
网站制作公司排行榜,四大门户网站排名?
高端智能建站公司优选:品牌定制与SEO优化一站式服务
移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?
个人网站制作流程图片大全,个人网站如何注销?
如何快速生成可下载的建站源码工具?
如何快速搭建高效WAP手机网站?
设计网站制作公司有哪些,制作网页教程?
建站之星展会模板:智能建站与自助搭建高效解决方案
如何通过远程VPS快速搭建个人网站?
微课制作网站有哪些,微课网怎么进?
建站之星收费标准详解:套餐费用及年费价格表一览
动图在线制作网站有哪些,滑动动图图集怎么做?
深入理解Android中的xmlns:tools属性
建站IDE高效指南:快速搭建+SEO优化+自适应模板全解析
如何用美橙互联一键搭建多站合一网站?
如何通过网站建站时间优化SEO与用户体验?
湖北网站制作公司有哪些,湖北清能集团官网?
如何通过云梦建站系统实现SEO快速优化?
孙琪峥织梦建站教程如何优化数据库安全?
*请认真填写需求信息,我们会在24小时内与您取得联系。