草庐IT

android - 在Android中逐渐绘制路径

coder 2023-12-19 原文

我有一个自定义 View ,我想围绕它绘制一条路径,例如边框。 但是边界应该像蛇一样逐渐变大。 目的是将其用作玩家在游戏中采取行动的计时器。

我使用 Path 类和方法 lineTo 和 addArc 来绘制边框。

timerPath = new Path();
timerPath.moveTo(getTranslationX() + width / 2, getTranslationY() + 3);
timerPath.lineTo(getTranslationX() + width - 10, getTranslationY() + 3);
timerPath.addArc(new RectF(getTranslationX() + width - 20, getTranslationY() + 3, 
getTranslationX() + width - 3, getTranslationY() + 20), -90f, 90f);
...
...
timerPath.lineTo(getTranslationX() + width / 2, getTranslationY() + 3);

timerPaint = new Paint();
timerPaint.setColor(Color.GREEN);
timerPaint.setStyle(Paint.Style.STROKE);
timerPaint.setStrokeWidth(6);

我在 onDraw 中使用 drawPath() 方法:

canvas.drawPath(timerPath, timerPaint);

看起来不错。

现在,我想知道是否有一种方法可以使用百分比(10%、11%、12% .. 等)来绘制路径的一部分。 然后我就可以为绘图设置动画了。

如果不可能,是否有另一种动画边框绘制方法? (用作计时器)

感谢您的帮助。

最佳答案

您可以使用 PathMeasure类来做到这一点。从您的路径创建一个 PathMeasure 对象,测量长度,然后使用 getSegment() 返回您可以绘制到 Canvas 的部分路径:

    float percentage = 50.0f; // initialize to your desired percentage

    PathMeasure measure = new PathMeasure(timerPath, false);
    float length = measure.getLength();
    Path partialPath = new Path();
    measure.getSegment(0.0f, (length * percentage) / 100.0f, partialPath, true);
    partialPath.rLineTo(0.0f, 0.0f); // workaround to display on hardware accelerated canvas as described in docs
    canvas.drawPath(partialPath, timerPaint);

关于android - 在Android中逐渐绘制路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29243610/

有关android - 在Android中逐渐绘制路径的更多相关文章

  1. ruby-on-rails - Rails - 使用/自定义 URL : '/dashboard' 指定根路径 - 2

    如何使此根路径转到:“/dashboard”而不仅仅是http://example.com?root:to=>'dashboard#index',:constraints=>lambda{|req|!req.session[:user_id].blank?} 最佳答案 您可以通过以下方式实现:root:to=>redirect('/dashboard')match'/dashboard',:to=>"dashboard#index",:constraints=>lambda{|req|!req.session[:user_id].b

  2. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  3. ruby - 如何根据长度将路径数组转换为嵌套数组或散列 - 2

    我需要根据字符串路径的长度将字符串路径数组转换为符号、哈希和数组的数组给定以下数组:array=["info","services","about/company","about/history/part1","about/history/part2"]我想生成以下输出,对不同级别进行分组,根据级别的结构混合使用符号和对象。产生以下输出:[:info,:services,about:[:company,history:[:part1,:part2]]]#altsyntax[:info,:services,{:about=>[:company,{:history=>[:part1,:pa

  4. ruby-on-rails - 如何播种图像的路径? - 2

    Organization和Image具有一对一的关系。Image有一个名为filename的列,它存储文件的路径。我在Assets管道中包含这样一个文件:app/assets/other/image.jpg。播种时如何包含此文件的路径?我已经在我的种子文件中尝试过:@organization=...@organization.image.create!(filename:File.open('app/assets/other/image.jpg'))#Ialsotried:#@organization.image.create!(filename:'app/assets/other/i

  5. Ruby 和指南针路径与 yeoman 项目 - 2

    我安装了ruby​​、yeoman,当我运行我的项目时,出现了这个错误:Warning:Running"compass:dist"(compass)taskWarning:YouneedtohaveRubyandCompassinstalledthistasktowork.Moreinfo:https://github.com/gruUse--forcetocontinue.Use--forcetocontinue.我有进入可变session目标的路径,但它不起作用。谁能帮帮我? 最佳答案 我必须运行这个:geminstallcom

  6. 对象的 Ruby 方法查找路径 - 2

    是否有内置的Ruby方法或众所周知的库可以返回对象的整个方法查找链?Ruby查看一系列令人困惑的类(如thisquestion中所讨论)以查找与消息对应的实例方法,如果没有类响应消息,则调用接收方的method_missing。我将以下代码放在一起,但我确信它遗漏了某些情况或者它是否100%正确。请指出任何缺陷并指导我找到一些更好的代码(如果存在)。defmethod_lookup_chain(obj,result=[obj.singleton_class])ifobj.instance_of?Classreturnadd_modules(result)ifresult.last==B

  7. ruby-on-rails - rails 中的路径解析 - 2

    我正在寻找这样解析路由路径的方法:ActionController::Routing.new("post_path").parse#=>{:controller=>"posts",:action=>"index"}应该和url_for相反更新我发现:Whatistheoppositeofurl_forinRails?Afunctionthattakesapathandgeneratestheinterpretedroute?ActionController::Routing::Routes.recognize_path("/posts")所以现在我需要将posts_path转换为“/p

  8. python3获取路径方法 - 2

    一:os.path.dirname(__file__)和os.getcwd()importospath=os.path.dirname(__file__)print("os.path.dirname(__file__)方法的结果{}".format(path))path=os.getcwd()print("os.getcwd()方法的结果{}".format(path))该脚本路径为:/User/xxx/Work1.在当前目录/User/xxx/Work运行程序结果:2.在上一级目录/User/xxx运行程序:3.在其他目录/User/xxx/Work/python运行程序:\在其他目录/Us

  9. ruby-on-rails - 更改回形针附件上集成测试的路径 - 2

    Paperclip的文档提到,您可以通过将以下代码放在test.rb环境文件中来更改测试的上传路径:Paperclip::Attachment.default_options[:path]="#{Rails.root}/spec/test_files/:class/:id_partition/:style.:extension"我遇到的问题是附件在模型中设置了路径,不会被覆盖:has_attached_file:photo,path:':attachment/:id/:style.:extension'当我运行测试时,文件会上传到/photo/文件夹而不是/spec/test_file

  10. ruby-on-rails - 自定义 Rails 的默认 Resourceful Route 路径 - 2

    长话短说我想更改Rails资源路由的默认行为,移动所有资源的create路径,使其成为/resources/new的POST而不是比/resources。设置让我们假设一个像这样指定的足智多谋的路线:#routes.rbresources:events实际生成的路由是:$rakeroutesPrefixVerbURIPatternController#ActioneventsGET/events(.:format)events#indexPOST/events(.:format)events#createnew_eventGET/events/new(.:format)events#n

随机推荐