├── Rakefile ├── .gitignore ├── lib ├── android_img_resizer │ ├── version.rb │ └── android_img_resizer.rb └── android_img_resizer.rb ├── Gemfile ├── android_img_resizer.gemspec ├── bin └── android_img_resizer └── README.md /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | Gemfile.lock 4 | pkg/* 5 | -------------------------------------------------------------------------------- /lib/android_img_resizer/version.rb: -------------------------------------------------------------------------------- 1 | module AndroidImgResizer 2 | VERSION = "1.0.0" 3 | end 4 | -------------------------------------------------------------------------------- /lib/android_img_resizer.rb: -------------------------------------------------------------------------------- 1 | require 'android_img_resizer/version' 2 | require 'android_img_resizer/android_img_resizer' 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | # Specify your gem's dependencies in android_img_resizer.gemspec 4 | gemspec 5 | 6 | gem 'rmagick', '~>2.13.1' 7 | -------------------------------------------------------------------------------- /android_img_resizer.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "android_img_resizer/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "android_img_resizer" 7 | s.version = AndroidImgResizer::VERSION 8 | s.authors = ["Hélder Vasconcels"] 9 | s.email = ["heldervasc@bearstouch.com"] 10 | s.homepage = "http://www.bearstouch.com" 11 | s.summary = %q{Tool to resize Android Resource images } 12 | s.description = %q{Tool to resize Android Resource images} 13 | 14 | s.rubyforge_project = "android_img_resizer" 15 | 16 | s.files = ['lib/android_img_resizer.rb','lib/android_img_resizer/version.rb','lib/android_img_resizer/android_img_resizer.rb'] 17 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 18 | s.executables = ['android_img_resizer'] 19 | s.require_paths = ["lib"] 20 | # specify any dependencies here; for example: 21 | # s.add_development_dependency "rspec" 22 | # s.add_runtime_dependency "rest-client" 23 | s.add_runtime_dependency "rmagick","~>2.13.1" 24 | end 25 | -------------------------------------------------------------------------------- /bin/android_img_resizer: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'android_img_resizer' 3 | 4 | DPI_SUPPORTED = ["xxxhdpi","xxhdpi","xhdpi","hdpi"] 5 | 6 | puts "----------------------------------------------------------------------" 7 | puts "Android Resource Image Resizer "+AndroidImgResizer::VERSION 8 | puts "----------------------------------------------------------------------" 9 | 10 | def print_sintax() 11 | puts "In the Android Project directory (PATH that has a AndroidManifest.xml) file please type: \n\n" 12 | puts "android_img_resizer list : To list Resource Image Files \n\n" 13 | puts "android_img_resizer img : To Resize a Resource Image File : \n\n" 14 | puts "android_img_resizer all : To Resize all Resource Image files : \n\n" 15 | puts "baseline_dpi := [xxxhdpi|xxhdpi|xhdpi|hdpi]\n\n" 16 | puts "Note: by default Android Image resizer will use xxhdpi as baseline image file \n\n" 17 | end 18 | 19 | if ARGV[0] == "list" 20 | if DPI_SUPPORTED.include? ARGV[1] 21 | AndroidImgResizer.list_images(ARGV[1]) 22 | else 23 | AndroidImgResizer.list_images 24 | end 25 | 26 | elsif ARGV[0] == "img" 27 | if ARGV[1] != "" 28 | if DPI_SUPPORTED.include?(ARGV[2]) 29 | AndroidImgResizer.resize_image(ARGV[1],ARGV[2]) 30 | else 31 | AndroidImgResizer.resize_image(ARGV[1]) 32 | end 33 | else 34 | print_sintax() 35 | end 36 | elsif ARGV[0] == "all" 37 | if DPI_SUPPORTED.include?(ARGV[1]) 38 | AndroidImgResizer.resize_all_images(ARGV[1]) 39 | else 40 | AndroidImgResizer.resize_all_images 41 | end 42 | else 43 | print_sintax() 44 | end 45 | 46 | puts "Exiting. Thanks" 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## What is Android Image Resizer 2 | 3 | Android Image Resizer is a tool to resize android project image files(Drawables) with the proper sizes. 4 | 5 | For example a image in res/drawable/xxhdpi with 300x300px as the base file will be resized to : 6 | 7 | * res/drawable/xhdpi with 200x200 px 8 | * res/drawable/hdpi with 150x150 px 9 | * res/drawable/mdpi with 100x100 px 10 | 11 | ## Install Instructions for Mac OS X 12 | 13 | 1. Install imagemagick 14 | 15 | ``` 16 | sudo brew install imagemagick 17 | ``` 18 | 19 | 2. Install the rmagick gem 20 | 21 | ``` 22 | gem install rmagick 23 | ``` 24 | 25 | 3. Install it with [RubyGems](https://rubygems.org/) 26 | 27 | ``` 28 | sudo gem install android_img_resizer 29 | ``` 30 | 31 | ## How To use it 32 | 33 | In the Android Project directory (PATH that has a AndroidManifest.xml) file please type: 34 | 35 | ``` 36 | cd 37 | ``` 38 | 39 | ### To list all drawable resource imagefiles 40 | 41 | ``` 42 | android_img_resizer list [xxxhdpi|xxhdpi|xhdpi|hdpi] 43 | ``` 44 | 45 | ### To Resize a resource image File 46 | 47 | ``` 48 | android_img_resizer img [xxxhdpi|xxhdpi|xhdpi|hdpi] 49 | ``` 50 | 51 | ### To Resize all resource image files 52 | 53 | ``` 54 | android_img_resizer all [xxxhdpi|xxhdpi|xhdpi|hdpi] 55 | ``` 56 | 57 | Note: by default Android Image resizer will use xxhdpi as base image file. 58 | 59 | ## License and copyright ## 60 | 61 | Permission is hereby granted, free of charge, to any person obtaining a copy 62 | of this software and associated documentation files (the "Software"), to 63 | deal in the Software without restriction, including without limitation the 64 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 65 | sell copies of the Software, and to permit persons to whom the Software is 66 | furnished to do so, subject to the following conditions: 67 | 68 | The above copyright notice and this permission notice shall be included in 69 | all copies or substantial portions of the Software. 70 | 71 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 72 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 73 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 74 | THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 75 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 76 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 77 | 78 | ## How can i help ? 79 | 80 | Contributions are welcome ! The project is mainly missing documentation and examples... 81 | 82 | -------------------------------------------------------------------------------- /lib/android_img_resizer/android_img_resizer.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'RMagick' 3 | 4 | module AndroidImgResizer 5 | 6 | def self.verify_android_project 7 | files=["AndroidManifest.xml"] 8 | directories=["res","res/drawable-hdpi","res/drawable-xhdpi","res/drawable-mdpi","src"] 9 | 10 | files.each do |file| 11 | if !File::exists?(file) 12 | puts "Error: This is not a Android project Directory\n" 13 | puts "Error: File not exists #{file} \n" 14 | return false 15 | end 16 | end 17 | 18 | directories.each do |directory| 19 | if !File::directory?(directory) 20 | puts "Error: This is not a Android project Directory\n" 21 | puts "Error: Directory not exists #{directory} \n" 22 | return false 23 | end 24 | end 25 | return true 26 | end 27 | 28 | def self.list_images(base_dpi="xxhdpi") 29 | return if !self.verify_android_project 30 | Dir::chdir("res/drawable-"+base_dpi+"/") 31 | files=Dir["*.{png,jpg,gif,bitmap}"] 32 | files.each do |entry| 33 | puts "Drawable File = #{entry}" 34 | end 35 | end 36 | 37 | def self.resize_all_images(base_dpi="xxhdpi") 38 | return if !self.verify_android_project 39 | 40 | baseline_path = "res/drawable-"+base_dpi+"/" 41 | if !File::exists?(baseline_path) 42 | puts "Baseline path #{baseline_path} doesnt exist" 43 | exit(1) 44 | end 45 | 46 | Dir::chdir(baseline_path) 47 | files=Dir["*.{png,jpg,gif,bitmap}"] 48 | Dir::chdir("../../") 49 | files.each do |entry| 50 | resize_image(entry,base_dpi) 51 | end 52 | end 53 | 54 | def self.scale(imageFile,rimg,factor,tag) 55 | xsize=rimg.columns*factor 56 | ysize=rimg.rows*factor 57 | puts "Image = #{imageFile} #{tag} = #{xsize.to_i}x#{ysize.to_i}" 58 | hdpi = rimg.scale(xsize.to_i,ysize.to_i) 59 | hdpi.write "res/drawable-#{tag}/"+imageFile 60 | end 61 | 62 | def self.resize_image(imageFile,base_dpi="xxhdpi") 63 | return if !self.verify_android_project 64 | 65 | full_path = "res/drawable-#{base_dpi}/#{imageFile}" 66 | 67 | if !File::exists?(full_path) 68 | puts "Image #{full_path} doesnt exist" 69 | exit(1) 70 | end 71 | 72 | rimg = Magick::Image.read(full_path).first 73 | puts "Resizing Image = #{imageFile} with size = #{rimg.columns}x#{rimg.rows} from #{base_dpi}" 74 | 75 | # Scale Down images in res/drawable-xxxhdpi to the lower dpis 76 | if base_dpi == 'xxxhdpi' 77 | self.scale_xxxhdpi(imageFile,rimg) 78 | # Scale Down images in res/drawable-xxhdpi to the lower dpis 79 | elsif base_dpi == 'xxhdpi' 80 | self.scale_xxhdpi(imageFile,rimg) 81 | # Scale Down images in res/drawable-xhdpi to the lower dpis 82 | elsif base_dpi == 'xhdpi' 83 | self.scale_xhdpi(imageFile,rimg) 84 | # Scale Down images in res/drawable-hdpi to the lower dpis 85 | else 86 | self.scale_hdpi(imageFile,rimg) 87 | end 88 | 89 | end 90 | 91 | def self.scale_xxxhdpi(imageFile,rimg) 92 | # xxdpi version 3/4 - 3.0x mdpi 93 | self.scale(imageFile,rimg,0.75,"xxhdpi") 94 | # xxdpi version 2/4 - 2.0x mdpi 95 | self.scale(imageFile,rimg,0.5,"xhdpi") 96 | # xxdpi version 1.5/4 - 1.5x mdpi 97 | self.scale(imageFile,rimg,0.375,"hdpi") 98 | # xxdpi version 1/4 - 1.0x mdpi 99 | self.scale(imageFile,rimg,0.25,"mdpi") 100 | end 101 | 102 | def self.scale_xxhdpi(imageFile,rimg) 103 | # xdpi version 2/3 - 2.0x mdpi 104 | self.scale(imageFile,rimg,2.0/3,"xhdpi") 105 | # hdpi version 0.5 - 1.5x mdpi 106 | self.scale(imageFile,rimg,0.5,"hdpi") 107 | # hdpi version 1/3 - 1.0x mdpi 108 | self.scale(imageFile,rimg,1.0/3,"mdpi") 109 | end 110 | 111 | def self.scale_xhdpi(imageFile,rimg) 112 | # xdpi version 0.75 - 1.5x mdpi 113 | self.scale(imageFile,rimg,0.75,"hdpi") 114 | # xdpi version 0.5 - 1.0x mdpi 115 | self.scale(imageFile,rimg,0.5,"mdpi") 116 | end 117 | 118 | def self.scale_hdpi(imageFile,rimg) 119 | # xdpi version 2/3 - 1.5x mdpi 120 | self.scale(imageFile,rimg,2.0/3,"mdpi") 121 | end 122 | 123 | def self.clean_files(imgtoclean) 124 | if File::exists?("res/drawable-xxhdpi/"+imgtoclean) 125 | File.delete("res/drawable-xxhdpi/"+imgtoclean) 126 | end 127 | if File::exists?("res/drawable-xhdpi/"+imgtoclean) 128 | File.delete("res/drawable-xhdpi/"+imgtoclean) 129 | end 130 | if File::exists?("res/drawable-hdpi/"+imgtoclean) 131 | File.delete("res/drawable-hdpi/"+imgtoclean) 132 | end 133 | if File::exists?("res/drawable-mdpi/"+imgtoclean) 134 | File.delete("res/drawable-mdpi/"+imgtoclean) 135 | end 136 | if File::exists?("res/drawable-ldpi/"+imgtoclean) 137 | File.delete("res/drawable-ldpi/"+imgtoclean) 138 | end 139 | end 140 | 141 | end 142 | --------------------------------------------------------------------------------