编译Android Framework下的资源文件

1./frameworks/base/api   Android app开发所使用的API

current.txt文件下,是app开发所引用的包、类以及接口

2./frameworks/base/core  Frameworks的核心架构层

   2.1   /res/color

相关属性:

android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件

根据这些状态同样可以设置buttonselector效果。也可以设置selector改变button中的文字状态。

以下是配置button中的文字效果:
drawable/button_font.xml
<?xml version="1.0"encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
    <itemandroid:state_selected="true" android:color="#FFF"/>
    <itemandroid:state_focused="true" android:color="#FFF"/>
    <itemandroid:state_pressed="true" android:color="#FFF"/>
    <itemandroid:color="#000" />
</selector>
Button还可以实现更复杂的效果,例如渐变
drawable/button_color.xml
<?xml version="1.0"encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">      
<itemandroid:state_pressed="true">//定义当button处于pressed状态时的形态。 
            <shape>

             <gradient  android:startColor="#8600ff"/> 

               <stroke  android:width="2dp" android:color="#000000"/> 
                <cornersandroid:radius="5dp"/>  
                <paddingandroid:left="10dp"android:top="10dp" 
                      android:bottom="10dp"android:right="10dp"/>  
            </shape> 
</item> 
<itemandroid:state_focused="true">//定义当button获得 focus时的形态 
                        <shape> 
                                <gradientandroid:startColor="#eac100"/> 
                                <stroke android:width="2dp"android:color="#333333"    color="#ffffff"/> 
                                <corners android:radius="8dp"/>   
                                <padding android:left="10dp"android:top="10dp" 
                                            android:bottom="10dp"android:right="10dp"/>                         
                    </shape> 
  </item>
</selector> 
最后,需要在包含button的xml文件里添加两项。例如main.xml 文件,需要在<Button/>里加两项android:focusable="true"  android:background="@drawable/button_color"


     2.2   /res/menu

原来是特定版本用到的,当你的应用程序目标设为蜂巢平台(即 Android 3.0)时,你可以利用Action Bar部件提供的全部功能,将你的选项菜单项放在Action Bar的右上角,对用户来说使用更方便,控制该行为的主菜单项属性是android:showAsAction。

  这个属性可接受的值有:
  1、always:这个值会使菜单项一直显示在Action Bar上。
  2、ifRoom:如果有足够的空间,这个值会使菜单项显示在Action Bar上。
  3、never:这个值使菜单项永远都不出现在Action Bar上。
  4、withText:这个值使菜单项和它的图标,菜单文本一起显示。
 

Action Bar是什么?  

很长一段时间内,开发人员一直在为他们的Android应用程序设计功能标题栏,Android 3.0正式引入了Action Bar部件,规范了应用程序导航设计,一些新的Android设备,如平板电脑也抛弃了传统的导航功能,使用软件按钮取代了物理主屏、菜单、后退和搜索按钮。  

Action Bar的概念非常简单,如果你的应用程序有一个“选项”菜单,当用户点击“菜单”按钮时,出现一个活动项目菜单,你的应用程序可以利用Android 3.0中的Action Bar功能,它修改了应用程序标题栏,以一种易于使用的方式显示那些先前在“选项”菜单中的活动



2.3   res/values

越来越多互联网企业都在Android平台上部署其客户端,为了提升用户体验,这些客户端都做得布局合理而且美观.......Android的Style设计就是提升用户体验的关键之一。Android上的Style分为了两个方面:

  1. Theme是针对窗体级别的,改变窗体样式;
  2. Style是针对窗体元素级别的,改变指定控件或者Layout的样式

 Android系统的themes.xml和style.xml(位于\base\core\res\res\values\)包含了很多系统定义好的style,建议在里面挑个合适的,然后再继承修改。

