Skip to content

Instantly share code, notes, and snippets.

@s-kovacevic
Last active August 29, 2015 14:20
Show Gist options
  • Save s-kovacevic/29b5c1e343b1d0599fbc to your computer and use it in GitHub Desktop.
Save s-kovacevic/29b5c1e343b1d0599fbc to your computer and use it in GitHub Desktop.
Android dp to pixels converter
#!/bin/bash
# Make sure you use bash <script>.sh for this to work
#
# In dpi
#ldpi=120
#mdpi=160
#hdpi=240
#xhdpi=320
#xxhdpi=480
#xxxhdpi=640
#
# Formula is px = dp * (dpi / 160)
clear
echo "~~~~~~~~~~~~~~~~~~~~~~"
echo "~~DP to PX converter~~"
echo "~~~~~~~~~~~~~~~~~~~~~~"
array_names=(" ldpi" " mdpi" " hdpi" " xhdpi" " xxhdpi" "xxxhdpi")
array_values=(120 160 240 320 480 640)
#Read user dp input
echo "Enter dp(s) you want to convert (integer(s) separated with space)"
read -a arr
#Loop through every dp value user entered and print results
for dp in ${arr[@]}; do
echo ""
echo "~~~~~~~~~~~~~~~~~~~~~~~"
echo "Converting $dp dp to px"
echo "~~~~~~~~~~~~~~~~~~~~~~~"
for ((i=0; i<6; i++)) ; do
solution=`echo "scale=5;$dp*(${array_values[$i]}/160)" | bc`
solution_int=`echo "$solution/1" | bc`
echo "${array_names[$i]}" : $solution_int "px"
done
done
read -r -p "Want to convert more? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
bash dpicalc.sh
else
clear
fi
# For desktop entry
#
# [Desktop Entry]
# Version=1.0
# Name=dp to px
# Comment=Android dpi to pixels calculator.
# Exec=bash INSERT_PATH/dpicalc.sh
# Icon=INSERT_PATH/dpicalc.png
# Terminal=true
# Type=Application
# Categories=Utility;Application;Development;
#
# Make sure you chmod +x the desktop file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment