1、使用style、color、string、dimen样式来分离xml布局文件,减少代码的重复使用,增加代码复用率,防止hardcode,下面是一个例子:

在定义layout时候,因为每个View或ViewGroup都必须要有layout_widthlayout_height,所以我们可以专门定义一个size_style.xml,里面的内容为:

<resources>
    <style name="wrap">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="match">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
    </style>

    <style name="matchWidth_wrapHeight">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="matchHeight_wrapWidth">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">match_parent</item>
    </style>

    <style name="matchWidth_0dpHeight">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">0dp</item>
    </style>

    <style name="_0dpWidth_matchHeight">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
    </style>

    <style name="_0dpWidth_0dpHeight">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">0dp</item>
    </style>

    <style name="wrapWidth_0dpHeight">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">0dp</item>
    </style>

    <style name="_0dpWidth_wrapHeight">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
</resources>

这样,我们在定义布局的时候便可以这样定义:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/match"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        style="@style/matchWidth_wrapHeight"
        android:text="@string/button" />

    <LinearLayout
        style="@style/matchWidth_wrapHeight"
        android:weightSum="3">

        <Button
            style="@style/_0dpWidth_wrapHeight"
            android:layout_weight="1"
            android:text="@string/button" />

        <Button
            style="@style/_0dpWidth_wrapHeight"
            android:layout_weight="1"
            android:text="@string/add" />

        <Button
            style="@style/_0dpWidth_wrapHeight"
            android:layout_weight="1"
            android:text="@string/delete" />
    </LinearLayout>
</LinearLayout>

如:



这样做的好处是代码更简洁,而且代码复用率高,假如还定义了其它的style(比如背景、字体大小、字体颜色、margin等),我们可以继承size_style并引入其它的样式,代码为:

<style name="buttonStyle" parent="matchWidth_wrapHeight">
        <item name="android:textColor">#ffffff</item>
        <item name="android:layout_margin">15dp</item>
        <item name="android:textSize">14sp</item>
        <item name="android:background">#673AB7</item>
</style>

如:


2、在视图有变化的ViewGroup中,比如在这个界面中有视图的添加或者删除,可以在ViewGroup中设置这个属性:

android:animateLayoutChanges="true"

这样,视图的添加和删除就有默认的动画效果了,使得界面变换更加平滑,如:


3、在ListView或者GridView内容为空的时候,可以设置个默认视图,如:

mListView.setEmptyView(View view);

4、当需要设置一个View的动画时候,可以通过View.animate()快捷得到ViewPropertyAnimator进而设置动画效果,如:

这里设置View在y轴平移了10个像素

mView.animate().translationY(10).setDuration(300).start();

当然还可以通过代码定义一个ObjectAnimator或者通过xml在animator目录下新建动画


5、对于简单的图形图片,可以使用矢量图片,关于矢量图片的更多介绍见w3c组织,矢量图形拥有很好的屏幕适配性,色彩分辨率非常高,便于更改,同时也能够减少内存的占用,而平常普通的图片资源都是位图资源,对内存的消耗很明显,所以可以用矢量图的情况下最好用矢量图


6、对于整个应用中需要用到的颜色和字体大小,可以单独定义两个style供整个app使用,如:

颜色:

<?xml version="1.0" encoding="utf-8">
<resources>
    <color name="primarycolor_dark">#0e5a83</color>
    <color name="primarycolor">#0680c3</color>
    <color name="primarycolor_light">#489dca</color>
    <color name="secondary_light">#999999</color>
    <color name="secondary">#565656</color>
    <color name="secondary_dark">#4b4b4b</color>
    <color name="secondary_extraDark">#231f20</color>
    <color name="highlight_one">#35791d</color>
    <color name="highlight_two">#ff5151</color>
    <color name="main_bg">#ecf0f3</color>
    <color name="button_pressed">#036194</color>
    <color name="button_default">#0680c3</color>
</resources>

字体:

<?xml version="1.0" encoding="utf-8">
<resources>
    <dimen name="text_mirco">12sp</dimen>
    <dimen name="text_mini">14sp</dimen>
    <dimen name="text_ultraSmall">16sp</dimen>
    <dimen name="text_moderate1">18sp</dimen>
    <dimen name="text_moderate2">20sp</dimen>
    <dimen name="text_moderate3">22sp</dimen>
    <dimen name="text_big">25sp</dimen>
</resources>

7、Android系统提供了自动根据设备显示能力使用最合适的资源的能力,基本所有的资源都支持通过一定规则来动态切换。即假如在sdk版本为21的情况下,那么系统则先会找是否有values-v21等文件夹如果有则用这个文件夹下的资源,如果没有则才用values中的资源,可以说values是默认的资源目录。比如在不同版本的sdk中,创建res/values-v21等目录让系统自动获取合适的资源,在不同大小的屏幕上可以通过提供一个res/layout-small/文件夹来为小尺寸的设备提供定制化的布局,通过提供一个res/layout-large/文件夹来为大屏幕手机提供定制化布局