至于控件的Style设计就范围大多了,看看Eclipse的Android控件属性编辑器[Properties]就大概知道有哪些条目,而Android内置的style.xml也只是定义每个控件的默认样式而已....不过控件的style不建议大改,耐看的style更能让用户长时间使用软件。另外,控件的Style在很多情况下都用到9.png,学习9.png就必须到\base\core\res\res\drawable-hdpi里面看看,里面有很多系统内置的9.png。

style和theme都是资源。你可以用android提供的一些默认的style和theme资源,你也可以自定义你自己的style和theme资源。
如何新建自定义的style和theme:
1.在res/values 目录下新建一个名叫style.xml的文件。增加一个<resources>根节点。
2.对每一个style和theme,给<style>element增加一个全局唯一的名字,也可以选择增加一个父类属性。在后边我们可以用这个名字来应用风格,而父类   属性标识了当前风格是继承于哪个风格。
3.在<style>元素内部,申明一个或者多个<item>,每一个<item>定义了一个名字属性,并且在元素内部定义了这个风格的值。
4.你可以应用在其他XML定义的资源。
Style:
下边是一个申明风格的实例:
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="SpecialText" parent="@style/Text">
     <item name="android:textSize">18sp</item>
     <item name="android:textColor">#008</item>
 </style>
</resources>
如上所示,你可以用<item>元素来为你的风格定义一组格式化的值。在Item当中的名字的属性可以是一个字符串,一个16进制数所表示的颜色或者是其他资源的引用。
注意在<style>元素中的父类属性。这个属性让你可以能够定义一个资源,当前风格可以从这个资源当中继承到值。你可以从任何包 含这个风格的资源当中继承此风格。通常上,你的资源应该一直直接或者间接地继承Android的标准风格资源。 这样的话,你就只需要定义你想改变的值。
在这个例子当中的EditText元素,演示了如何引用一个XML布局文件当中定义的风格:
<EditText id="@+id/text1"
    style="@style/SpecialText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello, World!" />
现在这个EditText组件的所表现出来的风格就为我们在上边的XML文件中所定义的那样。

Theme:

就像style一样,主题依然在<style>元素里边申明,也是以同样的方式引用。
不同的是你通过在Android Manifest中定义的<application>和<activity>元素将主题添加到整个程序或者某个 Activity,但是主题是不能应用在某一个单独的View里。
下边是申明主题的一个例子:
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="CustomTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="windowFrame">@drawable/screen_frame</item>
    <item name="windowBackground">@drawable/screen_background_white</item>
    <item name="panelForegroundColor">#FF000000</item>
    <item name="panelBackgroundColor">#FFFFFFFF</item>
    <item name="panelTextColor">?panelForegroundColor</item>
    <item name="panelTextSize">14</item>
    <item name="menuItemTextColor">?panelTextColor</item>
    <item name="menuItemTextSize">?panelTextSize</item>
 </style>
</resources>
注意我们用了@符号和?符号来应用资源。@符号表明了我们应用的资源是前边定义过的(或者在前一个项目中或者在Android 框架中)。问号?表明了我们引用的资源的值在当前的主题当中定义过。通过引用在<item>里边定义的名字可以做到( panelTextColor 用的颜色和 panelForegroundColor中定义的一样)。这中技巧只能用在XML资源当中。
设置主题(theme):

1.在manifest当中设置主题

1.为了在成用当中 所有的Activity当中使用主题,你可以打开A ndroidManifest.xml 文件, 编辑<application> 标签,让其包含android:theme属性,值是一个主题的名字,如下:
<application android:theme="@style/CustomTheme">
2.如果你只是想让你程序当中的 某个Activity拥有这个主题,那么你可以 编辑<activity>标签
Android中提供了几种内置的资源,有好几种主题你可以切换而不用自己写。比如你可以用对话框主题来让你的Activity看起来像一个对话框。在manifest中定义如下: <activity android:theme="@android:style/Theme.Dialog">(看前面链接有很多theme)
补充:
如果你喜欢一个主题,但是想做一些轻微的改变,你只需要将这个主题添加为父主题。比如我们修改Theme.Dialog主题。我们来继承Theme.Dialog来生成一个新的主题。
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
继承了Theme.Dialog后,我们可以按照我们的要求来调整主题。我们可以修改在Theme.Dialog中定义的每个item元素的值,然后我们在Android Manifest 文件中使用 CustomDialogTheme 而不是 Theme.Dialog 。

