Android基础学习【历史流程重走】 ---- Android入门基础(一)

一、历史回顾

随科技的迅速发展,当前已经全线进入4G时代,5G时代也即将开启。Android版本迭代迅速,如今已是6.0的版本。时不时可以看到,手机危害了当前人群的生活,如熬夜看手机会深度伤害眼睛,上班族路上低头党,聚会吃饭外只剩手机党等。确实存在一些情形,但是却无不在强调在当今社会手机在人的生活中,占有越来越重的地位。手机通讯,到手机娱乐、办公,以至于到手机管理自身财富。甚至于将来,手机将管理我们的车、房。技术的潮流不可阻挡,最好是做一个弄潮儿,次之可以“随波逐流”。聊了很多题外话,就是想说,做移动开发的小伙伴,你们太有眼光了!

重走Android重生路,一切才刚刚开始~_~

二、Android体系结构、虚拟机

Kernel作为内核,驱动硬件,驱动硬件实现最终目的效果;对内核的驱动操作,封装成为库文件,形成Library;Application就是一个应用,系统应用有打电话、发短信、照相等,实际的开发应用有工具、商城、游戏类等;Application Framework构成针对Android应用的顶层管理。包括Activity Manager,Window Manager 等。

相比于JVM,Dalvik虚拟机有更多的优势,将所有头部组合,抽离出来常量池,将方法分门别类,实现所有java的整理。理解帮助:将一个文件压缩成为压缩包后拷贝,提升数据传输速度。

之后又推出ART虚拟机。java作为高级语言,在机器执行命令之前需要编译。开启ART虚拟机,在程序安装时就直接将程序翻译成为机器语言。从而实现代码命令直接执行,从而提升效率。但是占用内存大、运行耗能多、内部依旧不稳定,阻碍了ART的快速扩展。

三、Android开发环境

ADT是使用率最高的环境。最近,随着Android Studio的逐渐成熟,ADT的使用,正在逐渐被替换掉。Google公司不提供后续维护,是最大的原因。如下是Eclipse环境下的项目目录结构:

在AS下使用Project模式,项目目录结构相似。AS更大的优势在于,内部嵌套gradle,能够实现自动打包、多版本、多渠道打包。

ADB即是Android Debug Bridge,用于连接开发环境和运行环境。adb命令可以方便使用。常用adb命令:

<span style="font-size:18px;">adb  kill-server :杀死服务,断开连接
adb  start-server :开启服务,连接设备
                                                       【adb install XXX.apk :安装手机软件   安装不可用】
adb  devices  :重启服务,链接设备
adb  connect 127.0.0.1:6555  链接天天模拟器  adb  connect 127.0.0.1:62001  链接夜神模拟器
adb uninstall  包名:卸载手机软件   卸载可用
Adb shell  进入设备
Adb shell input keyevent BACK按键
Adb shell input tap X Y 点击坐标点
Adb shell input swipe X Y X Y滑动
adb shell dumpsys activity  [ activities ]   查看activity   [] 可选
adb    ps 是看进程的
adb    top命令是看占用率的     查看手机CPU占用率
                 7817  0  15% R    37 596756K  72764K  fg u0_a75   com.ds365.order.test  //Monkey运行过程中
                 8599  0   3% S     34 586604K  56884K  fg u0_a75    com.ds365.order.test   //运行
                 8599  0   0% S     30 573724K  55252K  bg u0_a75   com.ds365.order.test   //后台运行</span>

在开发环境中,还提供了一些工具,如DDMS,hierarchyviewer。

DDMS可以管理虚拟机。

hierarchyviewer可用于查找View id,弄清楚View之间的相互关系。当前工具对于MonkeyRunner自动化测试提供很好的帮助。

四、一个小程序

<span style="font-size:18px;"><span style="font-size:18px;">public class MainActivity extends AppCompatActivity {

    private Button phoneCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        phoneCall = (Button) findViewById(R.id.phone_call);
        phoneCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * 测试发送消息
                 */
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage("15510728213", "moniqi", "你好", null, null);

                /**
                 * 指定意图:新建对象,设置动作,携带数据,触发动作
                 */
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel://15510728213"));
                startActivity(intent);
            }
        });
    }

    /**
     * 跳转下一界面
     *
     * @param view
     */
    public void changeNextPage(View view) {
        startActivity(new Intent(MainActivity.this,OnClickEventActivity.class));
    }
}</span></span>

