編輯:關於 Android 程式設計
AndroidAnnotations 是一個相當成熟的框架(專案 Wiki),它把幾件麻煩事一起包辦了:
好用之外,更關鍵的一點是:它對執行效能沒有影響。原因後面會講。
注意這裡宣告的 Activity 名稱,後面都多帶了一個下劃線。
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.hello"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="18" />
<application android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity android:name="com.example.hello.MainActivity_"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.hello.SecondActivity_" />
</application>
</manifest>
這裡的類名反而不帶下劃線,帶下劃線的是編譯期自動產生的子類。
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
@ViewById(R.id.myInput)
EditText myInput;
@ViewById(R.id.myTextView)
TextView textView;
@ViewById(R.id.myButton2)
Button btn2;
@StringRes(R.string.go_to_second)
String btn2Txt;
@AfterViews
protected void afterViews() {
btn2.setText(btn2Txt);
}
@Click
void myButton() {
String name = myInput.getText().toString();
textView.setText("Hello " + name);
}
@Click
void myButton2() {
SecondActivity_.intent(this).start();
}
}
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText android:id="@+id/myInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me!" />
<TextView android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/myButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/go_to_second" />
</LinearLayout>
註解一上,代碼立刻清爽了,那一大堆 findViewById 不見了。SecondActivity 的源碼這裡就不貼了。
-AandroidManifestFile=/path/to/AndroidManifest.xml 參數,明確指出清單檔位置。<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
<compilerArgs>
<arg>-Atrace=true</arg>
<arg>-AlogLevel=trace</arg>
<arg>-AlogConsoleAppender=true</arg>
<arg>-AandroidManifestFile=/path/to/AndroidManifest.xml</arg>
</compilerArgs>
</configuration>
</plugin>
因為它的原理是在編譯期產生 Activity 的子類,而不是執行期反射。看看編譯器幫我們自動生出的類別就很清楚:
public final class MainActivity_ extends MainActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
init_(savedInstanceState); // 處理 @StringRes、@ColorRes 等
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 對應 @EActivity
}
private void init_(Bundle savedInstanceState) {
Resources resources_ = this.getResources();
btn2Txt = resources_.getString(R.string.go_to_second);
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
afterSetContentView_();
}
private void afterSetContentView_() {
// @ViewById
textView = (TextView) findViewById(R.id.myTextView);
myInput = (EditText) findViewById(R.id.myInput);
btn2 = (Button) findViewById(R.id.myButton2);
// @Click
View view = findViewById(R.id.myButton2);
if (view != null) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MainActivity_.this.myButton2();
}
});
}
afterViews(); // 呼叫 @AfterViews 標註的方法
}
public static IntentBuilder_ intent(Context context) {
return new IntentBuilder_(context);
}
public static class IntentBuilder_ {
private final Context context_;
private final Intent intent_;
IntentBuilder_(Context context) {
context_ = context;
intent_ = new Intent(context, MainActivity_.class);
}
public IntentBuilder_ flags(int flags) {
intent_.setFlags(flags);
return this;
}
public void start() {
context_.startActivity(intent_);
}
public void startForResult(int requestCode) {
if (context_ instanceof Activity) {
((Activity) context_).startActivityForResult(intent_, requestCode);
} else {
context_.startActivity(intent_);
}
}
}
}
所有注入與事件綁定都變成了普通的 findViewById 與 setOnClickListener,跟手寫的沒兩樣,自然沒有額外的效能負擔。
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.androidannotations</groupId>
<artifactId>maveneclipse</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>apk</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<android.version>4.1.1.4</android.version>
<androidannotations.version>3.2</androidannotations.version>
<java.version>1.6</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${android.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.androidannotations</groupId>
<artifactId>androidannotations</artifactId>
<version>${androidannotations.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.androidannotations</groupId>
<artifactId>androidannotations-api</artifactId>
<version>${androidannotations.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgs>
<arg>-AlogLevel=trace</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.9.0-rc.3</version>
<configuration>
<sdk><platform>${android.platform}</platform></sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
小提醒:這套寫法屬於 APT 時代的方案,在今日的專案中通常會改用 DataBinding、ViewBinding 或 Hilt 等更現代的工具;但理解「編譯期生成代碼」這個思路,對讀懂那些框架仍然很有幫助。
Android 將圖片加載到內存的實戰示例
本文透過一個完整示例,演示如何在 Android 中把圖片加載到內存並顯示,重點在於避免大圖導致內存溢出。 先來設計界面: 佈局代碼如下: <LinearLay
Android Design 實作筆記(十):FloatingActionButton 與 CollapsingToolbarLayout 的對比
本篇延續上一回的 CoordinatorLayout 實驗,把 FloatingActionButton 與 ImageButton 放在一起做直觀對比,同時加進一個 C
Android 圖片異步加載實作:Handler + 線程池 + LruCache 打造相冊
整體架構 大量圖片載入(例如用 GridView 做手機相冊)通常會用到 LruCache、線程池與任務隊列。異步消息機制在這裡扮演兩個角色: 1. 主線程的 Handl
.NET 工程師轉戰 Android 開發(一):從零開始的環境建置
對沒碰過 Android 的人來說,第一印象往往是「門檻有點高」。但其實只要把環境準備好,後面的路就跟學任何一套新框架一樣:搞懂生命週期、搞懂資源管理、搞懂 UI 與資料