2.在程序当中设置主题

如果需要的话,你可 以在Activity当中通过使用方法setTheme()来加载一个主题。注意,如果你这么做的话,你应该初始化任何View之前设置主题。比如,在调 用setContentView(View) 和inflate(int, ViewGroup)方法前。这保证系统将当前主题应用在所有的UI界面。例子如下:
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 ...
 setTheme(android.R.style.Theme_Light);
 setContentView(R.layout.linear_layout_3);
}
如果你打算在程序代码中来加载主界面的主题,那么需要注意主题当中不能包括任何系统启动这个Activity所使用的动画,这些动画将在程序启动前显示。在很多情况下,如果你想将主题应用到你的主界面,在XML中定义似乎是一个更好的办法。

Shape

5月23日 西安 OSC 源创会开始报名啦,存储、虚拟机、Docker 等干货分享

本文章转接于:http://kofi1122.blog.51cto.com/2815761/521605

Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对shape有了大体的了解,稍作总结:

先看下面的代码:
        <shape>
            <!-- 实心 -->
            <solid android:color="#ff9d77"/>
            <!-- 渐变 -->
            <gradient
                android:startColor="#ff8c00"
                android:endColor="#FFFFFF"
                android:angle="270" />
            <!-- 描边 -->
            <stroke
                android:width="2dp"
                android:color="#dcdcdc" />
            <!-- 圆角 -->
            <corners
                android:radius="2dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>

solid:实心,就是填充的意思
android:color指定填充的颜色

gradient:渐变
android:startColor和android:endColor分别为起始和结束颜色,ndroid:angle是渐变角度,必须为45的整数倍
另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。

stroke:描边
android:width="2dp" 描边的宽度,android:color 描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp" 
android:dashGap="3dp"
其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。

corners:圆角
android:radius为角的弧度,值越大角越圆。
我们还可以把四个角设定成不同的角度,方法为:
<corners 
        android:topRightRadius="20dp"    右上角
        android:bottomLeftRadius="20dp"    右下角
        android:topLeftRadius="1dp"    左上角
        android:bottomRightRadius="0dp"    左下角
 />
这里有个地方需要注意,bottomLeftRadius是右下角,而不是左下角,这个有点郁闷,不过不影响使用,记得别搞错了就行。
还有网上看到有人说设置成0dp无效,不过我在测试中发现是可以的,我用的是2.2,可能修复了这个问题吧,如果无效的话那就只能设成1dp了。

padding:间隔
这个就不用多说了,XML布局文件中经常用到。


大体的就是这样,以下是一个使用的具体示例:用在Selector中作为Button的背景,分别定义了按钮的一般状态、获得焦点状态和按下时的状态,具体代码如下:

main.xml:
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TestShapeButton"
    android:background="@drawable/button_selector"
    />


button_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <!-- 渐变 -->
            <gradient
                android:startColor="#ff8c00"
                android:endColor="#FFFFFF"
                android:type="radial"
                android:gradientRadius="50" />
            <!-- 描边 -->
            <stroke
                android:width="2dp"
                android:color="#dcdcdc"
                android:dashWidth="5dp" 
                android:dashGap="3dp" />
            <!-- 圆角 -->
            <corners
                android:radius="2dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:startColor="#ffc2b7"
                android:endColor="#ffc2b7"
                android:angle="270" />
            <stroke
                android:width="2dp"
                android:color="#dcdcdc" />
            <corners
                android:radius="2dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>       
        <shape>
            
