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 使用在 相對佈局內的子組件上,必須有組件纔會有相對位置。

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