Android workshop - 01. Getting started on android phone

64
Android Workshop 01. Getting started on android phone Johnny Sung

Transcript of Android workshop - 01. Getting started on android phone

Page 1: Android workshop - 01. Getting started on android phone

Android Workshop01. Getting started on android phone

Johnny Sung

Page 2: Android workshop - 01. Getting started on android phone
Page 3: Android workshop - 01. Getting started on android phone
Page 4: Android workshop - 01. Getting started on android phone

Android Studio 設定

• Appearance

• Theme: Darcula

• Keymap

• Keymaps: Eclipse (Mac OS X)

Page 5: Android workshop - 01. Getting started on android phone

Appearance Settings

Page 6: Android workshop - 01. Getting started on android phone

Keymap Settings

Page 7: Android workshop - 01. Getting started on android phone

⼿手機前置作業• 開啟USB偵錯

• 設定 > 關於

• 連續點擊 Build Number

• 設定 > 開發⼈人員選項

• 勾選未知的來源

• 設定 > 安全性

Page 8: Android workshop - 01. Getting started on android phone

開啟 開發⼈人員選項 選單

Page 9: Android workshop - 01. Getting started on android phone

勾選未知的來源 開啟 USB 偵錯

Page 10: Android workshop - 01. Getting started on android phone
Page 11: Android workshop - 01. Getting started on android phone
Page 12: Android workshop - 01. Getting started on android phone
Page 13: Android workshop - 01. Getting started on android phone

Exercise 00: Hello, World

Page 14: Android workshop - 01. Getting started on android phone

Exercise 00: Hello, World

Page 15: Android workshop - 01. Getting started on android phone

建⽴立專案

• 建⽴立⼀一個專案

• Application name: PetStar

• Company Domain: johnny.com

• Package name: com.johnny.petstar

Page 16: Android workshop - 01. Getting started on android phone
Page 17: Android workshop - 01. Getting started on android phone
Page 18: Android workshop - 01. Getting started on android phone
Page 19: Android workshop - 01. Getting started on android phone
Page 20: Android workshop - 01. Getting started on android phone
Page 21: Android workshop - 01. Getting started on android phone

執⾏行您的 Hello, World• 接上 Android ⼿手機

• 安裝驅動 (Windows)

• ⼿手機勾選允許裝置

• 按下執⾏行

Page 22: Android workshop - 01. Getting started on android phone
Page 23: Android workshop - 01. Getting started on android phone

Code Examples

http://goo.gl/RFPHzi

PetStar - Exercise00: HelloWorld

Page 24: Android workshop - 01. Getting started on android phone

Break;

Page 25: Android workshop - 01. Getting started on android phone

Project Overview

Page 26: Android workshop - 01. Getting started on android phone

專案結構• Module

• AndroidManifest.xml (專案設定)

• java (程式碼)

• res

• drawable (圖⽚片)

• layout (版⾯面)

• values/strings.xml (字串檔)

• Gradle

Page 27: Android workshop - 01. Getting started on android phone

activity_main.xml

Page 28: Android workshop - 01. Getting started on android phone

MainActivity.java

Page 29: Android workshop - 01. Getting started on android phone

AndroidManifest.xml

Page 30: Android workshop - 01. Getting started on android phone

Gradle

Page 31: Android workshop - 01. Getting started on android phone

Exercise 01: Show pet’s name

Page 32: Android workshop - 01. Getting started on android phone

Exercise 01: Show pet’s name

Page 33: Android workshop - 01. Getting started on android phone

activity_main.xmlChange to LinearLayout

Add this

Page 34: Android workshop - 01. Getting started on android phone

MainActivity.java

Add this

Page 35: Android workshop - 01. Getting started on android phone

鎖定螢幕橫直• AndroidManifest.xml

• <activity> </activity> 中加上

• android:screenOrientation="portrait"

• android:screenOrientation="landscape"

Page 36: Android workshop - 01. Getting started on android phone

AndroidManifest.xml

Add this

Page 37: Android workshop - 01. Getting started on android phone

Code Examples

http://goo.gl/UH814G

PetStar - Exercise01: PetsName

Page 38: Android workshop - 01. Getting started on android phone

Layout properties• android:layout_width="wrap_content"

• android:layout_height="wrap_content"

• wrap_content

• match_parent (fill_parent)

• dp (dip)

• px

Page 39: Android workshop - 01. Getting started on android phone

Layout properties• Margin

• android:layout_marginLeft="5dp" • android:layout_marginRight="5dp" • android:layout_marginTop="5dp" • android:layout_marginBottom=“5dp"

• Padding • android:paddingLeft="5dp" • android:paddingRight="5dp" • android:paddingTop="5dp" • android:paddingBottom="5dp"

Page 40: Android workshop - 01. Getting started on android phone

Layout

• RelativeLayout

• FrameLayout

• LinearLayout

Page 41: Android workshop - 01. Getting started on android phone

LinearLayout

• 排列⽅方向

• android:orientation="vertical" 排列⽅方向為垂直(由上⽽而下)

• android:orientation="horizontal" 排列⽅方向為⽔水平(由左⽽而右)

Page 42: Android workshop - 01. Getting started on android phone

RelativeLayout

