編輯:關於 Android 程式設計
圓角矩形是行動介面裡最常見的裝飾元素之一,特別是當按鈕需要「按下時變色」的效果時。與其準備一堆九宮格圖片,不如直接用程式繪製——既銳利又是純向量,任何解析度都不失真。
整個實作分為兩層:先寫一個負責畫圓角矩形的 Drawable,再用 StateListDrawable 把「正常狀態」與「按下狀態」組裝起來。
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.view.MotionEvent;
public class RoundRectDradable extends Drawable {
private static final float DEFAULT_RADIUS = 6.f;
private Paint mPaint = new Paint();
private RoundRectShape mShape;
private float[] mOuter;
private int mColor;
private int mPressColor;
private float mTopLeftRadius = DEFAULT_RADIUS;
private float mTopRightRadius = DEFAULT_RADIUS;
private float mBottomLeftRadius = DEFAULT_RADIUS;
private float mBottomRightRadius = DEFAULT_RADIUS;
public RoundRectDradable() {
mColor = Color.WHITE;
mPressColor = Color.WHITE;
mPaint.setColor(mColor);
mPaint.setAntiAlias(true);
}
public float getTopLeftRadius() { return mTopLeftRadius; }
public void setTopLeftRadius(float topLeftRadius) { this.mTopLeftRadius = topLeftRadius; }
public float getTopRightRadius() { return mTopRightRadius; }
public void setTopRightRadius(float topRightRadius) { this.mTopRightRadius = topRightRadius; }
public float getBottomLeftRadius() { return mBottomLeftRadius; }
public void setBottomLeftRadius(float bottomLeftRadius) { this.mBottomLeftRadius = bottomLeftRadius; }
public float getBottomRightRadius() { return mBottomRightRadius; }
public void setBottomRightRadius(float bottomRightRadius) { this.mBottomRightRadius = bottomRightRadius; }
public int getPressColor() { return mPressColor; }
public void setPressColor(int pressColor) { this.mPressColor = pressColor; }
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
refreshShape();
mShape.resize(bounds.right - bounds.left, bounds.bottom - bounds.top);
}
private void refreshShape() {
mOuter = new float[]{mTopLeftRadius, mTopLeftRadius,
mTopRightRadius, mTopRightRadius,
mBottomLeftRadius, mBottomLeftRadius,
mBottomRightRadius, mBottomLeftRadius};
mShape = new RoundRectShape(mOuter, null, null);
}
public void setColor(int color) {
mColor = color;
mPaint.setColor(color);
}
@Override public void draw(Canvas canvas) { mShape.draw(canvas, mPaint); }
@Override public void setAlpha(int alpha) { mPaint.setAlpha(alpha); }
@Override public void setColorFilter(ColorFilter cf) { mPaint.setColorFilter(cf); }
@Override public int getOpacity() { return mPaint.getAlpha(); }
}
這裡的關鍵是 RoundRectShape:它接受一個長度為 8 的 float 陣列,依序描述左上、右上、左下、右下的橫向與縱向圓角半徑,因此四角可以各自不同。setAntiAlias(true) 則是讓圓角邊緣平滑、不出現鋸齒的必要設定。
import android.graphics.Rect;
import android.graphics.drawable.StateListDrawable;
public class StateRoundRectDrawable extends StateListDrawable {
private static final float DEFAULT_RADIUS = 6.f;
private float mTopLeftRadius = DEFAULT_RADIUS;
private float mTopRightRadius = DEFAULT_RADIUS;
private float mBottomLeftRadius = DEFAULT_RADIUS;
private float mBottomRightRadius = DEFAULT_RADIUS;
private int mNormalColor;
private int mPressedColor;
private RoundRectDradable mNormalDradable;
private RoundRectDradable mPressedDradable;
public StateRoundRectDrawable(int normalCorlor, int pressColor) {
this.mNormalColor = normalCorlor;
this.mPressedColor = pressColor;
}
@Override
protected void onBoundsChange(Rect bounds) {
if (mNormalDradable == null) {
mNormalDradable = new RoundRectDradable();
mNormalDradable.setTopLeftRadius(mTopLeftRadius);
mNormalDradable.setTopRightRadius(mTopRightRadius);
mNormalDradable.setBottomLeftRadius(mBottomLeftRadius);
mNormalDradable.setBottomRightRadius(mBottomRightRadius);
mNormalDradable.setColor(mNormalColor);
mNormalDradable.onBoundsChange(bounds);
}
if (mPressedDradable == null) {
mPressedDradable = new RoundRectDradable();
mPressedDradable.setTopLeftRadius(mTopLeftRadius);
mPressedDradable.setTopRightRadius(mTopRightRadius);
mPressedDradable.setBottomLeftRadius(mBottomLeftRadius);
mPressedDradable.setBottomRightRadius(mBottomRightRadius);
mPressedDradable.setColor(mPressedColor);
mPressedDradable.onBoundsChange(bounds);
}
this.addState(new int[]{-android.R.attr.state_pressed}, mNormalDradable);
this.addState(new int[]{android.R.attr.state_pressed}, mPressedDradable);
}
public float getTopLeftRadius() { return mTopLeftRadius; }
public void setTopLeftRadius(float topLeftRadius) { this.mTopLeftRadius = topLeftRadius; }
public float getTopRightRadius() { return mTopRightRadius; }
public void setTopRightRadius(float topRightRadius) { this.mTopRightRadius = topRightRadius; }
public float getBottomLeftRadius() { return mBottomLeftRadius; }
public void setBottomLeftRadius(float bottomLeftRadius) { this.mBottomLeftRadius = bottomLeftRadius; }
public float getBottomRightRadius() { return mBottomRightRadius; }
public void setBottomRightRadius(float bottomRightRadius) { this.mBottomRightRadius = bottomRightRadius; }
public int getNormalColor() { return mNormalColor; }
public void setNormalColor(int normalColor) { this.mNormalColor = normalColor; }
public int getPressedColor() { return mPressedColor; }
public void setPressedColor(int pressedColor) { this.mPressedColor = pressedColor; }
}
建立 StateRoundRectDrawable 物件並傳入正常色與按下色,然後以 view.setBackground(...) 套用即可。StateListDrawable 的運作原理是比對當前的狀態清單,state_pressed 前面加上負號代表「未被按下」的狀態,兩筆狀態記錄合起來就構成完整的切換邏輯。
另外提醒,類別名稱中的 Dradable 應為 Drawable 的拼字誤差,若在自己的專案中沿用,建議一併更正以免造成誤會。
用 AChartEngine 打造 Android 動態心電圖效果實作分享
AChartEngine 是一款功能強大的圖表引擎,我在求學時期就接觸過,並用它開發過一個感測器應用,現在回想起來仍覺得十分懷念。今天就把這個 App 的原始碼整理出來,
微信發不出語音怎麼辦?從網路到快取資料的排查順序
微信最常用的功能就是語音,一旦發不出去,整個溝通就卡住。這個問題看起來嚴重,但多數情況可以用幾個步驟自己解決。先排除最常見的原因語音傳送失敗,多半來自網路問題:訊號不穩、
Android 屬性動畫入門:從 ObjectAnimator 到自訂 TypeEvaluator
網路上高手的開場白往往先談心境、聊生活,我這裡就省掉鋪墊,直接進主題——因為這東西本來就不難。 動畫的兩套體系 Android 的動畫效果繞不開四件事:位移、縮放、旋轉、
Android 自定義 ViewGroup 實戰:Margin、Gravity、滑動與事件攔截
自定義 ViewGroup 的核心 自定義 ViewGroup 至少要處理兩個問題:如何測量自身大小,以及如何排列子 View。前者通常覆寫 onMeasure,後者覆寫