自定义分段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
- 自定义分段Seebar,仿投米App
- SeeBar自定义样式需要注意的地方,否则会出现闪烁的效果
- iOS-分段跳转-自定义分段View
- 自定义分段Segment的用法
- jquery 文章自定义分段显示
- iOS 自定义滑动分段选择Slider
- 仿ios分段选择器(自定义radiogroup)
- 自定义EditText之电话号码分段显示
- Objective-C 自定义UISlider滑杆 分段样式
- 控件——RatingBar、SeeBar控件
- 分段
- 分段
- 分段
- 自定义一个分段控件(仿QQ顶部的分段控件)
- 自定义一个分段控件(仿QQ顶部的分段控件)tab切换
- EasySegmentedBarView简单易用的自定义分段控件,方便快速实现分段效果
- app中自定义url
- 自定义App.config
- 方差,协方差、标准差,与其意义
- 《从零开始学Swift》学习笔记(Day 24)——枚举
- H5问题总结
- 吐槽 : Bugly 接入报错 arm64-master.o
- SharedPreference数据的读写操作
- 自定义分段Seebar,仿投米App
- Android Studio 快捷键整理
- 新手学习第一课环境搭建
- Base64编码有时会默认换行
- QT出现问题 无法解析的外部符号 未找到文件**.obj
- 欢迎使用CSDN-markdown编辑器
- 晓月_技能栏(二)显示技能和技能冷却
- C/C++之习题16-20
- 关于python协程与异步举例