基本编程流程:修改编写xml文件,写主类:找到关心控件,为控件添加事件。

点击事件的四种写法:

<span style="font-size:18px;">public class OnClickEventActivity extends Activity implements View.OnClickListener {
    private Button clickSecond;
    private Button clickThird;
    private Button clickFour;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.onclick_event_layout);

        clickSecond = (Button) findViewById(R.id.onclick_second);
        clickThird = (Button) findViewById(R.id.onclick_third);
        clickFour = (Button) findViewById(R.id.onclick_four);

        clickSecond.setOnClickListener(new ClickSecond());
        clickThird.setOnClickListener(new View.OnClickListener() {
            /**
             * 第三种方法:匿名内部类实现点击方法
             */
            @Override
            public void onClick(View v) {
                Toast.makeText(OnClickEventActivity.this, "匿名内部类实现", Toast.LENGTH_SHORT).show();
            }
        });
        clickFour.setOnClickListener(this);
    }

    /**
     * 第一种方式:xml中写方法
     *
     * @param view
     */
    public void clickEventFirst(View view) {
        Toast.makeText(OnClickEventActivity.this, "xml中写方法", Toast.LENGTH_SHORT).show();
    }

    /**
     * 第四种方法:类实现Onclick接口
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.onclick_four:
                Toast.makeText(OnClickEventActivity.this, "类实现Onclick接口", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    /**
     * 第二种方法:内部类实现点击方法
     */
    class ClickSecond implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Toast.makeText(OnClickEventActivity.this, "内部类实现", Toast.LENGTH_SHORT).show();
        }
    }
}</span>

五、四大布局

四大布局是:RelativeLayout、LinearLayout、FrameLayout、TableLayout。绝对布局不建议使用。

相对布局(RelativeLayout):

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/middle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="中间" />

    <Button
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/middle"
        android:text="左" />

    <Button
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/middle"
        android:text="右" />

    <Button
        android:id="@+id/up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/middle"
        android:layout_centerHorizontal="true"
        android:text="上" />

    <Button
        android:id="@+id/down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/middle"
        android:layout_centerHorizontal="true"
        android:text="下" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左上" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="右上" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="左下" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="右下" />
</RelativeLayout></span>

线性布局(LinearLayout):

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:text="LinearLayout第一行第一列" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:text="LinearLayout第一行第二列" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:layout_weight="1"
            android:text="LinearLayout第二行第一列" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:layout_weight="1"
            android:text="LinearLayout第二行第二列" />
    </LinearLayout>

</LinearLayout></span>

帧布局(FrameLayout):

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="350dp"
        android:layout_height="350dp"
        android:background="@color/black"
        android:layout_gravity="center"/>

    <TextView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="@color/blue"
        android:layout_gravity="center"/>

    <TextView
        android:layout_width="260dp"
        android:layout_height="260dp"
        android:background="@color/red"
        android:layout_gravity="center"/>

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@color/green"
        android:layout_gravity="center"/>

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorAccent"
        android:layout_gravity="center"/>
</FrameLayout></span>

RelativeLayout属性说明:

第一类:属性值为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                   离某元素上边缘的距离

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靠右

EditText 的 android:hint

设置EditText为空时输入框内的提示信息。

ImageView 的 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 用矩阵来绘制,动态缩小放大图片来显示。

源码下载

雄关漫道真如铁 而今迈步从头越~_~

时间: 2024-08-07 14:16:18

Android基础学习【历史流程重走】 ---- Android入门基础(一)的相关文章

Android基础学习【历史流程重走】 ---- 网络请求(四)

一.网络请求 移动软件及APP实现主要在于本地功能交互的实现与数据的展示,且数据常为移动软件的核心.数据常源自于 服务器,网络数据交互则扮演十分重要的角色. 二.网络情形考量 网络请求在数据交互中扮演重要角色.因其流程的特殊性,存有多种情形需要考虑. 1,返回值情形 接口崩溃,返回异常情形:以及接口正确抛出异常的返回 接口返回内容为空,或者状态正常,可用数据部分为空: 接口正常返回数据,解析数据出现错误: 2,网络请求执行过程 执行开始前:提示网络请求正在执行,给予用户良好的反馈,屏蔽用户的其他

