转自:http://www.cnblogs.com/playing/archive/2011/05/17/2048287.html
1.使用include标签实现包含共享UI设计
(1)创建新的布局文件 title_layout.xml,里面包含共享内容的布局
(2)layout属性:其对应抽取出来的共享的布局内容
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android " android:orientation = " vertical " android:background = " @drawable/share_background " android:layout_width = " fill_parent " android:layout_height = " fill_parent " > <include layout="@layout/title_layout"></include> </ LinearLayout >
2.使用shapes实现渐变效果
(1)创建新的drawable的xml文件
< shape xmlns:android = " http://schemas.android.com/apk/res/android " android:shape ="rectangle" > < gradient android:startColor = " #FFFF0000 " android:endColor = " #80FF00FF " android:angle = " 270 " /> < padding android:left = " 50dp " android:top = " 20dp " android:right = " 7dp " android:bottom = " 7dp " /> < corners android:radius = " 8dp " /> </ shape >
android:shape="rectangle" 默认的也是长方形
corners表示是有半径
(还有很多其它参数)
(2)设置android:background属性
android:background = " @drawable/share_background "
3.灵活使用styles.xml
(1)在res/values目录下新建一个style.xml,增加<resource>根节点
<? 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 > < style name="button_style" > < item name = " android:textStyle " > bold </ item > < item name = " android:textColor " > #FFFFFFFF </ item > < item name = " android:layout_width " > 100 .0dip </ item ></style> </ resources >
(2)设置style属性
< EditText id = " @+id/text1 " style = " @style/SpecialText " android:layout_width = " fill_parent " android:layout_height = " wrap_content " android:text = " Hello, World! " />
现在这个EditText组件的所表现出来的风格就为我们在上边的XML文件中所定义的那样。
|
4.自定义按钮显示效果
(1)在drawable中创建新的xml文件--mybutton.xml文件。
< item android:state_window_focused = " false " android:drawable = " @color/transparent " /> <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. 注意这句话 --> < item android:state_focused = " true " android:state_enabled = " false " android:state_pressed = " true " android:drawable = " @drawable/selector_background_disabled " /> < item android:state_focused = " true " android:state_enabled = " false " android:drawable = " @drawable/lselector_background_disabled " /> < item android:state_focused = " true " android:state_pressed = " true " android:drawable = " @drawable/selector_background_transition " /> < item android:state_focused = " false " android:state_pressed = " true " android:drawable = " @drawable/selector_background_transition " /> < item android:state_focused = " true " android:drawable = " @drawable/selector_background_focus " /> </ selector >
(2)在构造的layout中引用这个xml
< ImageButton android:id = " @+id/ImageButton01 " android:layout_width = " wrap_content " android:layout_height = " wrap_content " android:background ="@drawable/mybutton" > </ ImageButton >
5.实现无失真图片拉伸(NinePatch图片处理)
使用draw9patch.bat工具创建 .9.png图片
6.attr.xml(自定义属性)
一般用在自定义view的时候的构造方法里面,用法:
一、在res/values文件下定义一个attrs.xml文件.代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
public MyView(Context context,AttributeSet attrs)
{
//使用自定义属性,就需要有这么一个构造函数
super(context,attrs);
mPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
int textColor = a.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);
//R.styleable.MyView_textColor是读取attrs中参数名,是以“样式名_参数名”的形式。
//第二个参数,是默认值,如果从xml中获取不到则使用默认值。
float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
mPaint.setTextSize(textSize);
mPaint.setColor(textColor);
//使用xml属性来设置代码参数。
a.recycle();
//TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性! (原文原话,不清除什么
//意思)
}
7.动画(tween动画和frame动画)
tween动画有四种(滤镜动画、旋转动画、缩放动画、位移动画),分别是:
首先是AlphaAnimation。
alpha_anim.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="2000" /> </set>不用解释了吧。
RotateAnimation
rotate_anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="500" /> </set>
ScaleAnimation
scale_anim.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:fillAfter="false" android:duration="500" /> </set>
TranslateAnimation
translate_anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="10" android:toXDelta="100" android:fromYDelta="10" android:toYDelta="100" />
那怎样调用这些布局动画呢?下面举例平移动画,其他的类似:
Animation translateAnimation = AnimationUtils.loadAnimation(this.getContext(), R.anim.translate_anim); this.startAnimation(translateAnimation);
其次是帧动画,xml如下:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" > <item android:drawable="@drawable/c1" android:duration="200"/> <item android:drawable="@drawable/c2" android:duration="200"/> <item android:drawable="@drawable/c3" android:duration="200"/> <item android:drawable="@drawable/c4" android:duration="200"/> <item android:drawable="@drawable/c5" android:duration="200"/> <item android:drawable="@drawable/c6" android:duration="200"/> </animation-list>
调用如下:
ImageView imageView = (ImageView) findViewById(R.id.imageView1); imageView.setBackgroundResource(R.anim.animation_list); AnimationDrawable draw = (AnimationDrawable) imageView.getBackground(); draw.start(); //draw.stop();
暂时总结这么多,资料都来自与网上,主要是整理下思路而已。