1 / 40

Android 开发及框架培训

Android 开发及框架培训. 徐德良 2012-7-23. 目录. 目录. 目录. Android 开发基本 知识. 一、工程目录 Src / 源代码 Gen/ 自动生成常量 Assets/ 类似游戏数据 Bin/ 编译输出文件 Jni / 本地 c 代码 Libs/ 库目录 Androidmanifest.xml 应用及组件的描述 Project.propertiies 工程属性比如目标版本 Local.properties Res/. Android 开发基本 知识. Res/ Anim / 动画效果 xml Color/ 颜色定义

nikki
Télécharger la présentation

Android 开发及框架培训

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android开发及框架培训 徐德良 2012-7-23

  2. 目录

  3. 目录

  4. 目录

  5. Android开发基本知识 • 一、工程目录 Src/ 源代码 Gen/自动生成常量 Assets/类似游戏数据 Bin/编译输出文件 Jni/本地c代码 Libs/库目录 Androidmanifest.xml 应用及组件的描述 Project.propertiies工程属性比如目标版本 Local.properties Res/

  6. Android开发基本知识 Res/ Anim/动画效果xml Color/颜色定义 Drawable/可绘制的资源png、jpg,gif,xml Layout/布局xml Menu/菜单 Raw/音频数据等 Values/文字资源等 Xml/xml配置文件

  7. Android开发基本知识 • 二、应用配置<manifest> • 属性: • xmlns:android=http://schemas.android.com/apk/res/android • package="com.example.android.apis" • android:versionCode="integer“ • android:versionName="string“ • android:installLocation=["auto" | "internalOnly" | "preferExternal"] > • 子标签 • <uses-permission /> • <permission /> • <permission-tree /> • <permission-group /> • <instrumentation /> • <uses-sdk/> • <uses-configuration /> • <uses-feature /> • <supports-screens /> • <compatible-screens /> • <supports-gl-texture /> • <application>

  8. Android开发基本知识 • <uses-sdk/> • <uses-sdkandroid:minSdkVersion="integer" • android:targetSdkVersion="integer" • android:maxSdkVersion="integer" /> • <uses-permission /> • <uses-permission android:name="android.permission.READ_CONTACTS" /> • <uses-permission android:name="android.permission.WRITE_CONTACTS" /> • <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> • <uses-permission android:name="android.permission.INTERNET" />

  9. Android开发基本知识 • 三、<application> • <activity> • <intent-filter> • <action /><category /><data /> • </intent-filter> • <meta-data /> • </activity> • <activity-alias><intent-filter> . . . </intent-filter><meta-data /></activity-alias><service><intent-filter> . . . </intent-filter><meta-data/></service> • <receiver><intent-filter> . . . </intent-filter><meta-data /></receiver> • <provider><grant-uri-permission /><meta-data /></provider>

  10. Android开发基本知识 • 四、<activity> • android:configChanges=["mcc", "mnc", "locale", "touchscreen", "keyboard", "keyboardHidden", "navigation", "screenLayout", "fontScale", "uiMode", "orientation", "screenSize", "smallestScreenSize"] • android:icon="drawable resource" • android:label="string resource" • android:launchMode=["multiple" | "singleTop" | "singleTask" | "singleInstance"] • android:name="string“ • android:screenOrientation="landscape" | "portrait"

  11. Android开发基本知识 四、activity 生命周期

  12. Android开发基本知识 • 五、布局使用 • FrameLayout,  • GridLayout, • LinearLayout,->TableLayout, TableRow • RelativeLayout • Padding and Margins

  13. 快速开发项目 • 框架解决如下问题: • 1、工程师只关心ui布局 • 2、工程师只关心数据解析及数据结构 • 3、快速搭建程序架构,提高开发效率

  14. 快速开发项目 框架配置初始化(主activity中调用) Xml配置,提供domain等 • FrameworkConfig.getInst(this,R.xml.config); 初始化应用跟目录 • ZMFilePath.APPROOT="zmframework";

  15. 快速开发项目

  16. 快速开发项目 业务demo DemoBusi(UiCallBackbCallBack,Class<?> cls) DemoBusi lb =new DemoBusi(this,DemoStruct.class); lb.start=10; lb.iExecute(); @Override public void uiCallBack(BaseBusiobj) { • if(objinstanceofDemoBusi) • { • DemoBusidemoBusi = (DemoBusi)obj; • DemoStructds = (DemoStruct)demoBusi.getBaseStruct(); • } }

  17. 快速开发项目 解析demo • public DemoStruct(int type) • { • super(type); • } • //实现解析工作 • @Override • public boolean parse(byte[]data) • { • }

  18. 快速开发项目 封装的类 获取经纬度业务:GetLocationBusi 保存值到本地文件:LocalData MD5加密: MD5Util 对象存储对象:ObjectStores 文件路径:ZMFilePath 文件读写:ZMFile

  19. 快速开发项目 表单数据提交 • /** • * @author xudeliang • * 曝光 • */ • public class ExposureBusi extends BaseBusi • { • private String tail = "exposure.php"; • public String brand; • public String contact; • public String content; • public String verifycode; • public String filePath; • public String userid; • public ExposureBusi(UiCallBackbCallBack,Class<?> cls) • { • super(bCallBack,cls); • } • /* (non-Javadoc) • * @see com.andframework.business.BaseBusi#prepare() • */ • @Override • protected void prepare() • { • reqParam = tail; • Map<String, String> mapnormal = new HashMap<String, String>(); • mapnormal.put("brand", brand); • mapnormal.put("contact", contact); • mapnormal.put("content", content); • mapnormal.put("code",verifycode); • mapnormal.put("userid",userid); • this.setFormData(mapnormal); • if(filePath!=null &&filePath.length()>0) • { • Map<String, UploadFileVO> mapfile = new HashMap<String,UploadFileVO>(); • UploadFileVOvo = new UploadFileVO(filePath); • intdotindex = filePath.lastIndexOf('.'); • int pl = filePath.lastIndexOf('/'); • String ext =filePath.substring(dotindex+1); • vo.setContentType("image/"+ext); • String filename =filePath.substring(pl+1); • vo.setFileName(filename); • mapfile.put("file", vo); • this.setUploadFileData(mapfile); • } • //this.pushHeadPropertys(new KeyValue());//setHeadPropertys(kvs); • } • }

  20. 支持不同的平台版本 • 一、xml配置 • <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > <uses-sdkandroid:minSdkVersion="4" android:targetSdkVersion="15" /> ... </manifest>

  21. 支持不同的平台版本 • 二、检查版本 • if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ActionBaractionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); }

  22. 支持不同的平台版本 • 三、使用系统样式和主题 • 对话框风格: • <activity android:theme="@android:style/Theme.Dialog"> • 透明风格: • <activity android:theme="@android:style/Theme.Translucent"> • 自定义风格: • <activity android:theme=“@style/CustomTheme”>定义在/res/values/styles.xml • 统一应用风格 • <application android:theme="@style/CustomTheme"> • 四、平台差异描述 • http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels

  23. 多分辨率支持 • 一、创建不同的布局 • res/ layout/ # default (portrait) for handsets: • layout-land/ # landscape • layout-large/ # large (portrait) for tablets • layout-large-land/ # large landscape •  res/layout-xlarge/ 平板 3.x • res/layout-sw600dp/ 最小600dp 放该目录 • res/layout/main_activity.xml # For handsets (smaller than 600dp available width) res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger) res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger

  24. 多分辨率支持 • 二、创建不同的位图 • xhdpi: 2.0 • hdpi: 1.5 • mdpi: 1.0 (baseline) • ldpi: 0.75 • 以上是三个资源目录,意味着你在mdpi下面切了100*100的图,那么应该切200*200给xhdpi,150*150给hdpi,75*75给ldpi • res/ • drawable-xhdpi/ awesomeimage.png • drawable-hdpi/ awesomeimage.png • drawable-mdpi/ awesomeimage.png • drawable-ldpi/ awesomeimage.png

  25. 多分辨率支持 • 三、使用 "wrap_content" and "match_parent"

  26. 多分辨率支持 • 四、使用RelativeLayout,不用AbsoluteLayout

  27. 多分辨率支持 • 五、坐标使用dp作为单位,字体大小使用sp作为单位。不用像素px作为单位。 • px = dp * (dpi / 160) • Dpi:dots per inch • Dp: Density-independent pixel

  28. 多语言支持 • res/ • values/ strings.xml • values-es/ strings.xml , • values-fr/ strings.xml ,Spanish • English (default locale), /values/strings.xml: • Spanish, /values-es/strings.xml: • French, /values-fr/strings.xml: • String hello = getResources().getString(R.string.hello_world); • TextViewtextView = new TextView(this); • textView.setText(R.string.hello_world); • <TextViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> • 只要在res/目录下在重新定义values-语言编码,/values-fr/ 标识法语,values-zh-rCN简体汉语,values-zh-rTW繁体,values-ja-rJP日语,前面加个r(region)后面是地区。values-es-rUS西班牙语。 • http://www.loc.gov/standards/iso639-2/php/code_list.phpISO 639-1 Code • http://guojianhui0906.iteye.com/blog/1271827

  29. 横竖屏适配 • 在res目录下建立竖屏布局目录layout-land。目录下所有id的命名要求跟横屏目录下一样,以便系统适配到。 • android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置,那样,进行中的游戏就不会自动重启了! • 有的程序适合从竖屏切换到横屏,或者反过来,这个时候怎么办呢?可以在配置Activity的地方进行如下的配置android:screenOrientation="portrait"。这样就可以保证是竖屏总是竖屏了,或者landscape横向。 • 而有的程序是适合横竖屏切换的。如何处理呢?首先要在配置Activity的时候进行如下的配置:android:configChanges="keyboardHidden|orientation",另外需要重写Activity的onConfigurationChanged方法。实现方式如下,不需要做太多的内容: • @Override • public void onConfigurationChanged(Configuration newConfig) { • super.onConfigurationChanged(newConfig); • if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { • // land do nothing is ok • } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { • // port do nothing is ok • } • }

  30. 工具类 Util printLog Dip2px Px2dip urlToFileName emailValidate hideInputMethod getStatusBarHeight setFullScreen getLocation shareContent

  31. 常用Ui控件封装 CustomListView:下拉刷新,可以获取更多 • channelview= (CustomListView) findViewById(R.id.channelview); • channelview =(CustomListView) findViewById(R.id.list); • channelview.setonGetMoreListener(this); • customListView.setonRefreshListener(this); • customListView.setMoreTextColor(Color.parseColor("#668B8B")); • customListView.setUpdateTextColor(Color.parseColor("#668B8B")); • customListView.setHeadBackgrondColor(Color.parseColor("#DCDCDC")); • customListView.setBottomBackgrondColor(Color.parseColor("#DCDCDC")); • customListView.setPageSize(TitleListBusi.pagesize); • customListView.freshData(); • channelview.setOnItemClickListener(new OnItemClickListener(){ • @Override • public void onItemClick(AdapterView<?> arg0, View v, int arg2, • long arg3) • { • // TODO Auto-generated method stub • TextViewTt = (TextView)v; • Bundle bundle=new Bundle(); • Intent intent=new Intent(); • intent.putExtra("bundle",bundle); • CChannelnli = (CChannel)v.getTag(); • bundle.putString("listid",nli.cid+""); • bundle.putString("title",Tt.getText().toString()); • intent.setClass(HealthShareActivity.this,ActicleListActivity.class); • HealthShareActivity.this.startActivity(intent); • } • });

  32. 常用Ui控件封装 • 刷新 • @Override • public void onGetmore() • { • int num = customListView.getListNum(); • intpagenum =(num/TitleListBusi.pagesize)+1; • reqActileListBusi(pagenum); • } • /* (non-Javadoc) • * @see com.palmedia.framework.ui.CustomListView.OnRefreshListener#onRefresh() • */ • @Override • public void onRefresh() • { • reqActileListBusi(1); • // TODO Auto-generated method stub • }

  33. 常用Ui控件封装 • 刷新 • private void updateNewsClassItemUI() • { • channelview.removeAllView(); • int size = pchanels[currentTabIndex].childrens.length; • for(inti=0;i<size;i++) • { • TextView text = new TextView(this); • //text.setBackgroundResource(R.drawable.buttonstyle); • //text.setBackgroundColor(Color.parseColor("#F8F8FF")); • //text.setBackgroundResource(R.drawable.newslistshap); • // android:background="#F8F8FF" • text.setText(pchanels[currentTabIndex].childrens[i].channelname); • text.setTag(pchanels[currentTabIndex].childrens[i]); • text.setGravity(Gravity.CENTER); • text.setTextColor(Color.parseColor("#8968CD")); • text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24); • channelview.addViewToLast(text); • } • channelview.onRefreshComplete(); • }

  34. 常用Ui控件封装 • 图片控件CustomImageView • setImageFromeUrl(java.lang.Stringurl)

  35. 常用Ui控件封装 • 提示框控件CustomMessageShow

  36. 动画 • Android的动画效果分为两种,一种是tween animation(补间动画),第二种是frame by frame animation。一般我们用的是第一种。补间动画又分为AlphaAnimation,透明度转换 RotateAnimation,旋转转换 ScaleAnimation,缩放转换 TranslateAnimation位置转换(移动)。 • http://www.cnblogs.com/feisky/archive/2010/01/11/1644482.html • tween animation: • AlphaAnimation渐变透明度动画效果 • ScaleAnimation渐变尺寸伸缩动画效果 • TranslateAnimation画面转换位置移动动画效果 • RotateAnimation画面转移旋转动画效果 • Activity切换动画 • 在startActivity()或者finish()后面添加overridePendingTransition(intenterAnim, intexitAnim) • 实现淡入淡出的效果:overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out); • 由左向右滑入的效果: • overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right)

  37. 动画 • 自定义: res/anim/ • <?xml version="1.0" encoding="utf-8"?> • <setxmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@[package:]anim/interpolator_resource" android:shareInterpolator=["true" | "false"] > • <alphaandroid:fromAlpha="float" android:toAlpha="float" /> • <scaleandroid:fromXScale="float" android:toXScale="float" android:fromYScale="float" android:toYScale="float" android:pivotX="float" android:pivotY="float" /> • <translateandroid:fromXDelta="float" android:toXDelta="float" android:fromYDelta="float" android:toYDelta="float" /> • <rotateandroid:fromDegrees="float" android:toDegrees="float" android:pivotX="float" android:pivotY="float" /> • <set> ... </set> • </set>

  38. 动画 • View动画 • Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); • findViewById(R.id.pw).startAnimation(shake); • ViewFlipperflipper; • flipper = new ViewFlipper(currentAct); • flipper.addView(pre, lpre); • flipper.addView(next, lnext); • Animation a = AnimationUtils.loadAnimation(currentAct, R.anim.push_left_in);// flipper.setInAnimation(a); • a.setAnimationListener(this); flipper.setOutAnimation(AnimationUtils.loadAnimation(currentAct,R.anim.push_left_out)); • flipper.showNext();

  39. 动画 • 例子 • 淡出效果 • <?xml version="1.0" encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><alphaandroid:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" /></set> • 淡入效果 • <?xml version="1.0" encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><alphaandroid:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" /></set><!-- fromAlpha:开始时透明度toAlpha:结束时透明度duration:动画持续时间--> • 旋转效果 • <?xml version="1.0" encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><rotateandroid:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="300" android:toDegrees="-360" android:pivotX="10%" android:pivotY="100%" android:duration="10000" /></set><!-- fromDegrees开始时的角度toDegrees动画结束时角度pivotX,pivotY不太清楚,看效果应该是定义旋转的圆心的--> • 缩放效果: • <?xml version="1.0" encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><scaleandroid:interpolator= "@android:anim/decelerate_interpolator" android:fromXScale="0.0" android:toXScale="1.5" android:fromYScale="0.0" android:toYScale="1.5" android:pivotX="50%" android:pivotY="50%" android:startOffset="0" android:duration="10000" android:repeatCount="1" android:repeatMode="reverse" /></set><!-- interpolator指定动画插入器,常见的有加速减速插入器accelerate_decelerate_interpolator,加速插入器accelerate_interpolator,减速插入器decelerate_interpolator。fromXScale,fromYScale,动画开始前X,Y的缩放,0.0为不显示,1.0为正常大小toXScale,toYScale,动画最终缩放的倍数,1.0为正常大小,大于1.0放大pivotX,pivotY动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从屏幕中间开始startOffset,动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒duration,一次动画效果消耗的时间,单位毫秒,值越小动画速度越快repeatCount,动画重复的计数,动画将会执行该值+1次repeatMode,动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变--> • 移动效果: • <?xml version="1.0" encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromXDelta="320" android:toXDelta="0" android:fromYDelta="480" android:toYDelta="0" android:duration="10000" /></set><!-- fromXDelta,fromYDelta起始时X,Y座标,屏幕右下角的座标是X:320,Y:480toXDelta,toYDelta动画结束时X,Y的座标-->

  40. 动画 • 帧动画 • <animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="true">    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /></animation-list> • ImageViewrocketImage; • rocketImage.setBackgroundResource((R.anim.rocket_thrust); • AnimationDrawablerocketAnimation = (AnimationDrawable) rocketImage.getBackground(); • rocketAnimation.start();

More Related