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是设置是否响应事件,指所有事件
根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。
以下是配置button中的文字效果:
drawable/button_font.xml
<?xml version="1.0"encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
</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状态时的形态。
<itemandroid:state_focused="true">//定义当button获得 focus时的形态
</selector>
最后,需要在包含button的xml文件里添加两项。例如main.xml 文件,需要在<Button/>里加两项android:focusable="true"
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分为了两个方面:
-
Theme是针对窗体级别的,改变窗体样式;
-
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。
<?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>
<EditText id="@+id/text1"
style="@style/SpecialText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
Theme:
<?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>
1.在manifest当中设置主题
<application android:theme="@style/CustomTheme">
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
2.在程序当中设置主题
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... setTheme(android.R.style.Theme_Light); setContentView(R.layout.linear_layout_3); }
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)正常情况下:
(2)按下的时候:
编译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" />,之后的步骤都一样。
2.3 /res/anim
Animations
一、Animations介绍
Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。
二、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
四、具体实现
1、main.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表示使用Animation的interpolator,false则是使用自己的
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//设置动画执行的时间
alphaAnimation.setDuration(500);
//将alphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
//使用ImageView的startAnimation方法执行动画
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相对于父控件的坐标
//参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数5:确定y轴坐标的类型
//参数6:y轴的值,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);
//参数1:x轴的初始值
//参数2:x轴收缩后的值
//参数3:y轴的初始值
//参数4:y轴收缩后的值
//参数5:确定x轴坐标的类型
//参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数7:确定y轴坐标的类型
//参数8:y轴的值,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);
//参数1~2:x轴的开始位置
//参数3~4:y轴的开始位置