Android-developed Ruby enables Android Automation screen adaptation

Source: Internet
Author: User

Background: I believe that for Android developers, screen fitting has always been a pain in your mind, and in order for Android developers to get out of the pain of screen adaptation, I decided to implement the 1.0 version of this Android Automation screen adaptation. Here's a look at the idea.

  1. First to values-w360 as the standard is 360DP, the corresponding device is 720x1080 commonly known as the 720P screen, that is, designers give the design is a 720P screen as a design diagram, then if the design is the size of the picture is 100px, So we need to use 100/2 = 50DP to get our standard value, and so on, to write all the dimensions in the app that need to fit into the Dimens.xml (standard value) of the values directory, Android programmers need to do so much.

  2. The next job is given to the script, first the script will values/dimens.xml copy to the desired screen resolution corresponding to the directory, for example values-w400 , and then match each size with a regular, calculate and then re-write the value after the adaptation, here to 360DP as the standard, 400DP for the target device to be adapted, Take 100px for example, then (400/360) x100 = 111, then the values-w400dp value of Dimens in this directory is 111DP.

Require ' fileutils ' base_dp=360 def adapte_dimens (Dimens_file,alpha) puts "adapter #{dimens_file}" file. Open Dimens_file, ' A + ' Do|fStr=""F.each_line{|linevalue=/(?<=>) \d+ (? = (DP|SP))/.Match(line). to_a[0] #匹配出尺寸的项目包括dp和spif value            value=value. to_f * alpha# calculate the value after adaptationStr+ = Line.gsub (/(?<=>) \d+ (? = (DP|SP))/,value. to_i.to_s) #将新值写入Else          Str+ = lineEnd} f.truncate(0) f.putsStr      End    EndValues_list = Dir.entries ("./"). Find_all{|f #通过修改这里的正则表达式, you can control which screen is specifically adapted for example, change a regular to aValues-w400dp/, then it's only going to fit. -DP screen is up/Values-w|Values-sw/.Match(f)} values_list. each  Do|v FILEUTILS.CP ("Values/dimens.xml", v) dest_file ="#{v}/dimens.xml"DP =/(\d{3})/.Match(v) Alpha = dp[0].to_f/base_dp# calculating the ratio coefficient adapte_dimens (dest_file,alpha)End

Summary: Well, the simple Ruby Automation adaptation script is done, and there's a lot of room for improvement, Now, every time you run the script, it overwrites the Dimens.xml file in the target directory, and if sometimes the auto-fit does not work well, occasionally you need to make some fine-tuning, but the value of fine-tuning after executing the script is lost, so you need to improve it. Interested students can achieve their own.


Android-developed Ruby enables Android Automation screen adaptation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.