[原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图

关于如何移植SDL2.0到安卓上面来参考我的上一篇文章:[原]零基础学习SDL开发之移植SDL2.0到Android 在一篇文章我们主要使用SDL2.0来加载一张BMP图来渲染显示. 博主的开发环境:Ubuntu 14.04 64位,Eclipse + CDT + ADT+NDK 博主曾经自己使用NDK编译出了libSDL2.so,然后使用共享库的方式来调用libSDL2中的函数,结果发现SDL\src\core\android\SDL_android.c 这个jni函数写的实在是不够自己另外做

[原]零基础学习SDL开发之在Android使用SDL2.0显示BMP叠加图

关于如何移植在android上使用SDL,可以参考[原]零基础学习SDL开发之移植SDL2.0到Android 和 [原]零基础学习SDL开发之在Android使用SDL2.0显示BMP图 . 在一篇文章我们主要使用SDL2.0来加载一张BMP图来渲染显示,同时叠加一张图作为背景图. 博主的开发环境:Ubuntu 14.04 64位,Eclipse + CDT + ADT+NDK 在前面两篇文章我们知道了如何移植SDL2.0到android上面来,并且可以在Android上面来显示一张图片,这篇

[原]零基础学习SDL开发之在Android使用SDL2.0加载字体

在上一篇文章我们知道了如何在android使用SDL2.0来渲染显示一张png图,而且在上上一篇我们知道如何使用sdl来渲染输出bmp图,那么sdl是否可以渲染输出自己喜爱的字体库的字体呢?答案是当然可以. 我们需要移植SDL_ttf字体库来支持相应的字体的渲染输出. 一.移植SDL_ttf库: 使用如下命令,从SDL Mercurial获取SDL_image的源码: hg clone https://hg.libsdl.org/SDL_ttf/ 将SDL_ttf拷贝到在上一篇文章中的andro

android 横竖屏切换不重走生命周期

android在系统配置发生改变时,Activity会被重新创建,但是某些情况下我们希望系统配置改变时不会重新创建Activity,这个时候我们可以给Activity指定相对应的configChanges属性,在manifest加入 android:configChanges="keyboardHidden|orientation|screenSize" orientation:屏幕方向发生了改变,如旋转屏幕 screenSize:屏幕尺寸发生了变化,如旋转屏幕 keyboardHid

大数据技术之_23_Python核心基础学习_02_ 流程控制语句 + 序列(10.5小时)

第三章 流程控制语句3.1 简介3.2 条件判断语句(if 语句)3.2.1 if 语句3.2.2 input 函数3.2.3 if-else 语句3.2.4 if-elif-else 语句3.2.5 if 练习3.3 循环语句(while 语句 + for 语句)3.3.1 while 循环3.2.2 while 练习3.4 循环嵌套3.5 break 和 continue3.6 质数练习的优化3.7 小游戏 <唐僧大战白骨精>第四章 序列4.1 列表(list)的简介4.2 列表的切片4.

android studio学习----Failed to resolve: com.android.support:design:22.1.1

这个目前好像没有合适的办法,唯一可行的就是 点击那个提示  进行SDK Manager下载就可以了 但是天朝的网啊,我试了很多次,突然的可以下载,运气啊 类似这一系列问题解决办法就是  重新更新SDK吧 failed to resolve com.android.support:appcompat-v7:22 and com.android.support:recyclerview-v7:21.1.2

Shell基础学习(六) 流程控制

1.if if的语法格式 if conditon then command1 command2 ``` commandn fi 2.if else if conditon then command1 command2 ``` commandn else command1 command2 ``` commandn fi 3.if elseif else if conditon then command1 command2 ``` commandn else if conditon command

魏兆辉的IOS基础学习笔记之十二 OC语言基础-07 Foundation内存管理

本篇博文,将给大家介绍下再Objective-C中如何使用内存管理.一个程序运行的时候,如果不及时的释放没有用的空间内存.那么,程序会越来 越臃肿,内存占用量会不断升高.我们在使用的时候,就会感觉很卡,最终使得程序运行奔溃.因此,将无效的内存及时清理释放,是非常有必要的. 一个对象在最初创建使用,到最后的回收释放,经历的是怎样一个过程呢?包括:诞生(通过alloc或new方法实现).生存(接收消息并执行操作).交友(通过复合以及向方法传递参数).最终死去(被释放掉). 一.引用计数 在对象创建的