(一)Activity 页面切换的效果
Android 2.0 之后有了 overridePendingTransition() ,其中里面两个参 数,一个是前一个 activity 的退出两一个 activity 的进入,
Java 代码
1. @Override
public void onCreate(Bundle savedInstanceState) { 2. super.onCreate(savedInstanceState);
3.
4. setContentView(R.layout.SplashScreen);
5.
6. new Handler().postDelayed(new Runnable() { 7. @Override
8. public void run() {
9. Intent mainIntent = new Intent(SplashScreen.this, AndroidNews.class);
10. SplashScreen.this.startActivity(mainIntent);
11. SplashScreen.this.finish();
12.
13. overridePendingTransition(R.anim.mainfadein, 14. R.anim.splashfadeout);
15. }
16.}, 3000);
}
上面的代码只是闪屏的一部分。
Java 代码
1. getWindow (). setWindowAnimations ( int );
这可没有上个好但是也可以 。 实现淡入淡出的效果
整理的一些关于android的页面切换的效果:
Java 代码
1. overridePendingTransition(Android.R.anim.fade_in,android.R.anim .fade_out);
由左向右滑入的效果 Java 代码
1. overridePendingTransition(Android.R.anim.slide_in_left,android.
R.anim.slide_out_right);
实现 zoomin 和 zoomout,即类似 iphone 的进入和退出时的效果 Java 代码
1. overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
新建 zoomin.xml 文件 Xml 代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <set
3. xmlns:Android="http://schemas.android.com/apk/res/android"
4. Android:interpolator="@android:anim/decelerate_interpolator">
<scale Android:fromXScale="2.0" android:toXScale="1.0"
5. Android:fromYScale="2.0" android:toYScale="1.0"
6. Android:pivotX="50%p" android:pivotY="50%p"
7. Android:duration="@android:integer/config_mediumAnimTime" />
</set>
新建 zoomout.xml 文件 Xml 代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <set
3. xmlns:Android="http://schemas.android.com/apk/res/android"
4. Android:interpolator="@android:anim/decelerate_interpolator"
5. Android:zAdjustment="top">
6. <scale Android:fromXScale="1.0" android:toXScale=".5"
7. Android:fromYScale="1.0" android:toYScale=".5"
8. Android:pivotX="50%p" android:pivotY="50%p"
9. Android:duration="@android:integer/config_mediumAnimTime" />
10.<alpha Android:fromAlpha="1.0" android:toAlpha="0"
11.Android:duration="@android:integer/config_mediumAnimTime"/>
12.</set>
(二)android 菜单动画
先请注意,这里的菜单并不是按机器上的 MENU 出现在那种菜单,而是基于 Android SDK 提供的 android.view.animation.TranslateAnimation(extends android.view.animation.Animation)类实例后附加到一个 Layout 上使之产生的 有动画出现和隐藏效果的菜单。
原理:Layout(菜单)从屏幕内(挨着屏 幕边沿,其实并非一定,视需要的初态和末态而定)动态 的移动到屏幕外(在外面可以挨着边沿,也可以离远点,
这个无所谓了),这样就可以达到动态菜单的效果了。但 是由于 Animation 的一些奇怪特性(setFill**() 函数的作用效果,这个在我使 用的某几个 Animation 当中出现了没有想明白的效果),就暂不理会这个东西了,
所以使得我们还需要用上 XML 属性 android:visibility。当 Layout(菜单)显 示的时候,设置 android:visibility="visible",当 Layout(菜单)隐藏的时 候,设置 android:visibility="gone",这里 android:visibility 可以有 3 个 值, "visible"为可见, "invisible"为不可见但占空间, "gone"为不可见且不占 空间(所谓的占不占空间,这个可以自己写个 XML 来试试就明白了)。
Class TranslateAnimation 的使用:Animation 有两种定义方 法,一种是用 Java code,一种是用 XML,这里只介绍用 code 来定义(因为用 XML 来定义的那种我没用过。。嘿嘿。。)。多的不说,看代码。
这里是 TranslateAnimationMenu.java(我在里面还另加入了 ScaleAnimation 产生的动画,各位朋友可以照着 SDK 以及程序效果来理解):
package com.TranslateAnimation.Menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
public class TranslateAnimationMenu extends Activity { /** Called when the activity is first created. */
//TranslateAnimation showAction, hideAction;
Animation showAction, hideAction;
LinearLayout menu;
Button button;
boolean menuShowed;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
menu = (LinearLayout) findViewById(R.id.menu);
button = (Button) findViewById(R.id.button);
// 这里是 TranslateAnimation 动画 showAction = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
// 这里是 ScaleAnimation 动画 //showAction = new ScaleAnimation(
// 1.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, // Animation.RELATIVE_TO_SELF, 0.0f);
showAction.setDuration(500);
// 这里是 TranslateAnimation 动画 hideAction = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f);
// 这里是 ScaleAnimation 动画 //hideAction = new ScaleAnimation(
// 1.0f, 1.0f, 1.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
hideAction.setDuration(500);
menuShowed = false;
menu.setVisibility(View.GONE);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { // TODO Auto-generated method stub if (menuShowed) {
menuShowed = false;
menu.startAnimation(hideAction);
menu.setVisibility(View.GONE);
} else {
menuShowed = true;
menu.startAnimation(showAction);
menu.setVisibility(View.VISIBLE);
} }
});
} }
这里是 main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<LinearLayout android:id="@+id/menu"
android:layout_height="100px" android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:background="#ffffff">
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="I am a menu"
android:gravity="center" />
</LinearLayout>
<Button android:id="@+id/button" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Click to show/hide menu" />
</RelativeLayout>Android 基于 TranslateAnimation 的动画动态菜单
android 布局属性
文章分类:移动开发
第一类:属性值为 true 或 false
android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中 android:layout_alignParentBottom 贴紧父元素的下边缘 android:layout_alignParentLeft 贴紧父元素的左边缘 android:layout_alignParentRight 贴紧父元素的右边缘 android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找 不到的话就以父元素做参照物
第二类:属性值必须为 id 的引用名“@id/id-name”
android:layout_below 在某元素的下方 android:layout_above 在某元素的的上方 android:layout_toLeftOf 在某元素的左边 android:layout_toRightOf 在某元素的右边
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐 android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐 android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对 齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐 第三类:属性值为具体的像素值,如 30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
EditText 的 android:hint
设置 EditText 为空时输入框内的提示信息。
android:gravity
android:gravity 属性是对该 view 内容的限定.比如一个 button 上面的 text. 你可以设置该 text 在 view 的靠左,靠右等位置.以 button 为例,
android:gravity="right"则 button 上面的文字靠右 android:layout_gravity
android:layout_gravity 是用来设置该 view 相对与起父 view 的位置.比如一 个 button 在 linearlayout 里,你想把该 button 放在靠左、靠右等位置就可以 通过该属性设置.以 button 为例,android:layout_gravity="right"则 button 靠右
android:layout_alignParentRight
使当前控件的右端和父控件的右端对齐。这里属性值只能为 true 或 false,默 认 false。
android:scaleType:
android:scaleType 是控制图片如何 resized/moved 来匹对 ImageView 的 size。
ImageView.ScaleType / android:scaleType 值的意义区别:
CENTER /center 按图片的原来 size 居中显示,当图片长/宽超过 View 的长/
宽,则截取图片的居中部分显示
CENTER_CROP / centerCrop 按比例扩大图片的 size 居中显示,使得图片长(宽) 等于或大于 View 的长(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩 小或原来的 size 使得图片长/宽等于或小于 View 的长/宽
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到 View 的宽度,居中显示 FIT_END / fitEnd 把图片按比例扩大/缩小到 View 的宽度,显示在 View 的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到 View 的宽度,显示在 View 的上部分位置
FIT_XY / fitXY 把图片•不按比例扩大/缩小到 View 的大小显示 MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。
** 要注意一点,Drawable 文件夹里面的图片命名是不能大写的。
2010-10-28
android 翻页
:
之前看到不少翻页,不过人家没有分享出代码来,我也一直没有搞出来.
想了好久,实现原理并不是那么的难,怎么实现就比较难了.
当然像 3D 现实模拟分页的难度是比较大的.
平面的分页,简单点说就是用三张图片,模拟分页时可见区, 这里我弄了一个 View,里面有翻页的效果.
OnDraw 中;
最底一张是将要显示的,先画出来,
接着画原来那张,放在中间,因为这张图片要在翻页的过程中慢慢消失,一点一点 被拉出去,
最后一张就是最上面的,为什么要用这张图片呢?当页面被翻起后,一个角消失了, 就比如第二张不显示的部分,那这部分,就是第三张了,再覆盖在第二张上面,形 成一种看似翻书的.效果.
然后在第二张(假设从左边开始被翻起,左边先消失),左边缘再画出一条线,当然 这条线要粗点,然后设置颜色渐变,还要透明的,就有一种阴影的效果.
我开始一直在想,像这样的,不难实现啊,当然只限于矩形的,因为当时没有想到 如何处理第二张消失部分为一个三角形.
可以通过 Rect 来设置一个 Bitmap,的宽度.
Rect rect = new Rect(mWidth, 0, width, height);
canvas.drawBitmap(image2, null, rect, paint2);
mWidth 就是左边消失部分的宽度.通过不断改变这个值,然后再刷新 View 就可以 看到一种滚动的效果.第二张图片左边慢慢消失, width,height 为画面目的宽高.
然后可以添加一个第三张图片,也用同样的方法设置了它显示的宽,高, 通过上面 Rect 的处理,看到一般的效果.比较常见的是从一个角开始,然后慢慢 的卷起来,而不是像上面平行的,(上面只能看到平行的效果.)
这里就涉及到了一个角,就是三角形如何产生的问题,这个问题困扰了好久.今天 想到办法了.就是用 Path 去画多边形.一个矩形,减去一个多边形,就剩下一个三 角形了.
先讨论上面那种效果的处理方式:
首先是 View:的 OnDraw 方法.
Java 代码
1. width = getWidth();
2. height = getHeight();
3. //画最底下一张将要显示的图片
4. Rect rect = new Rect(0, 0, width, height);
5. canvas.drawBitmap(image1, null, rect, paint1);
6. //画上面卷起的那张.实现平行翻页效果.
7. rect = new Rect(mWidth, 0, width, height);
8. canvas.drawBitmap(image2, null, rect, paint2);
9. //当然之后再处理卷起边缘阴影,模糊等效果,这里省略了.还有图片 Image1,image2 自己准备了
10.然后就是手势,没有手势,翻页显得很呆板.
11.这个 View 实现 OnGestureListener,自然有一些方法要覆盖的.
12.其它方法随便了,有返回值的给它一个 True,主要是
13.public boolean onFling(MotionEvent e1, MotionEvent e2, fl oat velocityX, float velocityY)这个方法,比较有用.
14.myHandler.removeMessage(0);/../我觉得要先移除上一次翻页的动作, 然后会马上从上一次运行中停止,而立即向当前需要的方向变化.
15.if(e1.getX() - e2.getX() > 0){
16. turnLeft(5);//向左翻
17.}else if(e2.getX() - e1.getX() > 0){
18. turnRight(5);//向右翻 19.}
20.两个方法差不多,也可以合并,传入正负值.
21.delta = speed;参数就是上面的 5,作为变化速度.
22.myHandler.sendEmptyMessage(0);
23.普通的 View 要 Handler 来更新.之前试过了,以为在 View 直接 Invalidate 可以.
24.虽然没有提示非 UI 线程的问题,但是循环了多次只看到 OnDraw 执行一次 而以.
25.public void handleMessage(Message message){
26. invalidate();
27. mWidth += delta;//变化第二张图片的宽.
28. if(delta > 0){//向右滚动
29. if(mWidth < width){//当第二张图片的左侧空白超过了画布的宽 时停止
30. sendEmptyMessage(0);
31. }
32.}else{//向左滚动 33.if(mWidth > 0){
34. sendEmptyMessage(0);
35.}
36.}
37.}
38.
39.然后在 XML 里用这个 View,最后 Activity 里 SetContentView 就 OK 了 /
40.<com.me.page.PageView 41.android:id="@+id/pageView1"
42.android:layout_gravity="center"
43.android:layout_marginTop="5px"
44.android:layout_width="fill_parent"
45.android:layout_height="fill_parent"/>
46.
47.由于使用 XML,所以构造方法必须要有,带两个参数的.
48.public PageView(Context context, AttributeSet attrs){
49. super(context, attrs);
50. initView();
51.}
52.InitView:
53.private void initView(){
54. image1 = Bitmap.createBitmap(BitmapFactory.decodeResource (getResources(), R.drawable.aac));
55. image2 = Bitmap.createBitmap(BitmapFactory.decodeResource (getResources(), R.drawable.q1));
56. image3 = Bitmap.createBitmap(BitmapFactory.decodeResource (getResources(), R.drawable.q2));
57.
58. myHandler = new MyHandler();
59. gestureDetector = new GestureDetector(this);
60. mShader = new LinearGradient(10, 250, 250, 250, 61. new int[]{Color.RED, Color.GREEN, Color.BLUE}, 62. null, Shader.TileMode.MIRROR);
63. paint1 = new Paint();
64. paint1.setFlags(Paint.ANTI_ALIAS_FLAG); //去除插刺 65. paint2 = new Paint(paint1);
66. paint3 = new Paint(paint2);
67. paint3.setColor(0x45111111);
68. //paint.setShader(mShader);//其间颜色会有变化.
69. paint3.setStrokeWidth(12);
70. paint4 = new Paint(paint2);
71. paint4.setColor(0xff111111);
72. paint4.setShader(mShader);//其间颜色会有变化.
73. paint4.setStrokeWidth(12);
74. } 75.
76.代码就到这里了.关于三角形卷动翻页,代码没有写完整,(第三张图片还
没有弄,也没有阴影),先不写了,而且似乎也不止我这样一种写法的,网上
看到的翻页还有其它的,比如我见到一个,OnDraw 里得到画布的高,然后
一行一行描述,并逐行递减宽度,这样造成一个三角形,速度也不那么地慢.
还可接受.
77.
78.来些图片.
79.page-15.png 到 page-16.png,宽度越来越小了.
80.page-12.png 到 page-14.png 是三角形的,里面具体操作复杂一些,当前差 上面那张遮罩.以后再完善了.
81. android 中颜色对应的值 82.文章分类:移动开发
83. < ?xml version="1.0" encoding="utf-8" ?>
< resources>
< color name="white">#FFFFFF< /color>< !--白色 -->
< color name="ivory">#FFFFF0< /color>< !--象牙色 -->
< color name="lightyellow">#FFFFE0< /color>< !--亮黄色 -->
< color name="yellow">#FFFF00< /color>< !--黄色 -->
< color name="snow">#FFFAFA< /color>< !--雪白色 -->
< color name="floralwhite">#FFFAF0< /color>< !--花白色 -->
< color name="lemonchiffon">#FFFACD< /color>< !--柠檬绸色 -->
< color name="cornsilk">#FFF8DC< /color>< !--米绸色 -->
< color name="seashell">#FFF5EE< /color>< !--海贝色 -->
< color name="lavenderblush">#FFF0F5< /color>< !--淡紫红 -->
< color name="papayawhip">#FFEFD5< /color>< !--番木色 -->
< color name="blanchedalmond">#FFEBCD< /color>< !--白杏色 -->
< color name="mistyrose">#FFE4E1< /color>< !--浅玫瑰色 -->
< color name="bisque">#FFE4C4< /color>< !--桔黄色 -->
< color name="moccasin">#FFE4B5< /color>< !--鹿皮色 -->
< color name="navajowhite">#FFDEAD< /color>< !--纳瓦白 -->
< color name="peachpuff">#FFDAB9< /color>< !--桃色 -->
< color name="gold">#FFD700< /color>< !--金色 -->
< color name="pink">#FFC0CB< /color>< !--粉红色 -->
< color name="lightpink">#FFB6C1< /color>< !--亮粉红色 -->
< color name="orange">#FFA500< /color>< !--橙色 -->
< color name="lightsalmon">#FFA07A< /color>< !--亮肉色 -->
< color name="darkorange">#FF8C00< /color>< !--暗桔黄色 -->
< color name="coral">#FF7F50< /color>< !--珊瑚色 -->
< color name="hotpink">#FF69B4< /color>< !--热粉红色 -->
< color name="tomato">#FF6347< /color>< !--西红柿色 -->
< color name="orangered">#FF4500< /color>< !--红橙色 -->
< color name="deeppink">#FF1493< /color>< !--深粉红色 -->
< color name="fuchsia">#FF00FF< /color>< !--紫红色 -->
< color name="magenta">#FF00FF< /color>< !--红紫色 -->
< color name="red">#FF0000< /color>< !--红色 -->
< color name="oldlace">#FDF5E6< /color>< !--老花色 -->
< color name="lightgoldenrodyellow">#FAFAD2< /color>< !--亮 金黄色 -->
< color name="linen">#FAF0E6< /color>< !--亚麻色 -->
< color name="antiquewhite">#FAEBD7< /color>< !--古董白 -->
< color name="salmon">#FA8072< /color>< !--鲜肉色 -->
< color name="ghostwhite">#F8F8FF< /color>< !--幽灵白 -->
< color name="mintcream">#F5FFFA< /color>< !--薄荷色 -->
< color name="whitesmoke">#F5F5F5< /color>< !--烟白色 -->
< color name="beige">#F5F5DC< /color>< !--米色 -->
< color name="wheat">#F5DEB3< /color>< !--浅黄色 -->
< color name="sandybrown">#F4A460< /color>< !--沙褐色 -->
< color name="azure">#F0FFFF< /color>< !--天蓝色 -->
< color name="honeydew">#F0FFF0< /color>< !--蜜色 -->
< color name="aliceblue">#F0F8FF< /color>< !--艾利斯兰 -->
< color name="khaki">#F0E68C< /color>< !--黄褐色 -->
< color name="lightcoral">#F08080< /color>< !--亮珊瑚色 -->
< color name="palegoldenrod">#EEE8AA< /color>< !--苍麒麟色 -->
< color name="violet">#EE82EE< /color>< !--紫罗兰色 -->
< color name="darksalmon">#E9967A< /color>< !--暗肉色 -->
< color name="lavender">#E6E6FA< /color>< !--淡紫色 -->
< color name="lightcyan">#E0FFFF< /color>< !--亮青色 -->
< color name="burlywood">#DEB887< /color>< !--实木色 -->
< color name="plum">#DDA0DD< /color>< !--洋李色 -->
< color name="gainsboro">#DCDCDC< /color>< !--淡灰色 -->
< color name="crimson">#DC143C< /color>< !--暗深红色 -->
< color name="palevioletred">#DB7093< /color>< !--苍紫罗兰色 -->
< color name="goldenrod">#DAA520< /color>< !--金麒麟色 -->
< color name="orchid">#DA70D6< /color>< !--淡紫色 -->
< color name="thistle">#D8BFD8< /color>< !--蓟色 -->
< color name="lightgray">#D3D3D3< /color>< !--亮灰色 -->
< color name="lightgrey">#D3D3D3< /color>< !--亮灰色 -->
< color name="tan">#D2B48C< /color>< !--茶色 -->
< color name="chocolate">#D2691E< /color>< !--巧可力色 -->
< color name="peru">#CD853F< /color>< !--秘鲁色 -->
< color name="indianred">#CD5C5C< /color>< !--印第安红 -->
< color name="mediumvioletred">#C71585< /color>< !--中紫罗兰 色 -->
< color name="silver">#C0C0C0< /color>< !--银色 -->
< color name="darkkhaki">#BDB76B< /color>< !--暗黄褐色 < color name="rosybrown">#BC8F8F< /color>< !--褐玫瑰红 -->
< color name="mediumorchid">#BA55D3< /color>< !--中粉紫色 -->
< color name="darkgoldenrod">#B8860B< /color>< !--暗金黄色 -->
< color name="firebrick">#B22222< /color>< !--火砖色 -->
< color name="powderblue">#B0E0E6< /color>< !--粉蓝色 -->
< color name="lightsteelblue">#B0C4DE< /color>< !--亮钢兰色 -->
< color name="paleturquoise">#AFEEEE< /color>< !--苍宝石绿 -->
< color name="greenyellow">#ADFF2F< /color>< !--黄绿色 -->
< color name="lightblue">#ADD8E6< /color>< !--亮蓝色 -->
< color name="darkgray">#A9A9A9< /color>< !--暗灰色 -->
< color name="darkgrey">#A9A9A9< /color>< !--暗灰色 -->
< color name="brown">#A52A2A< /color>< !--褐色 -->
< color name="sienna">#A0522D< /color>< !--赭色 -->
< color name="darkorchid">#9932CC< /color>< !--暗紫色 -->
< color name="palegreen">#98FB98< /color>< !--苍绿色 -->
< color name="darkviolet">#9400D3< /color>< !--暗紫罗兰色 -->
< color name="mediumpurple">#9370DB< /color>< !--中紫色 -->
< color name="lightgreen">#90EE90< /color>< !--亮绿色 -->
< color name="darkseagreen">#8FBC8F< /color>< !--暗海兰色 -->
< color name="saddlebrown">#8B4513< /color>< !--重褐色 -->
< color name="darkmagenta">#8B008B< /color>< !--暗洋红 -->
< color name="darkred">#8B0000< /color>< !--暗红色 -->
< color name="blueviolet">#8A2BE2< /color>< !--紫罗兰蓝色 < color name="lightskyblue">#87CEFA< /color>< !--亮天蓝色 -->
< color name="skyblue">#87CEEB< /color>< !--天蓝色 -->
< color name="gray">#808080< /color>< !--灰色 -->
< color name="grey">#808080< /color>< !--灰色 -->
< color name="olive">#808000< /color>< !--橄榄色 -->
< color name="purple">#800080< /color>< !--紫色 -->
< color name="maroon">#800000< /color>< !--粟色 -->
< color name="aquamarine">#7FFFD4< /color>< !--碧绿色 -->
< color name="chartreuse">#7FFF00< /color>< !--黄绿色 -->
< color name="lawngreen">#7CFC00< /color>< !--草绿色 -->
< color name="mediumslateblue">#7B68EE< /color>< !--中暗蓝色 -->
< color name="lightslategray">#778899< /color>< !--亮蓝灰 -->
< color name="lightslategrey">#778899< /color>< !--亮蓝灰 -->
< color name="slategray">#708090< /color>< !--灰石色 -->
< color name="slategrey">#708090< /color>< !--灰石色 -->
< color name="olivedrab">#6B8E23< /color>< !--深绿褐色 -->
< color name="slateblue">#6A5ACD< /color>< !--石蓝色 -->
< color name="dimgray">#696969< /color>< !--暗灰色 -->
< color name="dimgrey">#696969< /color>< !--暗灰色 -->
< color name="mediumaquamarine">#66CDAA< /color>< !--中绿色 -->
< color name="cornflowerblue">#6495ED< /color>< !--菊兰色 -->
< color name="cadetblue">#5F9EA0< /color>< !--军兰色 -->
< color name="darkolivegreen">#556B2F< /color>< !--暗橄榄绿 < color name="indigo">#4B0082< /color>< !--靛青色 -->
< color name="mediumturquoise">#48D1CC< /color>< !--中绿宝石 -->
< color name="darkslateblue">#483D8B< /color>< !--暗灰蓝色 -->
< color name="steelblue">#4682B4< /color>< !--钢兰色 -->
< color name="royalblue">#4169E1< /color>< !--皇家蓝 -->
< color name="turquoise">#40E0D0< /color>< !--青绿色 -->
< color name="mediumseagreen">#3CB371< /color>< !--中海蓝 -->
< color name="limegreen">#32CD32< /color>< !--橙绿色 -->
< color name="darkslategray">#2F4F4F< /color>< !--暗瓦灰色 -->
< color name="darkslategrey">#2F4F4F< /color>< !--暗瓦灰色 -->
< color name="seagreen">#2E8B57< /color>< !--海绿色 -->
< color name="forestgreen">#228B22< /color>< !--森林绿 -->
< color name="lightseagreen">#20B2AA< /color>< !--亮海蓝色 -->
< color name="dodgerblue">#1E90FF< /color>< !--闪兰色 -->
< color name="midnightblue">#191970< /color>< !--中灰兰色 -->
< color name="aqua">#00FFFF< /color>< !--浅绿色 -->
< color name="cyan">#00FFFF< /color>< !--青色 -->
< color name="springgreen">#00FF7F< /color>< !--春绿色 -->
< color name="lime">#00FF00< /color>< !--酸橙色 -->
< color name="mediumspringgreen">#00FA9A< /color>< !--中春绿 色 -->
< color name="darkturquoise">#00CED1< /color>< !--暗宝石绿 -->
< color name="deepskyblue">#00BFFF< /color>< !--深天蓝色 -->
< color name="darkcyan">#008B8B< /color>< !--暗青色 -->
< color name="teal">#008080< /color>< !--水鸭色 -->
< color name="green">#008000< /color>< !--绿色 -->
< color name="darkgreen">#006400< /color>< !--暗绿色 -->
< color name="blue">#0000FF< /color>< !--蓝色 -->
< color name="mediumblue">#0000CD< /color>< !--中兰色 -->
< color name="darkblue">#00008B< /color>< !--暗蓝色 -->
< color name="navy">#000080< /color>< !--海军色 -->
< color name="black">#000000< /color>< !--黑色 -->
< /resources>
android ListView 详解
在android 开发中 ListView 是比较常用的组件,它以列表的形式展示具体内容,并且
能够根据数据的长度自适应显示。抽空把对 ListView 的使用做了整理,并写了个小例子,
如下图。
列表的显示需要三个元素:
1.ListVeiw 用来展示列表的 View。
2.适配器 用来把数据映射到 ListView 上的中介。
3.数据 具体的将被映射的字符串,图片,或者基本组件。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter 和 SimpleCur sorAdapter
其中以ArrayAdapter 最为简单,只能展示一行字。SimpleAdapter 有最好的扩充性,可
以自定义出各种效果。SimpleCursorAdapter 可以认为是 SimpleAdapter 对数据库的简
单结合,可以方面的把数据库的内容以列表的形式展示出来。
我们从最简单的 ListView 开始:
print?
01 /**
02 * @author allin 03 *
04 */
05 public class MyListView extends Activity { 06
07 private ListView listView;
08 //private List<String> data = new ArrayList<String>();
09 @Override
10 public void onCreate(Bundle savedInstanceState){
11 super.onCreate(savedInstanceState);
12
13 listView = new ListView(this);
14 listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
15 setContentView(listView);
16 }
17
18
19
20 private List<String> getData(){
21
22 List<String> data = new ArrayList<String>();
23 data.add("测试数据 1");
24 data.add("测试数据 2");
25 data.add("测试数据 3");
26 data.add("测试数据 4");
28 return data;
29 } 30 }
上面代码使用了
ArrayAdapter(Context context, int textViewResourceId, List<T>
objects)来装配数据,要装配这些数据就需要一个连接 ListView 视图对象和数组数据的
适配器来两者的适配工作,
ArrayAdapter
的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_
1 是系统定义好的布局文件只显示一行文字,数据源(一个 List 集合)。同时用 setAdapter
()完成适配的最后工作。运行后的现实结构如下图:
SimpleCursorAdapter
sdk 的解释是这样的:An easy adapter to map columns from a cursor to T extViews or ImageViews defined in an XML file. You can specify which colu mns you want, which views you want to display the columns, and the XML file that defines the appearance of these views。简单的说就是方便把从游标得到
的数据进行列表显示,并可以把指定的列映射到对应的TextView 中。
下面的程序是从电话簿中把联系人显示到类表中。先在通讯录中添加一个联系人作为数
据库的数据。然后获得一个指向数据库的Cursor 并且定义一个布局文件(当然也可以使用
系统自带的)。
view source
print?
01 /**
02 * @author allin
03 *
04 */
05 public class MyListView2 extends Activity { 07 private ListView listView;
08 //private List<String> data = new ArrayList<String>();
09 @Override
10 public void onCreate(Bundle savedInstanceState){
11 super.onCreate(savedInstanceState);
13 listView = new ListView(this);
15 Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
16 startManagingCursor(cursor);
18 ListAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_1,
19 cursor,
20 new String[]{People.NAME}, 21 new int[]{android.R.id.text1});
23 listView.setAdapter(listAdapter);
24 setContentView(listView);
25 }
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);先获得一个指向系统通讯录数据库的 Cursor 对象获得数据来源。
startManagingCursor(cursor);我们将获得的 Cursor 对象交由 Activity 管理,这样 C ursor 的生命周期和 Activity 便能够自动同步,省去自己手动管理 Cursor。
SimpleCursorAdapter 构造函数前面 3 个参数和 ArrayAdapter 是一样的,最后两个
参数:一个包含数据库的列的String 型数组,一个包含布局文件中对应组件 id 的 int 型数
组。其作用是自动的将String 型数组所表示的每一列数据映射到布局文件对应 id 的组件上。
上面的代码,将NAME 列的数据一次映射到布局文件的 id 为 text1 的组件上。
注意:需要在AndroidManifest.xml 中如权限:<uses-permission android:name="
android.permission.READ_CONTACTS"></uses-permission>
运行后效果如下图:
SimpleAdapter
simpleAdapter 的扩展性最好,可以定义各种各样的布局出来,可以放上 ImageView(图
片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了
ListActivity,ListActivity 和普通的 Activity 没有太大的差别,不同就是对显示 ListView 做了许多优化,方面显示而已。
下面的程序是实现一个带有图片的类表。
首先需要定义好一个用来显示每一个列内容的xml
vlist.xml
view source
print?
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
03 android:orientation="horizontal"
android:layout_width="fill_parent"
04 android:layout_height="fill_parent">
07 <ImageView android:id="@+id/img"
08 android:layout_width="wrap_content"
09 android:layout_height="wrap_content"
10 android:layout_margin="5px"/>
11
12 <LinearLayout android:orientation="vertical"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content">
15
16 <TextView android:id="@+id/title"
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content"
19 android:textColor="#FFFFFFFF"
20 android:textSize="22px" />
21 <TextView android:id="@+id/info"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:textColor="#FFFFFFFF"
25 android:textSize="13px" />
26
27 </LinearLayout>
30 </LinearLayout>
下面是实现代码:
view source
print?
01 /**
02 * @author allin 03 *
04 */
05 public class MyListView3 extends ListActivity {
08 // private List<String> data = new ArrayList<String>();
09 @Override
10 public void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState);
13 SimpleAdapter adapter =
new SimpleAdapter(this,getData(),R.layout.vlist, 14 new String[]{"title","info","img"},
15 new int[]{R.id.title,R.id.info,R.id.img});
16 setListAdapter(adapter);
17 } 18
19 private List<Map<String, Object>> getData() { 20 List<Map<String, Object>> list =
new ArrayList<Map<String, Object>>();
22 Map<String, Object> map = new HashMap<String, Object>();
23 map.put("title", "G1");
24 map.put("info", "google 1");
25 map.put("img", R.drawable.i1);
26 list.add(map);
27
28 map = new HashMap<String, Object>();
29 map.put("title", "G2");
30 map.put("info", "google 2");
31 map.put("img", R.drawable.i2);
32 list.add(map);
33
34 map = new HashMap<String, Object>();
35 map.put("title", "G3");
36 map.put("info", "google 3");
37 map.put("img", R.drawable.i3);
38 list.add(map);
39
40 return list;
41 } 42 }
使用simpleAdapter 的数据用一般都是 HashMap 构成的 List,list 的每一节对应 ListVi
ew 的每一行。HashMap 的每个键值数据映射到布局文件中对应 id 的组件上。因为系统没
有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。下面做适配,new 一个 S
impleAdapter 参数一次是:this,布局文件(vlist.xml),HashMap 的 title 和 info,
img。布局文件的组件 id,title,info,img。布局文件的各组件分别映射到 HashMap 的 各元素上,完成适配。
运行效果如下图:
有按钮的
ListView
但是有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。添加按钮首先要
写一个有按钮的xml 文件,然后自然会想到用上面的方法定义一个适配器,然后将数据映
射到布局文件上。但是事实并非这样,因为按钮是无法映射的,即使你成功的用布局文件显
示出了按钮也无法添加按钮的响应,这时就要研究一下ListView 是如何现实的了,而且必
须要重写一个类继承BaseAdapter。下面的示例将显示一个按钮和一个图片,两行字如果
单击按钮将删除此按钮的所在行。并告诉你ListView 究竟是如何工作的。效果如下:
vlist2.xml
view source
print?
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
03 android:orientation="horizontal"
04 android:layout_width="fill_parent
"
05 android:layout_height="fill_parent">
06
07
08 <ImageView android:id="@+id/img"
09 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:layout_margin="5px"/>
12
13 <LinearLayout android:orientation="vertical"
14 android:layout_width="wrap_content"
15 android:layout_height="wrap_content">
16
17 <TextView android:id="@+id/title"
18 android:layout_width="wrap_content"
19 android:layout_height="wrap_content"
20 android:textColor="#FFFFFFFF"
21 android:textSize="22px" />
22 <TextView android:id="@+id/info"
23 android:layout_width="wrap_content"
24 android:layout_height="wrap_content"
25 android:textColor="#FFFFFFFF"
26 android:textSize="13px" />
27
28 </LinearLayout>
29
30
31 <Button android:id="@+id/view_btn"
32 android:layout_width="wrap_content"
33 android:layout_height="wrap_content"
34 android:text="@string/s_view_btn"
35 android:layout_gravity="bottom|right" />
36 </LinearLayout>
程序代码:
view source
print?
001 /**
002 * @author allin 003 *
004 */
005 public class MyListView4 extends ListActivity { 008 private List<Map<String, Object>> mData;
010 @Override
011 public void onCreate(Bundle savedInstanceState) { 012 super.onCreate(savedInstanceState);
013 mData = getData();
014 MyAdapter adapter = new MyAdapter(this);
015 setListAdapter(adapter);
016 } 017
018 private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new 019 ArrayList<Map<String, Object>>();
020
021 Map<String, Object> map = new HashMap<String, Object>();
022 map.put("title", "G1");
023 map.put("info", "google 1");
024 map.put("img", R.drawable.i1);
025 list.add(map);
026
027 map = new HashMap<String, Object>();
028 map.put("title", "G2");
029 map.put("info", "google 2");
030 map.put("img", R.drawable.i2);
031 list.add(map);
032
033 map = new HashMap<String, Object>();
034 map.put("title", "G3");
035 map.put("info", "google 3");
036 map.put("img", R.drawable.i3);
037 list.add(map);
038
039 return list;
040 }
041
042 // ListView 中某项被选中后的逻辑 043 @Override
044 protected void onListItemClick(ListView l, View v, int position, long id) {
045
046 Log.v("MyListView4-click",
(String)mData.get(position).get("title"));
047 }
048
049 /**
050 * listview 中点击按键弹出对话框 051 */
052 public void showInfo(){
053 new AlertDialog.Builder(this) 054 .setTitle("我的 listview") 055 .setMessage("介绍...") 056
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
057 @Override
058 public void onClick(DialogInterface dialog, int which) { 059 }
060 })
061 .show();
062
067 public final class ViewHolder{
068 public ImageView img;
069 public TextView title;
070 public TextView info;
071 public Button viewBtn;
072 }
075
public class MyAdapter extends BaseAdapter{
076
077 private LayoutInflater mInflater;
080 public MyAdapter(Context context){
081 this.mInflater = LayoutInflater.from(context);
082 }
083 @Override
084 public int getCount() {
085 // TODO Auto-generated method stub 086 return mData.size();
087 } 088
089 @Override
090 public Object getItem(int arg0) {
091 // TODO Auto-generated method stub 092 return null;
093 } 094
095 @Override
096 public long getItemId(int arg0) {
097 // TODO Auto-generated method stub 098 return 0;
099 } 100
101 @Override
102 public View getView(int position, View convertView, ViewGroup parent) {
103
104 ViewHolder holder = null;
105 if (convertView == null) { 107 holder=new ViewHolder();
109 convertView = mInflater.inflate(R.layout.vlist2, null);
110
holder.img = (ImageView)convertView.findViewById(R.id.img);
111 holder.title = (TextView)convertView.findViewById(R.id.title);
112 holder.info = (TextView)convertView.findViewById(R.id.info);
113 holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
114 convertView.setTag(holder);
115
116 }else {
117
118 holder = (ViewHolder)convertView.getTag();
119 }
120
12 2
holder.img.setBackgroundResource((Integer)mData.get(position).ge t("img"));
123 holder.title.setText((String)mData.get(position).get("title"));
124 holder.info.setText((String)mData.get(position).get("info"));
125
126 holder.viewBtn.setOnClickListener(new View.OnClickListener() { 127
128 @Override
129 public void onClick(View v) {
130 showInfo();
131 } 132 });
133
134
135 return convertView;
136 }
137
下面将对上述代码,做详细的解释,listView 在开始绘制的时候,系统首先调用 getC ount()函数,根据他的返回值得到 listView 的长度(这也是为什么在开始的第一张图特
别的标出列表长度),然后根据这个长度,调用getView()逐一绘制每一行。如果你的 g
etCount()返回值是 0 的话,列表将不显示同样 return 1,就只显示一行。
系统显示列表时,首先实例化一个适配器(这里将实例化自定义的适配器)。当手动完
成适配时,必须手动映射数据,这需要重写 getView()方法。系统在绘制列表的每一行
的时候将调用此方法。getView()有三个参数,position 表示将显示的是第几行,covert View 是从布局文件中 inflate 来的布局。我们用 LayoutInflater 的方法将定义好的 vlist 2.xml 文件提取成 View 实例用来显示。然后将 xml 文件中的各个组件实例化(简单的 fi ndViewById()方法)。这样便可以将数据对应到各个组件上了。但是按钮为了响应点击事
件,需要为它添加点击监听器,这样就能捕获点击事件。至此一个自定义的 listView 就完
成了,现在让我们回过头从新审视这个过程。系统要绘制ListView 了,他首先获得要绘制
的这个列表的长度,然后开始绘制第一行,怎么绘制呢?调用 getView()函数。在这个函
数里面首先获得一个View(实际上是一个 ViewGroup),然后再实例并设置各个组件,显
示之。好了,绘制完这一行了。那 再绘制下一行,直到绘完为止。在实际的运行过程中会
发现listView 的每一行没有焦点了,这是因为 Button 抢夺了 listView 的焦点,只要布局
文件中将Button 设置为没有焦点就 OK 了。