android自定义高级控件(价格标)

最近项目有用到价格标图,就自已定义了一个,现在分享出来,供大家学习了解,好了废话不多说,直接看图吧!

gif_anime.gif

源代码请看:

    package single.electronic.sz.com.pircetagdemo;

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;



    /**
     * Created by men on 2017/11/30.
     * 邮箱:mensheng1990@163.com
     */

    public class SlidingPriceBarView extends View {

private Bitmap gray_bg;//灰色价格标
private Bitmap green_bg;//绿色价格标
private Bitmap greatRound;//大圆
private Bitmap priceNumber;//左侧价格底图
private int price_h;//高价
private int price_d;//低价
//价格区间
private final int price_0 = 0;
private final int price_200 = 200;
private final int price_500 = 500;
private final int price_1000 = 1000;
private final int price_10000 = 10000;
private int bg_width;
private int bg_height;
private float scale;//压缩比例
private int danHeight;//分成四段的长度
private float half_round;
private Paint mPaint;
private float greatRoundX;
private float y_h;
private float y_d;
private int PRICE_PADDING = 15;
private float bg_x;
private boolean isUpTouched=false;
private boolean isDownTouched=false;

public SlidingPriceBarView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
}

private void init(Context context, AttributeSet attrs) {
    gray_bg = BitmapFactory.decodeResource(getResources(), R.drawable.axis_before);
    green_bg = BitmapFactory.decodeResource(getResources(), R.drawable.axis_after);
    greatRound = BitmapFactory.decodeResource(getResources(), R.drawable.btn);
    priceNumber = BitmapFactory.decodeResource(getResources(), R.drawable.bg_number);
    //得到自定义价格
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SlidingPriceBarView);
    price_h = typedArray.getInteger(R.styleable.SlidingPriceBarView_price_h, 1000);
    price_d = typedArray.getInteger(R.styleable.SlidingPriceBarView_price_d, 200);
    mPaint = new Paint();
    mPaint.setColor(Color.BLACK);

    typedArray.recycle();

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    bg_width = gray_bg.getWidth();
    bg_height = gray_bg.getHeight();
    //根据父类给出的尺寸测量出
    int measureHeight = (heightMode == MeasureSpec.EXACTLY) ? heightSize : bg_height;
    measureHeight = Math.max(measureHeight, heightSize);
    int measureWidth = measureHeight * 2 / 3;
    scale = measureHeight / (float) bg_height;
    //分成四段的长度
    danHeight = (bg_height - bg_width) / 4;
    //小圆的半径
    half_round = bg_height * (1 - 0.95f) / 2;
    //设置自己测量的宽高
    setMeasuredDimension(measureWidth, measureHeight);

}


/**
 * @param canvas
 */
@Override
protected void onDraw(Canvas canvas) {
    //保存当前的画布
    canvas.save();
    //对画布进行缩放
    canvas.scale(scale, scale);
    //绘制灰色底图
    bg_x = (this.getWidth() / scale - gray_bg.getWidth()) / 2;
    canvas.drawBitmap(gray_bg, bg_x, 0, mPaint);
    //绘制右侧的数字
    mPaint.setTextSize(20 / scale);
    String[] number = new String[]{
            "不限",
            String.valueOf(price_1000),
            String.valueOf(price_500),
            String.valueOf(price_200),
            String.valueOf(price_0)
    };
    for (int i = 0; i < number.length; i++) {
        int startX = (int) (bg_x * 5 / 4);
        int endX = (int) (i * danHeight + bg_width / 2 - (mPaint.ascent() + mPaint.descent()) / 2);
        canvas.drawText(number[i].toString(), startX, endX, mPaint);
    }
    //画大圆(上下圆)
    greatRoundX = (this.getWidth() / scale - greatRound.getWidth()) / 2;
    y_h = getBtnYLocationByPrice(price_h);
    canvas.drawBitmap(greatRound,greatRoundX,y_h-greatRound.getHeight()/2,mPaint);
    y_d = getBtnYLocationByPrice(price_d);
   canvas.drawBitmap(greatRound,greatRoundX,y_d-greatRound.getHeight()/2,mPaint);
    //画中间的绿色部分
    Rect src=new Rect(0,(int)(y_h+greatRound.getHeight()/2),(int)green_bg.getWidth(),(int)y_d-greatRound.getHeight()/2);
    Rect dst=new Rect((int) bg_x,(int)y_h+greatRound.getHeight()/2,(int)(bg_x +green_bg.getWidth()),(int)y_d-greatRound.getHeight()/2);
    canvas.drawBitmap(green_bg,src,dst,mPaint);


    //画左侧的图标
    Rect rect_h= getRectByMidLine(y_h);
    canvas.drawBitmap(priceNumber,null,rect_h,mPaint);
    Rect rect_d= getRectByMidLine(y_d);
    canvas.drawBitmap(priceNumber,null,rect_d,mPaint);
    //画左侧的数字
    //上边文本坐标
    float text_u_x = (3*rect_h.width()/4 - mPaint.measureText(String.valueOf(price_h)))/2;
    float text_u_y = rect_h.top - mPaint.ascent() + PRICE_PADDING;
    //下边文字坐标
    float text_d_x = (3*rect_d.width()/4 - mPaint.measureText(String.valueOf(price_d)))/2;
    float text_d_y = rect_d.top - mPaint.ascent() + PRICE_PADDING;
    canvas.drawText(String.valueOf(price_h), text_u_x, text_u_y, mPaint);
    canvas.drawText(String.valueOf(price_d), text_d_x, text_d_y, mPaint);
    //恢复之前的保存的画布
    canvas.restore();


    super.onDraw(canvas);
}