Android性能优化之界面UI篇的更多相关文章

  1. 【Bugly干货】Android性能优化典范之多线程篇

    本文涉及的内容有:多线程并发的性能问题,介绍了 AsyncTask,HandlerThread,IntentService 与 ThreadPool 分别适合的使用场景以及各自的使用注意事项,这是一篇 ...

  2. Android 性能优化之减少UI过度绘制

    什么是过度绘制(OverDraw) 在多层次重叠的UI结构里面,如果不可见的UI也在做绘制的操作,会导致某些像素区域被绘制了多次.这样就会浪费大量的CPU以及GPU资源.过度绘制最直观的影响就是会导致 ...

  3. [Android 性能优化系列]内存之提升篇--应用应该怎样管理内存

    大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 原文地 ...

  4. 【转】Android性能优化之布局优化篇

     转自:http://blog.csdn.net/feiduclear_up/article/details/46670433 Android性能优化之布局优化篇 分类: andorid 开发2015 ...

  5. [Android 性能优化系列]降低你的界面布局层次结构的一部分

    大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 原文地 ...

  6. Android性能优化第(三)篇---MAT比Menmery Monitor更强大

    作者 LooperJing 2016.11.17 16:42* 字数 1687 阅读 1603评论 3喜欢 21 在Android性能优化第(一)篇---基本概念中讲了JAVA的四大引用,讲了一下GC ...

  7. (转)Android性能优化——工具篇

    Android性能优化是Android开发中经常遇见的一个问题,接下来将对Android性能优化方面的知识点做一个简单的梳理和总结,将从工具和代码两方面进行梳理.所谓工欲善其事必先利其器,本文首先来看 ...

  8. [Android 性能优化系列]内存之基础篇--Android怎样管理内存

    大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 原文地 ...

  9. 【腾讯Bugly干货分享】Android性能优化典范——第6季

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/580d91208d80e49771f0a07c 导语 这里是Android性能优 ...

随机推荐

  1. 详解EBS接口开发之应收INVOICE导入

    (一)应收INVOICE常用标准表简介 1.1   常用标准表 如下表中列出了与应收INVOICE导入相关的表和说明: 表名 说明 其他信息 RA_BATCH_SOURCES_ALL AR事务处理来源 ...

  2. RxJava操作符(06-错误处理)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51658235 本文出自:[openXu的博客] 目录: Catch Retry 源码下载 1 ...

  3. Asp.net 在刷新或提交页面后保持滚动条的位置

    网页内容在较长时,每次回传刷新页面或提交网页时都会定位到最顶端,非常不利于用户交互. 将Page.MaintainScrollPositionOnPostBack属性值设置为true即可实现刷新后保持 ...

  4. Android必知必会-使用okhttp的PUT方式上传文件

    注:如果移动端排版有问题,请看 简书版 (<-点击左边),希望CSDN能更好的支持移动端. 背景 公司的文件上传接口使用PUT协议,之前一直用的都是老项目中的上传类,现在项目中使用了okhttp ...

  5. java模拟链表

          java语言不存在指针,但是我们仍可以用相应的逻辑模拟链表的实现,下面这段代码就是我的一个小伙伴实现的: package com.brucezhang.test; public class ...

  6. Android首选项SharedPreference-android学习之旅(六)

    SharedPrefenence采用的键值对的方式来进行存储,采用内部存储的方式. 实例 public class MainActivity extends Activity { private Sh ...

  7. 【java线程系列】java线程系列之线程间的交互wait()/notify()/notifyAll()及生产者与消费者模型

    关于线程,博主写过java线程详解基本上把java线程的基础知识都讲解到位了,但是那还远远不够,多线程的存在就是为了让多个线程去协作来完成某一具体任务,比如生产者与消费者模型,因此了解线程间的协作是非 ...

  8. Cocos2D iOS之旅:如何写一个敲地鼠游戏(四):创建TexturePacker自动脚本

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  9. Oracle分页存储过程及PLSQL中的调用脚本

    撰写过程:网上搜集测试了好多的Oracle分页存储过程代码,经整理后终于通过测试,特分享给大家 测试步骤:1.运行创建包命令;2.运行创建存储过程命令;3.运行调用分页存储过程语句 测试环境:wind ...

  10. Android实现自定义的相机

    使用系统相机 android中使用系统相机是很方便的,单这仅仅是简单的使用而已,并不能获得什么特殊的效果. 要想让应用有相机的action,咱们就必须在清单文件中做一些声明,好让系统知道,如下 < ...