第一次写Android技术博客,有不足之处还望指正。接下来言归正传,之前在MIX滤镜大师上看到一个类似于seekbar的控件,用来调节图像属性,比如,对比度、亮度、饱和度等等。感觉很不错,现在项目中也要用到类似的控件,刚开始在网上找了一下demo,但都不符合要求,于是就自己写了一个。先上一下效果图:
![](https://img-blog.csdn.net/20151013175307447)
![](https://img-blog.csdn.net/20151013175331218)
![](https://img-blog.csdn.net/20151013175349310)
接下来直接上代码:
import java.math.BigDecimal;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class TwoWaysSeekBar extends View {
private static final int CLICK_ON_PRESS = 1;
private static final int CLICK_INVAILD = 0;
private static final int[] STATE_NORMAL = {};
private static final int[] STATE_PRESSED = {android.R.attr.state_pressed,android.R.attr.state_window_focused};
private static final String tag = "TwoWaysSeekBar";
private Drawable notScrollBarBg;
private Drawable hasScrollBarBg;
private Drawable mThumb;
private Drawable mCenterCircle;
private int mSeekBarWidth;
private int mSeekBarHeight;
private int mCenterCircleWidth;
private int mCenterCircleHeight;
private int mThumbWidth;
private int mThumbHeight;
private double mThumbOffset = 0;
private double mDefaultThumbOffSet = 100;
private int mThumbMarginTop = 70;
private int mDistance = 0;
private int mFlag = CLICK_INVAILD;
private OnSeekBarChangeListener mSeekBarChangeListener;
public TwoWaysSeekBar(Context context) {
this(context, null);
}
public TwoWaysSeekBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TwoWaysSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init(){
Resources resources = getResources();
notScrollBarBg = resources.getDrawable(R.drawable.seek_background_line);
hasScrollBarBg = resources.getDrawable(R.drawable.seek_progress_line);
mThumb = resources.getDrawable(R.drawable.seek_adjust_btn);
mCenterCircle = resources.getDrawable(R.drawable.seek_middle_btn);
mSeekBarWidth = notScrollBarBg.getIntrinsicWidth();
mSeekBarHeight = notScrollBarBg.getIntrinsicHeight();
mThumbWidth = mThumb.getIntrinsicWidth();
mThumbHeight = mThumb.getIntrinsicHeight();
mCenterCircleWidth = mCenterCircle.getIntrinsicWidth();
mCenterCircleHeight = mCenterCircle.getIntrinsicHeight();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = measureWidth(widthMeasureSpec);
mSeekBarWidth = width;
mThumbOffset = width/2;
mDistance = width - mThumbWidth;
mThumbOffset = formatDouble(mDefaultThumbOffSet/200*(mDistance) + mThumbWidth/2);
setMeasuredDimension(width, mThumbHeight + mThumbMarginTop+2);
}
private int measureWidth(int measureSpec){
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if(specMode == MeasureSpec.AT_MOST){
} else if (specMode == MeasureSpec.EXACTLY) {
}
return specSize;
}
@SuppressWarnings("unused")
private int measureHeight(int measureSpec){
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
int defaultHeight = 100;
if (specMode == MeasureSpec.AT_MOST) {
}
else if (specMode == MeasureSpec.EXACTLY) {
defaultHeight = specSize;
}
return defaultHeight;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint text_Paint = new Paint();
text_Paint.setTextAlign(Paint.Align.CENTER);
text_Paint.setColor(Color.parseColor("#4e8795"));
text_Paint.setAntiAlias(true);
text_Paint.setAlpha(255);
text_Paint.setTextSize(40);
int aaa = mThumbMarginTop + mThumbHeight/2 - mSeekBarHeight/2;
int bbb = aaa + mSeekBarHeight;
int ccc = mThumbMarginTop + mThumbHeight/2 - mCenterCircleHeight/2;
int ddd = ccc + mCenterCircleHeight;
notScrollBarBg.setBounds(mThumbWidth/2, aaa, mSeekBarWidth-mThumbWidth/2, bbb);
notScrollBarBg.draw(canvas);
if(mThumbOffset > mSeekBarWidth/2){
hasScrollBarBg.setBounds(mSeekBarWidth/2, aaa, (int)mThumbOffset, bbb);
}else if(mThumbOffset < mSeekBarWidth/2){
hasScrollBarBg.setBounds((int)mThumbOffset, aaa, mSeekBarWidth/2, bbb);
}else{
hasScrollBarBg.setBounds((int)mThumbOffset, aaa, mSeekBarWidth/2, bbb);
}
hasScrollBarBg.draw(canvas);
mCenterCircle.setBounds(mSeekBarWidth/2 - mCenterCircleWidth/2, ccc, mSeekBarWidth/2 + mCenterCircleWidth/2, ddd);
mCenterCircle.draw(canvas);
mThumb.setBounds((int)mThumbOffset - mThumbWidth/2,mThumbMarginTop,(int)mThumbOffset + mThumbWidth/2,mThumbMarginTop+mThumbHeight);
mThumb.draw(canvas);
double progress = formatDouble((mThumbOffset - mThumbWidth/2) * 200 / mDistance);
if((int)progress == 100){
progress = 0;
}else if((int)progress > 100){
progress -= 100;
}else if((int)progress < 100){
progress -=100;
}
Log.d(tag, "progress:"+progress);
canvas.drawText((int)progress+"", (int) mThumbOffset - 2 - 2, 50, text_Paint);
if(mSeekBarChangeListener != null){
mSeekBarChangeListener.onProgressChanged(this, progress);
}
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(mSeekBarChangeListener != null){
}
mFlag = getAreaFlag(event);
if(mFlag == CLICK_ON_PRESS){
mThumb.setState(STATE_PRESSED);
mSeekBarChangeListener.onProgressBefore();
}
break;
case MotionEvent.ACTION_MOVE:
if(mFlag == CLICK_ON_PRESS){
if(event.getX() < 0 || event.getX() <= mThumbWidth/2){
mThumbOffset = mThumbWidth/2;
}else if(event.getX() >= mSeekBarWidth - mThumbWidth/2){
mThumbOffset = mDistance + mThumbWidth/2;
}else{
mThumbOffset = formatDouble(event.getX());
}
}
refresh();
break;
case MotionEvent.ACTION_UP:
mThumb.setState(STATE_NORMAL);
if(mSeekBarChangeListener != null){
mSeekBarChangeListener.onProgressAfter();
}
break;
default:
break;
}
return true;
}
public int getAreaFlag(MotionEvent e){
int top = mThumbMarginTop;
int bottom = mThumbMarginTop + mThumbHeight;
if(e.getY() >= top && e.getY() <= bottom && e.getX() >= (mThumbOffset - mThumbWidth/2) && e.getX() <= (mThumbOffset + mThumbWidth/2)){
return CLICK_ON_PRESS;
}else{
return CLICK_INVAILD;
}
}
private void refresh(){
invalidate();
}
public void setProgress(double progress){
this.mDefaultThumbOffSet = progress;
if(progress >= 0){
mThumbOffset = formatDouble((100 + progress)/200*(mDistance))+mThumbWidth/2;
}else if(progress < 0){
mThumbOffset = formatDouble((100 + progress)/200*(mDistance))+mThumbWidth/2;
}
refresh();
}
public void setOnSeekBarChangeListener(OnSeekBarChangeListener mListener){
this.mSeekBarChangeListener = mListener;
}
public interface OnSeekBarChangeListener {
public void onProgressBefore();
public void onProgressChanged(TwoWaysSeekBar seekBar, double progress);
public void onProgressAfter();
}
public static double formatDouble(double mDouble){
BigDecimal bd = new BigDecimal(mDouble);
BigDecimal bd1 = bd.setScale(2,BigDecimal.ROUND_HALF_UP);
mDouble = bd1.doubleValue();
return mDouble;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
怎样引用在这里就不多赘述了,包名+类名就可以了,监听器也有,在activity中可以直接用。不足之处还望大家多给建议。
参考文献:[](http://lanaiver6291.blog.163.com/blog/static/17779831020153131432393/)