Android自定义view Path 的高级用法之搜索按钮动画
作者:WangRain1 时间:2023-12-23 06:53:28
关于Path之前写的也很多了,例如path绘制线,path绘制一阶,二阶和三阶贝塞尔路径,这些都是path的基本用法。今天我要带大家看的是Path 的高级用法,先上图,再吹。
效果大致是这样的。看着是不是挺好。话不多说,切入正题:
既然今天要谈Path的高级用法,那就先来讲一讲(Path -- 中文 )就是“路径”既然是路径,从我们面向对象的想法的话,我们就容易想到 路径 的长度,路径的某一点等。想到这里我们就引出今天 的主要 类--------PathMeasure,字面意思很容易理解--翻译成 路径测量。对这就是我们用来测量路径的类。既然是一个类,我们就要看他集体有哪些方法和属性。
1.构造方法:
一个是有参数,一个无参数。
无参数就很容易理解就是直接创建对象,那有参数呢?
PathMeasure(Path path, boolean forceClosed) ;
第一个参数 Path 咿 !这不就是我们的Path 了吗。对既然是测量类,那就是要确定需要测量的那个路径,那第二个参数呢?
第二个参数是用来确保 Path 闭合,如果设置为 true, 则不论之前Path是否闭合,都会自动闭合该 Path(如果Path可以闭合的话)。
2.方法:
这方法还挺多,都是干啥的呢?就找几个重要的,常用的讲讲:
第一个那就是 setPath(Path path, boolean forceClosed)
这方法一看就知道设置path 路径的,就是如果我们创建 PathMeasure,时用的是无参的构造方法时 ,这时候就要用此方法,告诉PathMeasure 当前需要测量的是哪一个path路径。(个人喜欢用无参,比较灵活)。
第二个那就是getLength(),很容易理解就是 获得 path 的总长度。
第三个:getSegment(float startD, float stopD, Path dst, boolean startWithMoveTo),这个方法非常重要,截取片段,截取的结果就是,我们的path路径。参数也很好理解。第一个是开始的距离,第二个是结束的距离(相对于起点==既然是起点,我们怎么找他们的起点呢,其实就是我们绘制图画笔开始的哪一点)。第三个参数就是返回的路径。第四个参数起始点是否使用 moveTo 用于保证截取的 Path 第一个点位置不变。
第四个:getPosTan(float distance, float[] pos, float[] tan),这个也是非常重要的,为什么呢?听我解释一下他的参数就知道了。第一个参数是距离(相对于绘制起点的路径距离),第二个参数是距离起点的坐标点,第三个参数返回的是正玄函数tan@角度。
对这些参数了解之后下面就来讲解我们今天的例子:
首先我们先来个简单的:例如
如图这样的分析过程:
1.首先绘制一个圆(蓝色的),从45度开始绘制,注意圆的起点。
2.同圆心绘制大圆(辅助圆)用来辅助绘制蓝色的线。
3.通过getPosTan(float distance, float[] pos, float[] tan)方法得到两个圆的终点的坐标。
4.利用lineTo(x,y)绘制线。就得到我们的搜索图形了。
5.接下来就是实时 截取片段。getSegment(float startD, float stopD, Path dst, boolean startWithMoveTo),截取从距离0到getlength();这样就产生了一个动态的效果了。
看代码:
关于画笔,canvas画布,valueanimator动画,这些请看之前的博客,都有详细讲解。
public class MySearch extends View {
Paint paint;
Path searchPath; //搜索的圆
Path ciclePath; //外圆
//获得宽高
int w;
int h;
//这是保存截取时,返回的坐标点
float serpos[];
float cicpos[];
//测量 path 的类
PathMeasure measure;
//动画产生的实时数据
float value;
public MySearch(Context context) {
this(context,null);
}
public MySearch(Context context, @Nullable AttributeSet attrs) {
this(context,attrs,0);
}
public MySearch(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获得屏幕的宽高
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
w = wm.getDefaultDisplay().getWidth();
h = wm.getDefaultDisplay().getHeight();
//初始化数据
serpos = new float[2];
cicpos = new float[2];
//画笔
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setDither(true);
paint.setStrokeWidth(6);
paint.setAntiAlias(true);
//初始化路径
initPath();
//初始化动画
initAnimator();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//移动画布的远点到屏幕中心
canvas.translate(w/2,h/5);
paint.setColor(Color.BLUE);
//这是截取方法返回的路径
Path dra = new Path();
//解除硬件加速,不然没效果
dra.reset();
dra.lineTo(0,0);
//设置当前需要测量的path
measure.setPath(searchPath,false);
//开始截取
boolean s = measure.getSegment(measure.getLength()*value,measure.getLength(),dra,true);
//绘制路径
canvas.drawPath(dra,paint);
}
/**
* 初始化路径
*/
public void initPath(){
paint.setColor(Color.BLUE);
//搜索的圆
searchPath = new Path();
RectF rectF = new RectF(-50,-50,50,50);
searchPath.addArc(rectF,45,359.9f);
//辅助圆
ciclePath = new Path();
RectF rectF1 = new RectF(-100,-100,100,100);
ciclePath.addArc(rectF1,45,359.9f);
//测量类
measure = new PathMeasure();
measure.setPath(searchPath,false);
//获取坐标
measure.getPosTan(0,serpos,null);
measure.setPath(ciclePath,false);
//获取坐标
measure.getPosTan(0,cicpos,null);
//根据两点坐标绘制线
searchPath.lineTo(cicpos[0],cicpos[1]);
}
/**
* 初始化动画
*/
public void initAnimator(){
ValueAnimator animator = ValueAnimator.ofFloat(0,1);
animator.setDuration(2600);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
value = (float) animation.getAnimatedValue();
postInvalidate();
}
});
animator.start();
}
}
第一个图大家学会了,去练习一下吧!
来源:http://blog.csdn.net/wangrain1/article/details/72898014
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
unity 文件流读取图片与www读取图片的区别介绍
![](https://img.aspxhome.com/file/2023/9/89769_0s.jpg)
使用adb命令向Android模拟器中导入通讯录联系人的方法
解决SpringMVC项目连接RabbitMQ出错的问题
![](https://img.aspxhome.com/file/2023/4/63034_0s.jpg)
springboot中使用redis并且执行调试lua脚本
![](https://img.aspxhome.com/file/2023/1/61031_0s.png)
C#图片压缩的实现方法
浅谈c#表达式树Expression简单类型比较demo
C#抓取网页数据 解析标题描述图片等信息 去除HTML标签
MyBatis-Plus实现多数据源的示例代码
![](https://img.aspxhome.com/file/2023/3/59383_0s.png)
Android应用动态修改主题的方法示例
Unicode、UTF-8 和 ISO8859-1区别解析
java实现饮料自助售货机
Kotlin扩展方法超详细介绍
![](https://img.aspxhome.com/file/2023/8/113338_0s.png)
springboot集成springsecurity 使用OAUTH2做权限管理的教程
![](https://img.aspxhome.com/file/2023/9/101939_0s.jpg)
Spring零基础入门WebFlux响应式编程
![](https://img.aspxhome.com/file/2023/7/119067_0s.png)
C++ 中String 替换指定字符串的实例详解
如何利用Jenkins + TFS为.Net Core实现持续集成/部署详解
![](https://img.aspxhome.com/file/2023/2/92962_0s.png)
C# Winform 分页功能的实现
![](https://img.aspxhome.com/file/2023/1/69241_0s.png)
java代理模式(静态代理、动态代理、cglib代理)
![](https://img.aspxhome.com/file/2023/7/103827_0s.png)
高吞吐、线程安全的LRU缓存详解
![](https://img.aspxhome.com/file/2023/1/104201_0s.png)
C#编程实现查看剪切板内容的方法
![](https://img.aspxhome.com/file/2023/7/113737_0s.jpg)