设为首页 加入收藏

TOP

Android打造带透明圆弧的ImageView(一)
2017-10-13 09:49:55 】 浏览:7237
Tags:Android 打造 透明 圆弧 ImageView

  这几天因为项目需求,需要在ImageView上面叠加一层透明圆弧,并且在沿着圆弧的方向显示相应的文字,效果如下图所示:

  拿到这个需求,首先想到的是自定义一个ImageView来实现此功能,即在onDraw()中绘制圆弧和文字。同时因为要保证圆弧的位置可以任意摆放,圆弧的颜色、透明度以及文字大小、颜色等都是可控的,所以增加了一些自定义属性。实现代码非常简单,如下:

1.自定义ImageView:

  1 package com.chunk.customviewsdemo.views.ArcImageView;
  2 
  3 import android.content.Context;
  4 import android.content.res.TypedArray;
  5 import android.graphics.Canvas;
  6 import android.graphics.Paint;
  7 import android.graphics.Path;
  8 import android.graphics.RectF;
  9 import android.util.AttributeSet;
 10 import android.widget.ImageView;
 11 
 12 import com.chunk.customviewsdemo.R;
 13 
 14 /**
 15  * Description:A custom ImageView with circular arc and text
 16  * Author: XiaoYu
 17  * Date: 2016/5/10 13:55
 18  */
 19 public class ArcImageView extends ImageView {
 20     /**
 21      * The default text size.
 22      */
 23     private final float DEFAULT_TEXT_SIZE = 20;
 24     /**
 25      * The default scale value which decides the width of arc.
 26      */
 27     private final float DEFAULT_SCALE = 0.5f;
 28     /**
 29      * The default transparency of arc.
 30      */
 31     private final int DEFAULT_ARC_ALPHA =100;
 32     /**
 33      * The default width of arc.
 34      */
 35     private final int DEFAULT_ARC_WIDTH =160;
 36     /**
 37      * The default angle that the arc starts with.
 38      */
 39     private final int DEFAULT_START_ANGLE = 180;
 40     /**
 41      * The default angle that the arc.
 42      */
 43     private final int DEFAULT_SWEEP_ANGLE = 90;
 44     /**
 45      * The default distance along the path to add to the text's starting position.
 46      */
 47     private final int DEFAULT_H_OFFSET = 100;
 48     /**
 49      * The default distance above(-) or below(+) the path to position the text.
 50      */
 51     private final int DEFAULT_V_OFFSET = 20;
 52     private Context mContext;
 53     /**
 54      * The text displayed on ImageView along arc.
 55      */
 56     private String mDrawStr;
 57     /**
 58      * The font size of text.
 59      */
 60     private float mTextSize = DEFAULT_TEXT_SIZE;
 61     /**
 62      * The scale value which decides the width of arc.
 63      */
 64     private float mScale = DEFAULT_SCALE;
 65     /**
 66      * The transparency of arc.
 67      */
 68     private int mArcAlpha = DEFAULT_ARC_ALPHA;
 69     /**
 70      * The width of arc.
 71      */
 72     private int mArcWidth = DEFAULT_ARC_WIDTH;
 73     /**
 74      * The start angle of angle.
 75      */
 76     private int mStartAngle = DEFAULT_START_ANGLE;
 77     /**
 78      * The swept angle of angle.
 79      */
 80     private int mSweepAngle = DEFAULT_SWEEP_ANGLE;
 81     /**
 82      * The default distance along the path to add to the text's starting position.
 83      */
 84     private float mHOffset = DEFAULT_H_OFFSET;
 85     /**
 86      * The default distance above(-) or below(+) the path to position the text.
 87      */
 88     private float mVOffset = DEFAULT_V_OFFSET;
 89     /**
 90      * The style of arc, all styles includes LEFT_TOP, LEFT_BOTTOM, RIGHT_TOP, RIGHT_BOTTOM, CENTER。
 91      * of course, you can add your own style according to your demands.
 92      */
 93     private int mDrawStyle;
 94     /**
 95      * The color of arc.
 96      */
 97     private int mArcColor;
 98     /**
 99      * The color of text.
100      */
101     private int mTextColor;
102 
103     public ArcImageView(Context context) {
104         super(context);
105         this.mContext = context;
106     }
107 
108     public ArcImageView(Context context, AttributeSet attrs) {
109         super(context, attrs);
110         this.mContext = context;
111         obtainAttributes(attrs);
112     }
113 
114     public ArcImageView(Context context, AttributeSet attrs, int defStyleAttr) {
115         super(context, att
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android之Proguard语法 下一篇利用android studio gsonformat插..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目