andorid 开发

基础简介:

基于 Liunx 平台的开放性操作系统的总称,该平台由操作系统 中间件 用户界面和应用软件做成。
系统架构: 高层到底层分为: 应用程序层,架构层, 系统运行库层,Liunx核心层。
应用程序框架层:
开发人员访问的核心应用程序使用API框架:
丰富可以扩展的试图views ,构建应用程序 包括列表 按钮 甚至嵌入web 浏览器
Content Provider: 使得应用程序可以访问另外一个应用程序,或者共享他们自己数据
资源管理器: 提供非代码资源的访问本地字符串,图形,和布局文件( layout files )。
通知管理器: 应用程序在状态栏中显示自定义 提示功能
活动管理器: 管理应用程序生命周期并提供常用的导航回退功能
安装四大组件: activity, service, content provider , BoradCast Receiver

  • activity: 用于表现功能
  • service:后台运行服务,不提供界面呈现
  • BroadcastReceiver: 用于接收广播
  • Content Receiver: 支持多个应用中存储和读取数据 相当于数据库
  • Activity : 开发者最通用的, 一个android 应用由多个Activity 组成 多个Activity 之间相互跳转,在APP 所有操作认为是Activity 活动
    android 记忆功能: 将每个应用的开始到当前屏幕保存到一个历史堆栈中
  • service: 两种方式 Context.startService(), Context.bindService()
    service 经过onCreate-> onStart(如果Servic有调用起来 android 先调用onCreate 在调用onStart) onStart 方法多次调用 -> stopService 关闭Service,
    第二中方式: Context.bindService, Service 经历onCreate -> onBind() ,onBind 返回一个IBind接口实例,IBind 允许客户端回调服务方法
  • 广播接收器 BroadcastReceive:
    应用程序之间传输信息机制
  • Content Provider:
    第三方应用程序访问方案 提供第三方方式 间接方式SD卡上数据

Andorid Runtime

Andoird 高级版本中,每个应用在自己的进程中允许。 有自己的ART 实例,ART通过执行的·DEX 文件在低设备允许多个虚拟机, Dex 是Android 设计的字节码格式 通过工具链 JACK将 JAVA 源代码编译成DEX 自己码
Native C /C++ 通过JAVA 语言调用原始的C库

Android UI

编码设计 : UI设计和编码设计 UI设计由美工设计高保真设计
Android 有许多预设UI组件,VUE 使用不同控局和布局
布局Layout : 由 view + Viewgroup
UI 界面 与视图容器构建起来,

熟悉XML与JAVA代码对UI界面设计:

在android 页面布局中 通过之下两种方式进行Layout 布局:

  1. XML 方式进行布局 (推荐使用)
  2. 通过代码方式书写,动态加载使用代码书写
  3. 使用 JAVA代码 和 XML 混合控制UI界面
  4. 开发自定义的view
XMLUI布局
  1. Android 应用的res/layout 目录下面编写XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  1. xml 文件定义后,需要在 mainActivity.java 中利用JAVA 代码显示XML的文件布局内容:
## 无需写扩展名称, 写文件名称
setContentView(R.layout.activity_main);

实例: XML布局文件实现游戏开始界面:

游戏开始界面: 1. 布局管理器, 2. 布局管理器中间添加button 按钮(button没有学习到位,使用 文本框替代)

  1. minmap 背景图片存放在 res/minmap 目录中

代码设计如下: 通过 XML布局 学到之下知识:

  1. @ 进行引用当前工程下面其他目录下资源: 比如如下:
  2. xml 中引用的外部字符串 必须放到特定目录下统一保存,这样为了方便管理: string.xml 下面
  3. 在Main_activity 中 显示加载xml 文件
### 设计背景文件 引用minmap / bg 文件
  android:background = "@mipmap/bg"
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom = "16dp"
    android:paddingLeft = "16dp"
    android:paddingRight = "16dp"
    android:paddingTop = "16dp"
    android:background = "@mipmap/bg"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity = "center"
        android:textSize="20sp"
        android:textColor="#115572"

        android:text="@string/start"
         />

</FrameLayout>
使用JAVA代码控制UI界面
  1. 通过JAVA swing 进行JAVA布局:
  2. 代码必须在Super() 调用父类构造器方法后面

开发自定义view 组件

自定义的view 组件例子: 随手指一动小兔子

  1. 开发自定义rabbitview 组件,继承父类View组件
  2. 在MainActivity 中 实例化这个子组件
package com.example.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

public class RabbitView extends View {
    public float bitmapX;
    public float bitmapY;


    public RabbitView(Context context) {
        super(context);
        bitmapX = 290;
        bitmapY = 130;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 进行画图
        Paint paint = new Paint();
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),R.mipmap.rabbit);
        // plot 使用 canvas
        canvas.drawBitmap(bitmap, this.bitmapX, this.bitmapY, paint);

        // 强制回收图片
        if(bitmap.isRecycled()){
            bitmap.recycle();
        }
    }
}

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


       setContentView(R.layout.activity_main);

       // 获取布局管理器 通过id
        FrameLayout frameLayout =  findViewById(R.id.myLayout);

        final RabbitView rabbitView = new RabbitView(this);

        // 添加触摸事假监听器,让其跟随手指移动
        rabbitView.setOnTouchListener(new View.OnTouchListener(){

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                rabbitView.bitmapX = motionEvent.getX();
                rabbitView.bitmapY = motionEvent.getY();

                rabbitView.invalidate();
                return true;
            }
        });

        // 必须将小兔子添加到frameLayout 中
        frameLayout.addView(rabbitView);
  }
}

布局管理器

目的: 让开发的UI界面适用于所有不同型号的机器,不同型号的机器意味着不同的屏幕分辨率。
五种常用不知管理器:

  1. RelativelLayout
  2. linearLayout
  3. FrameLayout
  4. TableLayout
  5. AbsoulteLayout 不利用屏幕自适应 适用 RelativeLayout 、FrameLayout 替代
  6. 4.0 版本后 适用GridLayout 替代TableLayout
  • 相对布局管理器
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
   <!-- -->
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom = "16dp"
    android:paddingLeft = "16dp"
    android:paddingRight = "16dp"
    android:paddingTop = "16dp"
    android:background = "@mipmap/bg"
    android:id = "@+id/myLayout"
    tools:context=".MainActivity">
</FrameLayout>

android:gravity: 设置布局中各个组件摆放位置
android:ignoreGravity: 不受上个组件影响对于的组件有对应的ID

<?xml version="1.0" encoding="utf-8"?>
<RealtiveLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom = "16dp"
    android:paddingLeft = "16dp"
    android:paddingRight = "16dp"
    android:paddingTop = "16dp"
    android:background = "@mipmap/bg"
    android:gravity = "center"
    andorid:ignoreGravity = "@id/TextView1"
    tools:context=".MainActivity">

    <TextView
    	android:id = "@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity = "center"
        android:textSize="20sp"
        android:textColor="#115572"

        android:text="@string/start"
         />

</RealtiveLayout>

在 相对布局管理器中 适用 android:gravity ,android:ignoreGravity 使用 是不够的 ,在提供内部类
RelativeLayout.LayoutParams 使用在 相对布局内的子组件上,必须有组件才会有相对位置。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章