320除以符号1.25简单的方式

当前访客身份:游客 [
对 Android 开发者有益的 40 条优化建议
英文原文:
Here’s a good way to get into Android programming:
&Find some code that does something similar to what you want to do
&Adjust it to try to make it do your thing
&Watch it fail
&Troubleshoot using StackOverflow
Repeat the process for the each feature you want to add. This method will keep you motivated and, because you keep iterating, you’ll also learn a lot without realising it. However, you’ll need to go a bit further when you release the app.
下面是开始Android编程的好方法:
&找一些与你想做事情类似的代码
&调整它,尝试让它做你像做的事情
&使用StackOverflow解决问题
对每个你像添加的特征重复上述过程。这种方法能够激励你,因为你在保持不断迭代,不经意中你学到了很多。然而,当你发布应用时你还要做一些更深入的事情。
To go from having some code that works pretty well to having an awesome app is a big jump, much bigger on Android than it is on iOS. When publishing on iOS your app jumps from one device – your phone – to a lot of devices that are pretty similar – same size screen, all with pretty good hardware, 95% running the same OS version. On Android, your app leaps into the void.
Your app must be able to handle whatever comes its way: in terms of screens, processor, custom OS, API level, and any other device specific quirks.
Here are my personal tips for making Android awesome.
从一些可正常工作的代码到一个可怕的应用程序是一个巨大的跳跃,相比iOS平台Android更是如此 。当在iOS上发布应用时只是在一个设备上跳跃–你的手机–对很多设备而言都很相似–同样大小的屏幕,都有很好的硬件,95%上运行相同版本的操作系统。在Android应用中你不会遇到这种情况。
你的程序必须能够处理一切:从屏幕,处理器,定制的操作系统,API层级以及任何其他的特定设备。
这是我对使Android应用舒服起来的个人建议。
Targeting screen size and resolution
There are currently over 100 different screen sizes on Android, and an even greater profusion of resolutions.&To make your app look good on different screen configurations there are two things you’ll need to make sure of:
&You have a good layout or structure for different screen sizes
Your images work well at different resolutions
These are independent tasks, you might have a super tablet layout, but your graphics could look horribly bobbly on it. We’ll look at them in turn.
目标屏幕尺寸及解决方法
在Android世界里目前有超过100种的不同屏幕尺寸,但解决方法也很丰富。为使你的应用适应不同的屏幕配置有两件事情你需要确定:
你对不同的屏幕尺寸有一个好的布局和结构
你的图像在不同分辨率下工作良好
这些都是独立的任务,你可能有一个超级的tablet布局,但上面的图形看起来很糟糕。我们会依次讨论他们。
Designing layouts for multiple screen sizes
Tip 1: ScrollViews and ListViews are an easy win. While there are a huge array of screen sizes, most of the variation – for phones at least – is in the screen height. For this reason ScrollViews and ListViews work well. They’re not appropriate for all views, however. For the dashboard tab in OpenSignal users should see everything all in one go, without scrolling. For the more advanced stats tab, scrolling is not such a bad thing. If you can design your layouts so they work well across all screens without using a scrollview, do so, but it’s an easy way of making sure it works on a lots of screens.
Dashboard style screens shouldn’t scroll
其它翻译版本:1(点击译者名切换)
为不同的屏幕而设计
1.通常会用ScrollView 和 ListView 轻松搞定
当我们有一系列不同尺寸的大屏手机时,它们之间最大的不同就是屏幕的高度。因此ScrollView和ListView通常可是有效的工作,虽然有时它们并不能完全覆盖全部屏幕。在OpenSignal中的Dashboard标签下我们可以看到所有部件一气呵成,不存在滑动、对于许多高级类型标签中,滑动展示并不见得是一件坏事。如果你能够为你所有的设计匹配到各种屏幕上面去,那么最好不过。否则,这两个控件会让你用最小的开发代价来保证你的软件在大多数屏幕上正常展示。
Dashboard style 的设计不需要scroll
Tip 2: Use the folder structures.&The resource folder structure is powerful in Android, it allows you to vary images/strings/layout files/dimensions/colours depending on the api level/language/screen size/screen resolution among other things. Here’s an example of some of the things you can do with it:
In values-small (above) I have a bools.xml file, this contains a line:
&resources&
&bool name=&small_screen&&true&/bool&
&/resources&
In the code I reference that like so:
if(getResources().getBoolean(R.bool.small_screen)){ getSupportActionBar().hide(); }
On small screened devices that boolean is found to be true and I hide the ActionBar to save space.&This is actually using the marvellous ActionBarSherlock, more on that later.&In values-sw360dp – so for screens wider than 360dp – I have
&resources&
&bool name=&small_screen&&false&/bool&
&/resources&
For large screens, the ActionBar is not hidden.
I don’t need to include the bools.xml file in values-sw400dp, because of the way that the OS searches for resources. For example for a device of width 600dp (600/160=3.75 inches, so this is what we generally call a 7″ tablet) the OS will look for a bools.xml in values-sw600dp and fail, look again in values-sw400dp and fail, then look in values-sw360dp and look no further.
2: 使用文件夹.&Android 的资源文件夹结构非常强大, 它允许开发者将不同的图片、字符串、布局文件、外形、颜色这些资源,在api、代码、屏幕尺寸等部分. 下面是一个例子,展示了在资源文件夹下你可以怎样做:
在 values-small 文件夹中存放了一个 bools.xml 文件, 文件中有如下几行代码:
&resources&
&bool name=&small_screen&&true&/bool&
&/resources&
在代码中我可这样引用:
if(getResources().getBoolean(R.bool.small_screen)){
getSupportActionBar().hide();
在小尺寸设备中boolean值将置为true 我此时将因此ActionBar来节省空间.&这段代码正是非凡的ActionBarSherlock 扩展库中的一部分,稍后再详细介绍. 在values-sw360dp文件夹中,存放对应屏幕宽于360dp的资源文件。与上面相同的位置,有如下代码
&resources&
&bool name=&small_screen&&false&/bool&
&/resources&
对于大屏幕而言,ActionBar就置为了显示状态.
我不需要将 bools.xml 文件放入 values-sw400dp文件夹中, 因为操作系统会自动按相应路径搜索. 例如一个设备宽 600dp (600/160=3.75 英寸, 这就是我们通常所说的7片装) 操作系统会在values-sw600dp 和其包含的的文件夹中搜索 bools.xml 文件, 若没有找到则搜索 values-sw400dp 文件夹,在搜索 values-sw360dp 文件夹以此类推.
Tip 3: 160dp = 1 inch. 320 dp = 2 inches. dp == dip
Tip 4: you can use these folder structure tricks for all resource types&For example you can use the system of appending folder names for XML layouts e.g. layout-sw360dp can be used if you have a layout file you want to target to screen width 360dp. If you want to switch the layout between portrait and landscape you can go one step further:
layout-sw360dp-land layout-sw360dp-port
But wait, half your users will speak Arabic? You probably want to flip your layout right to left, try:
layout-sw360dp-land layout-sw360dp-port layout-sw360dp-land-ar layout-sw360dp-port-ar
The first two files will get served for all other languages, your -ar files just for Arabic.
Tip 5: Resource rules of thumb:
XXX // i.e. no append to the folder name: default – use this for Nexus One, Droid 2, S2 XXX-sw360dp // larger phones – Galaxy Nexus, S3, S4 XXX-sw600dp // 7″ tablets XXX-sw720dp // 10” tablets
For Kindle devices things differ, use: XXX-large-mdpi // kindle fire 7″ XXX-large-hdpi // kindle fire 7″ HD
建议3:160dp = 1英寸。320 dp = 2英寸。dp = dip
建议4:你可以用这些目录结构技巧来应付所有资源类型,比如你的XML布局用指定的系统目录名称
来解决这个问题,如:layout-sw360dp目录可以匹配目标宽是360dp的机器。如果你也要支持横竖屏布局切换的话,可以用如下目录:
layout-sw360dp-land
layout-sw360dp-port
别急,你有一半的用户是说阿拉伯语的?那就将布局名称改为下面的样子吧:
layout-sw360dp-land
layout-sw360dp-port
layout-sw360dp-land-ar
layout-sw360dp-port-ar
前两个可以适用于所有语言,-ar代表阿拉伯语。
建议5:资源规则简介:
XXX //例子:没有添加目录名:默认-适用于Nexus One,Droid 2,S2
XXX-sw360dp // 比较大的手机 – Galaxy Nexus, S3, S4
XXX-sw600dp // 7〃 平板
XXX-sw720dp // 10” 平板
在Kindle设备有些不同,如下:
XXX-large-mdpi // kindle fire 7〃
XXX-large-hdpi // kindle fire 7〃 HD
Tip 6: you don’t have to tailor all your layout files, you could instead tailor the dimens.xml files.&In the screenshot a few paragraphs up, you’ll notice that I have a lot of dimens.xml files in my values folders, that’s because I prefer to work with a single set of layout.xml files, within each layout file I have code like:
&ImageView android:layout_centerHorizontal=&true& android:layout_marginTop=&@dimen/small_margin& android:layout_width=&@dimen/dashBoardWidth& android:layout_height=&@dimen/dashBoardHeight& android:id=&@+id/dashboard&/&
Where e.g. small_margin is defined in a dimens.xml file:
&resources&
&dimen name=&small_margin&&4dp&/dimen&
&/resources&
That 4dp varies between all the dimens files. I have an Excel file that creates all these different dimension definitions based on scaling up by a certain factor. You might ask: why not just let the Android OS handle all the scaling? Wy not, instead of hardcoding all values, just use one values folder and one layout folder? That would work, everything would get scaled if it was properly set up, but some elements look stupid scaled.
建议6:如果你不想裁剪所有的布局文件,你可以用dimens.xml文件。你要是留心我上面的文章,你就会注意到在我的values目录里有很多dimens.xml,这样是因为我更喜欢在一个layout.xml里设置值,在每一个布局文件里我喜欢这样做:
&ImageView
android:layout_centerHorizontal=&true&
android:layout_marginTop=&@dimen/small_margin&
android:layout_width=&@dimen/dashBoardWidth&
android:layout_height=&@dimen/dashBoardHeight&
android:id=&@+id/dashboard&/&
small_margin是在dimen.xml文件里定义的:
&resources&
&dimen name=&small_margin&&4dp&/dimen&
&/resources&
这个4dp变量在所有dimen文件里。我有个Excel文件,里面创建了所有不同的基于不同因素所需的尺寸定义。也许你会问:为什么不让android OS来处理所有尺寸的问题?为什么不呢,为什么不用一个values目录和一个布局目录来代替所有写死的数值呢?那当然是可以的,如果设置得当,都会得到所有的尺寸,但是对于有些元素看起来就不是那么好计算尺寸了。
Tip 7: Let whitespace scale more than graphics. Let graphics scale more than buttons.&Buttons, checkboxes, toggles look stupid when scaled up. A button that’s 100dip (0.63″) on a phone does not want to become 200dip (1.25″) on a tablet of twice the screen width. Just because the screens are bigger, it does not mean that tablets are used by giants. Instead, let the space between buttons increase while your shiny dials and images expand.
Tip 8:&Use the GraphicalLayout tool for fast previews.&GraphicalLayout is the WYSIWG editor for XML files. I prefer to write all my elements directly – instead of dragging and dropping – but after adding some elements, test out your layout on various screen sizes by toggling the GraphicalLayout selecting from the dropdown.
Lots of options, use them.
Scaling Images&
建议7:让空白空间大于图像空间。让图像空间大于按钮的大小。如果将按钮,多选框,切换控件放大后是很丑陋的。一个100dip(0.63&)大小的按钮是不想在平板上显示为原来两倍宽度200dip(1.25&)的.原因是屏幕变大了,这不是说平板是给巨人用的。我们可以这样做,在按钮增加的空间和图片扩展的空间里添加空白。
建议8:用GraphicalLayout工具快速预览。GraphicalLayout是WYSIWG XML编辑器。我喜欢直接编写元素-而不是拖,丢弃的可见编程方式,但在添加一些元素之后,可以在GraphicalLayout的下拉选择菜单里选择不同屏幕尺寸进行测试。
这里有很多选项供你选择。
Tip 9:&Don’t scale all your images.&Getting the layout files to adjust to different screen-sizes is just half the battle, the elements themselves (e.g. images) must work when blown up onto high-res screens. The conceptually simplest way of doing this is to produce a whole host of images and pop them into a matching plethora of drawable folders:
drawable-sw600dp-ldpi drawable-sw600dp-mdpi drawable-sw600dp-hdpi drawable-sw600dp-xhdpi drawable-sw600dp-xxhdpi
… And the same for other screen widths.
Don’t do this. You will go insane in the membrane. Having drawble-ldpi, drawable-hdpi, etc. folders might be worthwhile, but you don’t necessarily need to have all of them.
建议9:不要把所有的图片都缩放了。用布局文件来适应不同屏幕尺寸的方法只是成功的一半,布局里的元素(如:图片)也要能在高分辨率的屏幕下良好工作。在概念上比较简单的方式就是创建一套完整的图片目录并将它们与很多drawable目录匹配起来。
drawable-sw600dp-ldpi
drawable-sw600dp-mdpi
drawable-sw600dp-hdpi
drawable-sw600dp-xhdpi
drawable-sw600dp-xxhdpi
...其它的类似。
不要这样做:
你不要太尽信书了。
一般来说有drawble-ldpi, drawable-hdpi等目录就足够了,不需要将所有的情况都加上。
Tip 10:&Avoid bitmaps (jpg, png).&For some images – such as icons – bitmaps might be the best option, as they are simple to include. But where possible avoid them, you can save a lot of space and achieve much sharper results by using different methods.
Tip 11:&Use XML Drawables.&Wherever you can use XML drawables instead of bitmaps. XML drawables won’t let you do everything, but I was surprised by how flexible they are.
have a full overview, but here’re some tasters:
&shape xmlns:android=&/apk/res/android& android:shape=&rectangle& &
&corners android:bottomRightRadius=&14dp& android:bottomLeftRadius=&14dp& android:topLeftRadius=&14dp& android:topRightRadius=&14dp&/&
&gradient android:startColor=&@color/off_white& android:endColor=&@color/pale_yellow& android:angle=&270& android:type=&linear&/&
&stroke android:width=&4dp& android:color=&@color/osm_darkerblue&/&
This defines a rectangle with rounded corners, a border (darker blue) and a gradient. In layout files you can set it whatever width you like and it will look crisp on any screen. Ideal for buttons.
建议10:避免使用位图(jpg,png)。对于一些图标来说,用位图是个不错的选择,因为它们使用简单。但是如果可以避免使用位图,你可以节省很多空间。但用不同的方法也可以达到很好的结果。
建议11:用XML绘图。位图都可以用XML绘图来代替的。XML绘图不是万能的,但是它的方便性还是使我感到惊讶。中有详细的介绍,这里有个简单的例子:
xmlns:android=&/apk/res/android&
android:shape=&rectangle& &
android:bottomRightRadius=&14dp&
android:bottomLeftRadius=&14dp&
android:topLeftRadius=&14dp&
android:topRightRadius=&14dp&/&
android:startColor=&@color/off_white&
android:endColor=&@color/pale_yellow&
android:angle=&270&
android:type=&linear&/&
android:width=&4dp&
android:color=&@color/osm_darkerblue&/&
&/shape& 这里是定义了一个圆角矩形,一个有渐变的边(深蓝)。你可以在布局文件的任何地方来引用,而且它可以适应于任何屏幕。用它可以做出理想的按钮。}

我要回帖

更多关于 一个数除以3余2 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信