Android development: Ruby for Android automatic screen adaptation

Source: Internet
Author: User

Android development: Ruby for Android automatic screen adaptation

Background: I believe that screen adaptation has always been a pain point for Android Developers ~, In order to free Android Developers from the pain of screen adaptation, I decided to implement the Android automatic screen adaptation Version 1.0. Let's talk about the ideas below.

    First of all to values-w360 as the standard is 360dp, the corresponding device is 720 × 1080 commonly known as the 720P screen, that is to say, the designer to the design draft is 720P screen as the design drawing, if the size above the design drawing is 100/2 PX, we need to use = 50dp to get our standard value, and so on, write all dimensions to be adapted in the app to The dimens in the values directory. in xml (standard value), Android programmers need to do so much.

    The subsequent work is handed over to the script. First, the script willvalues/dimens.xmlCopy to the directory corresponding to the screen resolution to be adapted, for examplevalues-w400Then, use the regular expression to match each size, calculate the size, and re-write the value after adaptation. Here we use 360dp as the standard, and 400dp as the target device to adapt. Take 100px as an example, so (400/360) × 100 = 111, then invalues-w400dpThe project 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 | f str = "" f. each_line {| line value = /(? <=>) \ D + (? = (Dp | sp ))/. match (line ). to_a [0] # items matching the size include dp and sp if value = value. to_f * alpha # Calculate the adapted value str + = line. gsub (/(? <=>) \ D + (? = (Dp | sp)/, value. to_ I .to_s) # write the new value to else str + = line end} f. truncate (0) f. puts str end values_list = Dir. entries (". /"). find_all {| f # modify the regular expression here, you can control the specific adaptation to which screen, such as changing the regular expression to/values-w400dp/then only adapt to the screen of 400dp/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 # calculate the ratio coefficient adapte_dimens (dest_file, alpha) end

    Summary: A simple Ruby automatic adaptation script is completed. Of course, there are still many improvements to be made. Now, every script run will overwrite the dimens in the target directory. for xml files, if the automatic adaptation is ineffective sometimes, manual fine-tuning is required, but the fine-tuning value will be lost after the script is executed, so improvements are needed. If you are interested, you can implement it on your own.


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.