<solid android:color="#ff9d77"/>
            <stroke
                android:width="2dp"
                android:color="#fad3cf" />
            <corners 
                android:topRightRadius="5dp"
                android:bottomLeftRadius="5dp"
                android:topLeftRadius="0dp"
                android:bottomRightRadius="0dp"
            />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

   Android中,默认的控件显示效果往往不太美观。我们可以在drawable下建一个XML文件,里面放置shape标签以及其相关的属性。例如,可以用这样的方式设置一个LinearLayout的边框、填充颜色、圆角效果等等。

 

例如,在一下代码中(today_but_selector.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <!-- 圆角 -->
            <corners android:radius="5dp" />
            <solid android:color="#888800" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#333333" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="10dp"
                android:right="10dp" android:bottom="10dp" />
        </shape>
    </item>
</selector>
可以这样利用这个xml文件,例如Button定义为:
<Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="  今 天   "
            android:background="@drawable/today_but_selector" android:textColor="#ffffff" />

得到的按钮显示效果为:

(1)正常情况下:

QQ截图20111012103401

(2)按下的时候:

QQ截图20111012103423



编译android源码中的资源文件

1、将资源放入 frameworks/base/core/res/res/ 中的相应目录,假设要添加的资源是 drawable 类型,文件名为 test(后缀可能为xml或者png等等),则将文件放入 frameworks/base/core/res/res/drawable*/ 下。

2、仿照已有的同类型系统资源修改 framework/base/core/res/res/values/public.xml,public.xml中有两种类型的资源描述,一种是<java-symbol/>系统私有的资源(不对app公开),另一种是<public />公共资源(可以在app中访问)。

如果要添加的资源是私有的,则加上 <java-symbol type="drawable" name="test" />(不需要id),如果是公共的则先找到最后一个type为 drawable 的 public 项,4.1.2系统中最后一项为<public type="drawable" name="dialog_holo_light_frame" id="0x010800b3" />,然后添加一句 <public type="drawable" name="test" id="0x010800b4" />,即id为最后一个的id+1(为了避免 id 冲突)

3、在 frameworks/base/core/res/ 中执行mm,编译后生成framework-res.apk 并会更新com.android.internal.R,该 R 所对应的文件为 /out/target/common/R/com/android/internal/R.java,可以在该文件中查看自己的资源是否已经加进去了。(如果mm出错可以尝试先执行一下“touch 资源文件”命令)

4、如果添加的是公共资源,则还需要在源码目录下执行 make update-api 以更新 frameworks/base/api/current.txt 文件。

5、完成之后在源码根目录中 make 即可。

另外,如果要在系统中添加一个新的id,比如为myid,则上述第一步改为在 frameworks/base/core/res/res/values/ids.xml 中增加一列<item type="id" name="myid" />,之后的步骤都一样。


注:4.2源码中,将系统私有资源分离成了单独的文件 symbols.xml,公共资源仍然在 public.xml中。


2.3  /res/anim

Animations

一、Animations介绍

Animations是一个实现android UI界面动画效果的APIAnimations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。 

二、Animations的分类

Animations从总体上可以分为两大类:

1.Tweened Animations:该类Animations提供了旋转、移动、伸展和淡出等效果。Alpha——淡入淡出,Scale——缩放效果,Rotate——旋转,Translate——移动效果。

2.Frame-by-frame Animations:这一类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示。

 

三、Animations的使用方法(代码中使用)

Animations extends Object implements Cloneable 

 使用TweenedAnimations的步骤:

1.创建一个AnimationSet对象(Animation子类);

2.增加需要创建相应的Animation对象;

3.更加项目的需求,为Animation对象设置相应的数据;

4.Animatin对象添加到AnimationSet对象当中;

5.使用控件对象开始执行AnimationSet


  Tweened Animations的分类
  1、Alpha:淡入淡出效果
  2、Scale:缩放效果
  3、Rotate:旋转效果
  4、Translate:移动效果

 

Animation的四个子类:
  AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation

四、具体实现

1main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

         <Button

            android:id="@+id/rotateButton"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="旋转" />

         <Button

            android:id="@+id/scaleButton"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="缩放" />

         <Button

            android:id="@+id/alphaButton"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="淡入淡出" />

         <Button

            android:id="@+id/translateButton"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="移动" />

    </LinearLayout>

     <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:orientation="vertical" >

         <ImageView

            android:id="@+id/image"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerInParent="true"

            android:src="@drawable/an" />

    </LinearLayout>

 </LinearLayout>

2.java文件

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

import android.view.animation.AlphaAnimation;

import android.view.animation.Animation;

importandroid.view.animation.AnimationSet;

importandroid.view.animation.RotateAnimation;

importandroid.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

importandroid.widget.Button;

importandroid.widget.ImageView;

public class Animation1Activity extends Activity {

    private Button rotateButton = null;

    private Button scaleButton = null;

    private Button alphaButton = null;

    private Button translateButton = null;

    private ImageView image = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        rotateButton = (Button)findViewById(R.id.rotateButton);

        scaleButton = (Button)findViewById(R.id.scaleButton);

        alphaButton = (Button)findViewById(R.id.alphaButton);

        translateButton = (Button)findViewById(R.id.translateButton);

        image = (ImageView)findViewById(R.id.image);

     

        rotateButton.setOnClickListener(newRotateButtonListener());

        scaleButton.setOnClickListener(newScaleButtonListener());

        alphaButton.setOnClickListener(newAlphaButtonListener());

        translateButton.setOnClickListener(

           new TranslateButtonListener());

    }

    class AlphaButtonListener implementsOnClickListener{

       public void onClick(View v) {

           //创建一个AnimationSet对象,参数为Boolean型,

           //true表示使用Animationinterpolatorfalse则是使用自己的

           AnimationSet animationSet = new AnimationSet(true);

           //创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明

           AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

           //设置动画执行的时间

           alphaAnimation.setDuration(500);

           //alphaAnimation对象添加到AnimationSet当中

           animationSet.addAnimation(alphaAnimation);

           //使用ImageViewstartAnimation方法执行动画

           image.startAnimation(animationSet);

       }

    }

    class RotateButtonListener implementsOnClickListener{

       public void onClick(View v) {

           AnimationSet animationSet = new AnimationSet(true);

           //参数1:从哪个旋转角度开始

           //参数2:转到什么角度

           //4个参数用于设置围绕着旋转的圆的圆心在哪里

           //参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标

           //参数4x轴的值,0.5f表明是以自身这个控件的一半长度为x

           //参数5:确定y轴坐标的类型

           //参数6y轴的值,0.5f表明是以自身这个控件的一半长度为x

           RotateAnimation rotateAnimation = new RotateAnimation(0, 360,

                  Animation.RELATIVE_TO_SELF,0.5f,

                  Animation.RELATIVE_TO_SELF,0.5f);

           rotateAnimation.setDuration(1000);

           animationSet.addAnimation(rotateAnimation);

           image.startAnimation(animationSet);

       }

    }

    class ScaleButtonListener implementsOnClickListener{

       public void onClick(View v) {

           AnimationSet animationSet = new AnimationSet(true);

           //参数1x轴的初始值

           //参数2x轴收缩后的值

           //参数3y轴的初始值

           //参数4y轴收缩后的值

           //参数5:确定x轴坐标的类型

           //参数6x轴的值,0.5f表明是以自身这个控件的一半长度为x

           //参数7:确定y轴坐标的类型

           //参数8y轴的值,0.5f表明是以自身这个控件的一半长度为x

           ScaleAnimation scaleAnimation = new ScaleAnimation(

                  0, 0.1f,0,0.1f,

                  Animation.RELATIVE_TO_SELF,0.5f,

                  Animation.RELATIVE_TO_SELF,0.5f);

           scaleAnimation.setDuration(1000);

           animationSet.addAnimation(scaleAnimation);

           image.startAnimation(animationSet);

       }

    }

    class TranslateButtonListener implementsOnClickListener{

       public void onClick(View v) {

           AnimationSet animationSet = new AnimationSet(true);

           //参数12x轴的开始位置

           //参数34y轴的开始位置

          

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值