編輯:關於 Android 程式設計
前陣子因為專案需求,得在 ImageView 上疊一層透明圓弧,還要沿著圓弧方向排上文字,效果如下:

拿到需求,第一個念頭就是自訂一個 ImageView,在 onDraw() 裡畫出圓弧與文字。由於要讓圓弧能任意擺放,且圓弧顏色、透明度、文字大小與顏色都可調整,因此额外加了幾個自訂屬性。實作並不複雜:
package com.chunk.customviewsdemo.views.ArcImageView; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.util.AttributeSet; import android.widget.ImageView; import com.chunk.customviewsdemo.R; public class ArcImageView extends ImageView { private final float DEFAULT_TEXT_SIZE = ; private final float DEFAULT_SCALE = .f; private final int DEFAULT_ARC_ALPHA =; private final int DEFAULT_ARC_WIDTH =; private final int DEFAULT_START_ANGLE = ; private final int DEFAULT_SWEEP_ANGLE = ; private final int DEFAULT_H_OFFSET = ; private final int DEFAULT_V_OFFSET = ; private Context mContext; private String mDrawStr; private float mTextSize = DEFAULT_TEXT_SIZE; private float mScale = DEFAULT_SCALE; private int mArcAlpha = DEFAULT_ARC_ALPHA; private int mArcWidth = DEFAULT_ARC_WIDTH; private int mStartAngle = DEFAULT_START_ANGLE; private int mSweepAngle = DEFAULT_SWEEP_ANGLE; private float mHOffset = DEFAULT_H_OFFSET; private float mVOffset = DEFAULT_V_OFFSET; private int mDrawStyle; private int mArcColor; private int mTextColor; public ArcImageView(Context context) { super(context); this.mContext = context; } public ArcImageView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; obtainAttributes(attrs); } public ArcImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext = context; obtainAttributes(attrs); } public void setDrawStr(String drawStr) { this.mDrawStr = drawStr; invalidate(); } public void setArcAlpha(int mArcAlpha) { this.mArcAlpha = mArcAlpha; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint arcPaint = new Paint(); arcPaint.setStrokeWidth(mArcWidth); arcPaint.setStyle(Paint.Style.STROKE); arcPaint.setColor(mArcColor); arcPaint.setAlpha(mArcAlpha); int width = getWidth(); int height = getHeight(); float radius; if (width > height) { radius = mScale * height; } else { radius = mScale * width; } RectF oval = new RectF(); int center_x = width; int center_y = height; switch (mDrawStyle) { case : center_x = ; center_y = ; mStartAngle = ; mSweepAngle = -; break; case : center_x = ; center_y = height; mStartAngle = ; mSweepAngle = ; break; case : center_x = width; center_y = ; mStartAngle = ; mSweepAngle = -; break; case : center_x = width; center_y = height; mStartAngle = ; mSweepAngle = ; break; case : center_x = width / ; center_y = height / ; mStartAngle = ; mSweepAngle = ; break; } float left = center_x - radius; float top = center_y - radius; float right = center_x + radius; float bottom = center_y + radius; oval.set(left, top, right, bottom); canvas.drawArc(oval, mStartAngle, mSweepAngle, false, arcPaint); Paint textPaint = new Paint(); textPaint.setTextSize(mTextSize); textPaint.setStyle(Paint.Style.FILL); textPaint.setColor(mTextColor); Path path = new Path(); path.addArc(oval, mStartAngle, mSweepAngle); canvas.drawTextOnPath(mDrawStr, path, mHOffset, mVOffset, textPaint); } private void obtainAttributes(AttributeSet attrs) { TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.ArcImageView); mDrawStr = ta.getString(R.styleable.ArcImageView_drawStr); mTextSize = ta.getDimension(R.styleable.ArcImageView_textSize, DEFAULT_TEXT_SIZE); mArcAlpha = ta.getInteger(R.styleable.ArcImageView_arcAlpha, DEFAULT_ARC_ALPHA); mArcWidth = ta.getInteger(R.styleable.ArcImageView_arcWidth, DEFAULT_ARC_WIDTH); mStartAngle = ta.getInteger(R.styleable.ArcImageView_startAngle, DEFAULT_START_ANGLE); mSweepAngle = ta.getInteger(R.styleable.ArcImageView_startAngle, DEFAULT_SWEEP_ANGLE); mHOffset = ta.getInteger(R.styleable.ArcImageView_hOffset, DEFAULT_H_OFFSET); mVOffset = ta.getInteger(R.styleable.ArcImageView_vOffset, DEFAULT_V_OFFSET); mArcColor = ta.getColor(R.styleable.ArcImageView_arcColor, XCCCCCC); mTextColor = ta.getColor(R.styleable.ArcImageView_textColor, XFFFFFF); mDrawStyle = ta.getInt(R.styleable.ArcImageView_drawStyle, ); ta.recycle(); } }
<resources> <declare-styleable name="ArcImageView"> <attr name="drawStr" format="string" /> <attr name="textSize" format="dimension" /> <attr name="arcAlpha" format="integer" /> <attr name="arcWidth" format="integer" /> <attr name="startAngle" format="integer" /> <attr name="sweepAngle" format="integer" /> <attr name="scale" format="float" /> <attr name="hOffset" format="float" /> <attr name="vOffset" format="float" /> <attr name="drawStyle" format="enum"> <enum name="LEFT_TOP" value="" /> <enum name="LEFT_BOTTOM" value="" /> <enum name="RIGHT_TOP" value="" /> <enum name="RIGHT_BOTTOM" value="" /> <enum name="CENTER" value="" /> </attr> <attr name="arcColor" format="color" /> <attr name="textColor" format="color" /> </declare-styleable> </resources>
package com.chunk.customviewsdemo; import android.os.Bundle; import android.support.v.app.AppCompatActivity; import android.view.View; import android.widget.Button; import com.chunk.customviewsdemo.views.ArcImageView.ArcImageView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ArcImageView aiv_one; private ArcImageView aiv_two; private ArcImageView aiv_three; private ArcImageView aiv_four; private Button btn_another_one; private int mGroup = ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); aiv_one = (ArcImageView) findViewById(R.id.aiv_one); aiv_one.setArcAlpha(); aiv_two = (ArcImageView) findViewById(R.id.aiv_two); aiv_two.setArcAlpha(); aiv_three = (ArcImageView) findViewById(R.id.aiv_three); aiv_three.setArcAlpha(); aiv_four = (ArcImageView) findViewById(R.id.aiv_four); aiv_four.setArcAlpha(); btn_another_one = (Button) findViewById(R.id.btn_another_one); btn_another_one.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_another_one: if (mGroup == ) { aiv_one.setDrawStr("蘋果"); aiv_one.setBackgroundResource(R.drawable.apple); aiv_two.setDrawStr("柚子"); aiv_two.setBackgroundResource(R.drawable.pineapple); aiv_three.setDrawStr("香蕉"); aiv_three.setBackgroundResource(R.drawable.banana); aiv_four.setDrawStr("菠蘿"); aiv_four.setBackgroundResource(R.drawable.pineapple); mGroup = ; } else { aiv_one.setDrawStr("牛排"); aiv_one.setBackgroundResource(R.drawable.steak); aiv_two.setDrawStr("海鮮"); aiv_two.setBackgroundResource(R.drawable.seafood); aiv_three.setDrawStr("奶酪"); aiv_three.setBackgroundResource(R.drawable.cheese); aiv_four.setDrawStr("燒烤"); aiv_four.setBackgroundResource(R.drawable.barbecue); mGroup = ; } break; } } }
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:custom="https://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="dp" android:layout_marginBottom="dp" android:orientation="vertical" > <Button android:id="@+id/btn_another_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="換一組" /> <LinearLayout android:layout_width="match_parent" android:layout_height="dp" android:layout_weight="" android:orientation="horizontal" > <RelativeLayout android:layout_width="dp" android:layout_weight="" android:layout_height="match_parent" > <com.chunk.customviewsdemo.views.ArcImageView.ArcImageView android:id="@+id/aiv_one" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/steak" custom:drawStr="牛排" custom:arcAlpha="" custom:arcColor="@color/gray" custom:textColor="@color/black" custom:textSize="sp" /> </RelativeLayout> </LinearLayout> </LinearLayout>
提醒:在版面檔中引入自訂屬性時,記得加上 xmlns:custom="https://schemas.android.com/apk/res-auto" 這一行。
需求搞定,剩下就是搬進實際專案裡。最終效果如下:

總結一下,自訂 View 大致就是透過覆寫 onDraw、onMeasure()、onLayout() 等方法來完成測量與繪製,繪製時常會用到 Canvas、Paint、Bitmap 等類別。測量與繪製的過程,其實就是把現實中的作畫抽象成程式:用物件導向的概念把畫板、畫紙、畫筆與作畫動作一行行描述出來即可。若你對 2D 繪圖還不熟,建議再找些相關資料或書籍補強。
Android Service 基本概念與常見問題整理
Service(服務)是一個應用程式元件,用於在背景執行長時間的操作,本身不提供使用者介面。其他元件可以啟動一個 Service,即使使用者切換到別的 App,它仍會繼續
Android Studio 中正確引入 jar 包的完整步驟
很多新手在 Android Studio 裏引入 jar 包時總是磕磕絆絆,稍不注意就導致引用失敗。下面用幾張圖把正確的操作流程講清楚。 1. 首先,找到你需要的 jar
自定義 View 實戰:基於 SwipeRefreshLayout 打造上拉加載
注:本文 demo 已提交 GitHub,完整代碼地址:https://github.com/wsclwps123/UpLoadSwipeRefreshLayout,感謝
安卓轉蘋果怎麼搬資料?聯絡人、照片與檔案的轉移方法
手機換代速度太快,第一次從 Android 換到 iPhone 的人,最常卡在資料遷移。聯絡人通常有辦法,麻煩的是照片、影片、文件與音樂——這些東西不會自己跟著過來。蘋果