自定义分段Seebar,仿投米App

来源:互联网 发布:杂交水稻种植面积 知乎 编辑:程序博客网 时间:2024/06/07 00:14
package com.zuoyou.invest.investmanage.widget;import android.annotation.TargetApi;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Matrix;import android.graphics.Paint;import android.os.Build;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.WindowManager;import android.widget.FrameLayout;import android.widget.RelativeLayout;import com.zuoyou.invest.investmanage.R;/** * Created by zhoucheng on 2016/8/29. */public class MySeebarPointView extends View {    String TAG = "MySeebarPointView";    private int pointCount = 0;    private int currentPoint;    public MySeebarPointView(Context context) {        super(context);        mContext = context;        init();    }    int bg_width, btn_width, btn_height, bg_height;    Context mContext;    private Bitmap background;    private Bitmap point;    private Bitmap btn;    int screenWidth, screenHeight;    int btn_X = 0;    int Offset;    int moveX, startX, endX;    private MySeebarPointViewLinstener LinstenerCP;    public MySeebarPointView(Context context, AttributeSet attrs) {        super(context, attrs);        mContext = context;        init();    }    public MySeebarPointView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        mContext = context;        init();    }    public interface MySeebarPointViewLinstener{        void getCurrentPoint(int CurrentPoint);    }    public void setMySeebarPointViewLinstener(MySeebarPointViewLinstener l){        this.LinstenerCP=l;    }    public int getPointCount() {        return pointCount;    }    /**     * 设置进度条有多少个白点     *     * @param pointCount     */    public void setPointCount(int pointCount) {        this.pointCount = pointCount;    }    private void init() {        Log.d(TAG, "init");        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);        screenWidth = wm.getDefaultDisplay().getWidth();        screenHeight = wm.getDefaultDisplay().getHeight();        btn = BitmapFactory.decodeResource(getResources(), R.mipmap.mip_seebar_btn);        point = BitmapFactory.decodeResource(getResources(), R.mipmap.mip_seebar_point);        BitmapFactory.Options options = new BitmapFactory.Options();        options.inSampleSize = 2;        background = BitmapFactory.decodeResource(getResources(), R.mipmap.mip_seekbar_b);//        background = BitmapFactory.decodeResource(getResources(), R.mipmap.mip_seebar_bg3);        bg_width = background.getWidth();        bg_height = background.getHeight();        btn_height = btn.getHeight();        btn_width = btn.getWidth();//        background=resizeBitmap(background,screenWidth,screenHeight);        Log.d(TAG, "bg_width=" + bg_width + "   btn_width=" + btn_width + "   screenWidth=" + screenWidth + "background=" + background.getWidth());//        Log.d(TAG,"bg_height="+bg_height+"   btn_height="+btn_height+"   screenHeight="+screenHeight);        Offset = btn_width / 2;        btn_X = Offset;//设置初始btn位置,该点的位置限定至于背景的左右一个半径的距离        currentPoint=1;    }    @Override    public boolean onTouchEvent(MotionEvent event) {//        Log.d(TAG,"onTouchEvent");        int action = event.getAction();        switch (action) {            case MotionEvent.ACTION_DOWN:                startX = (int) event.getX();//                Log.d(TAG,"ACTION_DOWN"+startX);                break;            case MotionEvent.ACTION_MOVE://                Log.d(TAG,"ACTION_MOVE");                moveX = (int) event.getX();                limitView(moveX,false);                break;            case MotionEvent.ACTION_UP:                endX = (int) event.getX();//                Log.d(TAG,"ACTION_UP");                limitView(endX,true);                break;        }        return true;    }    /**     * 当手指移动的时候,判断btn坐标,使其自动移到白点上方     *     * @param x btn最后的坐标     * @param b 是否是手指抬起的事件     */    private void limitView(int x, boolean b) {        int pointDisance = (bg_width - btn_width - btn_width) / (getPointCount() - 1);//线的总宽度除以点的个数减一,得出每个点之间的距离        btn_X = x;        //限制btn移动的位置        if (btn_X < Offset) btn_X = Offset;//btn初始位置        //btn结束位置        if (btn_X > (getPointCount()-1)*pointDisance+Offset)            btn_X = (getPointCount()-1)*pointDisance+Offset;//该点的位置限定至于背景的左右一个半径的距离        //获得当前移动位置的所接近的点,        int i = (btn_X - btn_width) / pointDisance;//计算当前btn移动位置是每个点距离的多少倍        //此时btn的左边有i+1个点        if(pointDisance*i+pointDisance/2>=btn_X- Offset){//靠近左边的点            currentPoint=i+1;        }else{            currentPoint=i+2;        }        if(b){            //如果是手指抬起是,改变btn位置            btn_X=(currentPoint-1)*pointDisance+Offset;        }        changecurrentPoint(currentPoint);        invalidate();    }    private void changecurrentPoint(int point) {        if(LinstenerCP!=null)            LinstenerCP.getCurrentPoint(point);    }    public Bitmap resizeBitmap(Bitmap bitmap, int w, int h) {        if (bitmap != null) {            int newW = (int) (w * 0.9);            float scaleWight = ((float) newW) / bg_width;            Matrix matrix = new Matrix();            matrix.postScale(scaleWight, 1);            Bitmap res = Bitmap.createBitmap(bitmap, 0, 0, bg_width, bg_height, matrix, true);            return res;        } else {            return null;        }    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int mMaxHeight = btn.getHeight();        int mMaxwidth = background.getWidth();//        Log.d(TAG,"btn_height="+mMaxHeight);        int heightMode = MeasureSpec.getMode(heightMeasureSpec);        int heightSize = MeasureSpec.getSize(heightMeasureSpec);        int widthMode = MeasureSpec.getMode(widthMeasureSpec);        int widthSize = MeasureSpec.getSize(widthMeasureSpec);        if (heightMode == MeasureSpec.EXACTLY) {            heightSize = heightSize <= mMaxHeight ? heightSize                    : (int) mMaxHeight;        }        if (heightMode == MeasureSpec.UNSPECIFIED) {            heightSize = heightSize <= mMaxHeight ? heightSize                    : (int) mMaxHeight;        }        if (heightMode == MeasureSpec.AT_MOST) {            heightSize = heightSize <= mMaxHeight ? heightSize                    : (int) mMaxHeight;        }        if (widthMode == MeasureSpec.EXACTLY) {            widthSize = widthSize <= mMaxwidth ? widthSize                    : (int) mMaxwidth;        }        if (widthMode == MeasureSpec.UNSPECIFIED) {            widthSize = widthSize <= mMaxwidth ? widthSize                    : (int) mMaxwidth;        }        if (widthMode == MeasureSpec.AT_MOST) {            widthSize = widthSize <= mMaxwidth ? widthSize                    : (int) mMaxwidth;        }        int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize,                heightMode);        int maxWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize,                widthMode);        super.onMeasure(maxWidthMeasureSpec, maxHeightMeasureSpec);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);//        Log.d(TAG, "onDraw");        int topDisance = btn_height / 2 - bg_height / 2;        canvas.drawBitmap(background, 0, topDisance, new Paint());        //画白点        if (getPointCount() != 0) {            int pointDisance = (bg_width - btn_width - btn_width) / (getPointCount() - 1);//线的总宽度除以点的个数减一,得出每个点之间的距离            for (int i = 0; i < getPointCount(); i++) {                canvas.drawBitmap(point, btn_width + pointDisance * i, topDisance, new Paint());//第一个点的位置,            }        }////        canvas.drawBitmap(point,btn_width,topDisance,new Paint());//第一个点的位置,////        canvas.drawBitmap(point,bg_width/2,topDisance,new Paint());////        canvas.drawBitmap(point,bg_width/2,topDisance,new Paint());//        canvas.drawBitmap(point,bg_width-btn_width,topDisance,new Paint());//最后一个点的位置,        canvas.drawBitmap(btn, btn_X, 0, new Paint());    }}
0 0
原创粉丝点击