• 對⿑齊類 • android:layout_alignParentLeft 靠左對⿑齊,(吸附邊框左邊)

• android:layout_alignParentTop 靠上對⿑齊,(吸附邊框上⽅方)

• android:layout_alignParentRight 靠右對⿑齊,(吸附邊框右邊)

• android:layout_alignParentBottom 靠下對⿑齊,(吸附邊框下⽅方)

• android:layout_centerInParent 置中,(計算放在正中間)

Page 43: Android workshop - 01. Getting started on android phone

RelativeLayout• 相對位置類

• android:layout_toLeftOf 這在(等下要寫的控制項的名)的左邊

• android:layout_toRightOf 這在(等下要寫的控制項的名)的右邊

• android:layout_below 這在(等下要寫的控制項名)的下⾯面

• android:layout_above 這在(等下要寫的控制項名)的上⾯面

Page 44: Android workshop - 01. Getting started on android phone

TextView

• android:text="Hello!"

• android:background="#ff384c0a"

• android:textColor="#ffe5d9ff"

• android:textSize="35sp"

Page 45: Android workshop - 01. Getting started on android phone

dp? px? 單位⻑⾧長度⼤大集合• 螢幕⻑⾧長度

• px (pixel) 像素,RGB螢幕三原⾊色合成的⼀一個彩⾊色點

• 物理⻑⾧長度

• in (inches) 英吋,1 Inch = 2.54 cm

• pt (points) 點數,1pt = 1/72 Inch = 0.352777778 mm

• 密度

• dpi (dot per inch):⼀一英吋有幾個點

• ppi (pixel per inch):⼀一英⼨寸有多少像素

Page 46: Android workshop - 01. Getting started on android phone

dp? px? 單位⻑⾧長度⼤大集合

• dp, dip (Density-Independent Pixels) 對應到在 160 dpi 的螢幕上的幾個px

• 1 dp = 1/160 Inch = 0.15875 mm

• sp (Scale Independent Pixels)對應在 160 dpi 的螢幕上的幾個 pt。

Page 47: Android workshop - 01. Getting started on android phone

Density

160dpi (mdpi) 326dpi (xhdpi)

( >300dpi = Retina )

iPhone 3Gs iPhone 4

Page 48: Android workshop - 01. Getting started on android phone

我的圖應該要多⼤大?

• px = dip * (density / 160) • dip = px / (density / 160) • sp = pt * (density / 160) • 2:3:4:6:8 scaling ratio

http://labs.rampinteractive.co.uk/android_dp_px_calculator/

Page 49: Android workshop - 01. Getting started on android phone

我的圖應該要多⼤大?

• ldpi (low) ~120dpi • mdpi (medium) ~160dpi • hdpi (high) ~240dpi • xhdpi (extra-high) ~320dpi • xxhdpi (extra-extra-high) ~480dpi • xxxhdpi (extra-extra-extra-high) ~640dpi

http://pixplicity.com/dp-px-converter/

Page 50: Android workshop - 01. Getting started on android phone

Density Independent

Page 51: Android workshop - 01. Getting started on android phone

Activity LifeCycle• onCreate() 程式⼀一開始執⾏行時

• onResume() 畫⾯面可⾒見時

• onPause() 畫⾯面不可⾒見時

• onDestroy() 程式結束時

• 橫直轉換時例外

Page 52: Android workshop - 01. Getting started on android phone
Page 53: Android workshop - 01. Getting started on android phone

Exercise 02: Pet’s name card

Page 54: Android workshop - 01. Getting started on android phone
Page 55: Android workshop - 01. Getting started on android phone
Page 56: Android workshop - 01. Getting started on android phone
Page 57: Android workshop - 01. Getting started on android phone

http://www.collarplanetonline.com/military-style-qr-code-pet-id-tag-in-black/

Page 58: Android workshop - 01. Getting started on android phone
Page 59: Android workshop - 01. Getting started on android phone

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/dog01" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="Eason" android:textSize="28sp" />

<!-- 以下省略 —>

</LinearLayout> </ScrollView></LinearLayout>

activity_main.xml

Page 60: Android workshop - 01. Getting started on android phone

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_marginBottom="15dp" android:orientation="horizontal">

<ImageView android:id="@+id/facebook_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/facebook" android:layout_margin="5dp" />

<ImageView android:id="@+id/gplus_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/gplus" android:layout_margin="5dp" /></LinearLayout>

activity_main.xml

Page 61: Android workshop - 01. Getting started on android phone

Challenge: Open Browser• 利⽤用剛才的 Layout 讓 Facebook 圖⽚片能夠正常被按下

• 提⽰示: • findViewById • setOnClickListener

private void openWebBrowser(String url) { try { Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(i); } catch (Exception e) { e.printStackTrace(); }}

Page 62: Android workshop - 01. Getting started on android phone

Code Examples

http://goo.gl/YlNU1R

PetStar - Exercise02: PetsNameCard

Page 63: Android workshop - 01. Getting started on android phone

Where To Go From Here?

• ListView / BaseAdapter

• Layout orientation changes

• Internet

• Thread / Handler / Asynctask

• Fragment

Page 64: Android workshop - 01. Getting started on android phone

Q & A