編輯:關於 Android 程式設計
Path 是 Android 繪圖裡相當靈活的一塊,而搭配不同的 PathEffect,同一條線條能呈現完全不同的質感。這個範例用同一組座標畫七次,只替換 PathEffect 與顏色,方便直接比較差異。
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.SumPathEffect;
import android.graphics.PathEffect;
import android.os.Bundle;
import android.view.View;
public class PathTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View {
float phase;
PathEffect[] effects = new PathEffect[7];
int[] colors;
private Paint paint;
Path path;
public MyView(Context context) {
super(context);
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4);
// 建立並初始化 Path
path = new Path();
path.moveTo(0, 0);
for (int i = 1; i <= 15; i++) {
// 產生 15 個點,Y 座標隨機,連成一條折線
path.lineTo(i * 20, (float) Math.random() * 60);
}
// 初始化 7 種顏色
colors = new int[] { Color.BLACK, Color.BLUE, Color.CYAN,
Color.GREEN, Color.MAGENTA, Color.RED, Color.YELLOW };
// ---------- 初始化 7 種路徑效果 ----------
effects[0] = null; // 無效果
effects[1] = new CornerPathEffect(10); // 轉角平滑
effects[2] = new DiscretePathEffect(3.0f, 5.0f);// 隨機偏移
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
// DashPathEffect 帶有動畫感
effects[3] = new DashPathEffect(new float[] { 20, 10, 5, 10 }, phase);
// PathDashPathEffect:用一個小方塊沿路徑排列
Path p = new Path();
p.addRect(0, 0, 8, 8, Path.Direction.CCW);
effects[4] = new PathDashPathEffect(p, 12, phase,
PathDashPathEffect.Style.ROTATE);
// ComposePathEffect:先套用 effects[4],再套用 effects[2]
effects[5] = new ComposePathEffect(effects[2], effects[4]);
// SumPathEffect:兩個效果同時作用
effects[6] = new SumPathEffect(effects[4], effects[3]);
// 座標變換:整體位移到 (8, 8) 起繪
canvas.translate(8, 8);
for (int i = 0; i < effects.length; i++) {
paint.setPathEffect(effects[i]);
paint.setColor(colors[i]);
canvas.drawPath(path, paint);
canvas.translate(0, 60); // 每行往下 60
}
// 改變 phase 值形成動畫
phase += 1;
invalidate();
}
}
}
最後在 onDraw 裡 phase += 1 再 invalidate(),虛線與方塊就會持續位移,形成流動的視覺效果——這也是自繪載入動畫最省力的做法之一。
JNI 入門教學:在 Android 中透過 JNI 呼叫 C
一、JNI 是什麼 JNI 是 Java Native Interface 的縮寫,中文稱為 Java 本地呼叫。從 Java 1.1 起,JNI 就成為 Java 平台
Android 開源菜單庫 SlidingMenu 深入剖析
SlidingMenu 簡介 SlidingMenu 是一種較新的側滑式介面設計,在主界面左滑或右滑即可唤出設定或功能頁面,操作直覺便利。目前已有大量應用採用,如 Eve
Android 性能優化:界面流暢度該怎麼量、又該怎麼改
先談前提:流暢度不是一個絕對標準 很多人一提到卡頓就直覺是程式碼寫得爛,但流暢度其實由兩件事共同決定:機器的硬體配置,以及你寫的程式碼。因此在動手優化之前,第一件要想清楚
仿墨跡天氣:在Android中透過自訂zip包換膚
這裡來聊聊墨跡天氣的換膚做法。需要先聲明:以下原理是我透過反編譯並參考網上一些資料推測出來的,僅供參考;如果你有更理想的方式,非常歡迎一起交流。墨跡天氣下載的皮膚本質上就