private Rect getRectByMidLine(float y) {
    Rect mRect =new Rect();
    mRect.left=0;
    mRect.right=(int)greatRoundX;
    float text_h = mPaint.descent() - mPaint.ascent();
    mRect.top = (int)(y-text_h/2) - PRICE_PADDING ;
    mRect.bottom = (int)(y+text_h/2) + PRICE_PADDING ;
    return mRect;
}


private float getBtnYLocationByPrice(int price) {
    Log.i("ms","price:"+price);
    float y=0;
    if (price < price_0) {
        price = price_0;
    }
    if (price>price_10000){
        price=price_10000;
    }
    if (price>=price_0&&price<price_200){//0-200
        y=bg_height-danHeight*(price)/(price_200-price_0)-half_round;
    }else if (price>=price_200&&price<price_500){//200-500
     y=bg_height-danHeight*(price-price_200)/(price_500-price_200)-danHeight-half_round;
    }else if(price>=price_500&&price<price_1000){//500-1000
     y=bg_height-danHeight*(price-price_500)/(price_1000-price_500)-2*danHeight-half_round;
    }else{//1000-10000
    y=danHeight*(price_10000-price)/(price_10000-price_1000)+half_round;
    }
    return y;
}

private int getPriceByPosition(float y) {

    float half_height = this.getHeight()*(1-0.95f)/2;
    int price;
    if(y<half_height){
        //y>10000
        price = price_10000;
    }else if(y>=half_height&&y<half_height+danHeight){
        //1000~10000
        price = (int) (price_10000 - (price_10000-price_1000)*(y-half_height)/danHeight);
    }else if(y>half_height+danHeight&&y<half_height+2*danHeight){
        //500~1000
        price = (int) (price_1000 - (price_1000-price_500)*(y-half_height-danHeight)/danHeight);
    }else if(y>half_height+2*danHeight&&y<half_height+3*danHeight){
        //200~500
        price = (int) (price_500 - (price_500-price_200)*(y-half_height-2*danHeight)/danHeight);
    }else{
        //0~200
        price = (int) (price_200 - (price_200-price_0)*(y-half_height-3*danHeight)/danHeight);
    }
    if(price<price_0){
        price = price_0;
    }
    return price;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            float downX=event.getX()/scale;
            float downY=event.getY()/scale;
            if (downX>greatRoundX&&downX<greatRoundX+greatRound.getWidth()){
                if (downY>y_h-greatRound.getHeight()/2&&downY<=y_h+greatRound.getHeight()/2){
                    isUpTouched = true;
                    isDownTouched = false;
                }
                if (downY>y_d-greatRound.getHeight()/2&& downY<=y_d+greatRound.getHeight()/2){
                    isUpTouched = false;
                    isDownTouched = true;
                }

            }
            break;
        case MotionEvent.ACTION_MOVE:
            float y2=event.getY()/scale;
            if (isUpTouched){
                price_h=getPriceByPosition(y2);
                if (price_h<=price_d){
                    price_h=price_d;
                }
            }
            if (isDownTouched){
                price_d=getPriceByPosition(y2);
                if (isDownTouched){
                    if (price_d>price_h){
                        price_d=price_h;
                    }
                }
            }
            //开始重新绘制
        invalidate();


            break;
        case MotionEvent.ACTION_UP:
            isDownTouched=false;
            isUpTouched=false;
            break;
    }

    return true;
}

}

从上面的源代码可以看出,重写的onMeasure方法,测量子类的宽高,然后就是绘制,关键的地方我都写了注释,一看就明白!

本人做android开发多年,以后会陆续更新关于android高级UI,NDK开发,性能优化等文章,更多请关注我的微信公众号:谢谢!

android的那点事.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,029评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,395评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,570评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,535评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,650评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,850评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,006评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,747评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,207评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,536评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,683评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,342评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,964评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,772评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,004评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,401评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,566评论 2 349

推荐阅读更多精彩内容