編輯:關於 Android 程式設計
時間軸看起來有點花俏,實際上就是 ListView 裡一列又一列的項目擺在一起,加上一個自繪的圓形節點而已。只要盯著「單一項目」看,事情就單純了。
相關影片:
Android自定義控件系列
https://edu.csdn.net/course/detail/3719/65396
Android視頻全系列
https://edu.csdn.net/course/detail/2741/43163

先看單項目的版面:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" > <!-- 線條部分 --> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:paddingLeft="30dp" > <View android:layout_width="3dp" android:layout_height="20dp" android:background="#88000000" /> <com.example.time.TimeView android:src="@drawable/ic_launcher" android:id="@+id/timeView" android:layout_width="40dp" android:layout_height="40dp" /> <View android:layout_width="3dp" android:layout_height="40dp" android:background="#88000000" /> </LinearLayout> <!-- 文字部分 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="30dp" android:paddingRight="30dp" android:paddingTop="20dp" > <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="哈哈哈" android:textColor="#ABABAB" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_time" android:text="時間" android:textColor="#ABABAB" /> </LinearLayout> </LinearLayout>
左欄上下兩段灰色 View 就是那條「線」,中間夾著自定義的 TimeView 作為節點;右欄放內容與時間文字。
接著看 TimeView 怎麼畫:
package com.example.time; import java.util.Random; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; public class TimeView extends View { private Random random; private String time; private Rect mBounds = new Rect(); private int rgb; public TimeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } public TimeView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public TimeView(Context context) { super(context); initView(); } private void initView() { random = new Random(); //定義顏色---這裡純粹為了好玩--大家定義的時候可以在自定義控件外邊定義,將顏色傳遞進來 rgb = Color.rgb(100+random.nextInt(155), 100+random.nextInt(155), random.nextInt(100+155)); } public void setTime(String time) { this.time = time; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(rgb); paint.setAntiAlias(true); paint.setStyle(Style.FILL_AND_STROKE); //先繪制圓 canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, paint); paint = new Paint(); paint.setColor(Color.BLACK); paint.setTextSize(30); paint.getTextBounds(time, 0, time.length(), mBounds); float textWidth = mBounds.width(); float textHeight = mBounds.height(); //再繪制文字 canvas.drawText(time, getWidth() / 2 - textWidth / 2, getHeight() / 2 + textHeight / 2, paint); } }
節點的做法是「填色圓 + 置中文字」,顏色這裡用 Random 隨機產生只是為了看起來熱鬧;正式專案把顏色當作自定義屬性從 XML 傳進來會更合適。
最後在 Activity 裡用 BaseAdapter 餵資料:
public class MainActivity extends Activity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); initData(); listView.setAdapter(new MyBaseAdapter()); } class MyBaseAdapter extends BaseAdapter { @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int arg0) { return dataList.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { View view = View.inflate(MainActivity.this, R.layout.item, null); TextView tv_content = (TextView) view.findViewById(R.id.tv_content); TextView tv_time = (TextView) view.findViewById(R.id.tv_time); TimeView timeView = (TimeView) view.findViewById(R.id.timeView); timeView.setTime(dataList.get(arg0).getTime()); tv_content.setText(dataList.get(arg0).getContent()); tv_time.setText(dataList.get(arg0).getTime()); return view; } } ArrayList<DataBean> dataList = new ArrayList<DataBean>(); private void initData() { for (int i = 0; i < 20; i++) { dataList.add(new DataBean("哈哈哈哈" + i, "25/10")); } } }
這樣時間軸就完成了。若要更精緻,可以讓最後一項的下半段線條消失,做法是依 position 判斷並把下方 View 設為 visibility="invisible"——這類細節通常才是時間軸看起來「對不對勁」的關鍵。
Android Studio 定製輸出自己的 jar 包
眾所周知,Android Studio 在 library 模組被 app 依賴並 build 之後,會自動生成 jar,路徑是 build/intermediates/
Android 利用 Gradle 實現 App 多環境分離
在 Android 專案中,測試環境與正式環境通常需要同時存在。若每次切換都要重新打包、反覆覆蓋安裝,不僅影響開發效率,也容易造成版本混淆。本文將說明如何透過 Gradl
多人即時對戰遊戲的網路同步方案:延遲、預測與作弊檢測
寫在前面:研究生畢業一年半,一直在一家遊戲公司做客戶端研發。這篇講的卻偏服務端,原因有兩個:一是以前懶得寫部落格,學到的東西都堆在筆記本上;二是最近手上的專案和在玩的《王
Android 練功專案:動手做一個 MP3 播放器(上篇)
Android 要掌握的知識點實在不少,唯有持續動手才能內化。前陣子我決定拿 MP3 播放器來練功——這個題目幾乎涵蓋了 Android 開發中相當一部分核心概念:Lis