Android 사용자 정의 양 방향 진도 바 구현 코드
이렇게:
그러나 공식 컨트롤 에는 단 방향 만 있다.당황 하지 말고 우리 가 직접 그 려 보 자.
진 도 를 그 리 는 것 은 주로 세 가지 측면 이다.1.스타일,2.사이즈,3.조작 감청.
전체 코드 한 번:
주석 은 기본적으로 원 리 를 설명 했다.
package util;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.example.qzd.utildemo.R;
import java.math.BigDecimal;
/**
* ( )
*/
public class SeekRangeBar extends View {
private Context _context;
private static final int CLICK_ON_LOW = 1; //
private static final int CLICK_ON_HIGH = 2; //
private static final int CLICK_IN_LOW_AREA = 3; //
private static final int CLICK_IN_HIGH_AREA = 4; //
private static final int CLICK_OUT_AREA = 5; // view
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 int mThumbMarginTop = 0; // view
private static int mTextViewMarginTop = 0; // view
private Drawable hasScrollBarBg; //
private Drawable notScrollBarBg; //
private Drawable mThumbLow; //
private Drawable mThumbHigh; //
private int mScollBarWidth; // = +
private int mScollBarHeight; //
private int mThumbWidth; //
private double mOffsetLow = 0; //
private double mOffsetHigh = 0; //
private int mDistance=0; //
private int mFlag = CLICK_INVAILD; //
private double defaultScreenLow = 0; //
private double defaultScreenHigh = 100; //
private OnSeekBarChangeListener mBarChangeListener;
private boolean editable=false;//
private int miniGap=5;//AB
private double progressLow;// ( )
private double progressHigh;//
public SeekRangeBar(Context context) {
this(context, null);
}
public SeekRangeBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SeekRangeBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
_context=context;
// ,
notScrollBarBg = ContextCompat.getDrawable(_context,R.mipmap.hp_wbf);
hasScrollBarBg = ContextCompat.getDrawable(_context, R.mipmap.hp_ybf);
mThumbLow = ContextCompat.getDrawable(_context,R.mipmap.hp_a);
mThumbHigh = ContextCompat.getDrawable(_context,R.mipmap.hp_b);
mThumbLow.setState(STATE_NORMAL);
mThumbHigh.setState(STATE_NORMAL);
//
mScollBarHeight = notScrollBarBg.getIntrinsicHeight();
//
mThumbWidth = mThumbLow.getIntrinsicWidth();
}
/**
* view ( onDraw() )
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
mScollBarWidth = width;
if(mDistance==0) {// ( mDistance ), 。 。
mOffsetLow = mThumbWidth / 2;
mOffsetHigh = width - mThumbWidth / 2;
}
mDistance = width - mThumbWidth;
if(defaultScreenLow != 0) {
mOffsetLow = formatInt(defaultScreenLow / 100 * (mDistance)) + mThumbWidth / 2;
}
if(defaultScreenHigh != 100) {
mOffsetHigh = formatInt(defaultScreenHigh / 100 * (mDistance)) + mThumbWidth / 2;
}
setMeasuredDimension(width, mThumbWidth + mThumbMarginTop + 2);
}
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
/**
*
*/
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//
Paint text_Paint = new Paint();
text_Paint.setTextAlign(Paint.Align.CENTER);
text_Paint.setColor(Color.RED);
text_Paint.setTextSize(20);
int top = mThumbMarginTop + mThumbWidth / 2 - mScollBarHeight / 2;
int bottom = top + mScollBarHeight;
// ,
if(editable) {
// ,
notScrollBarBg.setBounds(mThumbWidth / 2, top, mScollBarWidth - mThumbWidth / 2, bottom);
notScrollBarBg.draw(canvas);
// ,
hasScrollBarBg.setBounds((int) mOffsetLow, top, (int) mOffsetHigh, bottom);
hasScrollBarBg.draw(canvas);
}
//
mThumbLow.setBounds((int) (mOffsetLow - mThumbWidth / 2), mThumbMarginTop, (int) (mOffsetLow + mThumbWidth / 2), mThumbWidth + mThumbMarginTop);
mThumbLow.draw(canvas);
//
mThumbHigh.setBounds((int) (mOffsetHigh - mThumbWidth / 2), mThumbMarginTop, (int) (mOffsetHigh + mThumbWidth / 2), mThumbWidth + mThumbMarginTop);
mThumbHigh.draw(canvas);
//
progressLow = formatInt((mOffsetLow - mThumbWidth / 2) * 100 / mDistance);
progressHigh = formatInt((mOffsetHigh - mThumbWidth / 2) * 100 / mDistance);
canvas.drawText((int) progressLow + "", (int) mOffsetLow - 2 - 2, mTextViewMarginTop, text_Paint);
canvas.drawText((int) progressHigh + "", (int) mOffsetHigh - 2, mTextViewMarginTop, text_Paint);
if (mBarChangeListener != null) {
mBarChangeListener.onProgressChanged(this, progressLow, progressHigh);
}
}
//
@Override
public boolean onTouchEvent(MotionEvent e) {
if(!editable) {
return false;
}
if (e.getAction() == MotionEvent.ACTION_DOWN) {
mFlag = getAreaFlag(e);
if (mFlag == CLICK_ON_LOW) {
mThumbLow.setState(STATE_PRESSED);
} else if (mFlag == CLICK_ON_HIGH) {
mThumbHigh.setState(STATE_PRESSED);
} else if (mFlag == CLICK_IN_LOW_AREA) {
mThumbLow.setState(STATE_PRESSED);
mThumbHigh.setState(STATE_NORMAL);
// 0-mThumbWidth/2
if (e.getX() < 0 || e.getX() <= mThumbWidth / 2) {
mOffsetLow = mThumbWidth / 2;
} else if (e.getX() > mScollBarWidth - mThumbWidth / 2) {
mOffsetLow = mThumbWidth / 2 + mDistance;
} else {
mOffsetLow = formatInt(e.getX());
}
} else if (mFlag == CLICK_IN_HIGH_AREA) {
mThumbHigh.setState(STATE_PRESSED);
mThumbLow.setState(STATE_NORMAL);
if (e.getX() >= mScollBarWidth - mThumbWidth / 2) {
mOffsetHigh = mDistance + mThumbWidth / 2;
} else {
mOffsetHigh = formatInt(e.getX());
}
}
//
invalidate();
} else if (e.getAction() == MotionEvent.ACTION_MOVE) {
if (mFlag == CLICK_ON_LOW) {
if (e.getX() < 0 || e.getX() <= mThumbWidth / 2) {
mOffsetLow = mThumbWidth / 2;
} else if (e.getX() >= mScollBarWidth - mThumbWidth / 2) {
mOffsetLow = mThumbWidth / 2 + mDistance;
mOffsetHigh = mOffsetLow;
} else {
mOffsetLow = formatInt(e.getX());
if (mOffsetHigh - mOffsetLow <= 0) {
mOffsetHigh = (mOffsetLow <= mDistance + mThumbWidth / 2) ? (mOffsetLow) : (mDistance + mThumbWidth / 2);
}
}
} else if (mFlag == CLICK_ON_HIGH) {
if (e.getX() < mThumbWidth / 2) {
mOffsetHigh = mThumbWidth / 2;
mOffsetLow = mThumbWidth / 2;
} else if (e.getX() > mScollBarWidth - mThumbWidth / 2) {
mOffsetHigh = mThumbWidth / 2 + mDistance;
} else {
mOffsetHigh = formatInt(e.getX());
if (mOffsetHigh - mOffsetLow <= 0) {
mOffsetLow = (mOffsetHigh >= mThumbWidth / 2) ? (mOffsetHigh) : mThumbWidth / 2;
}
}
}
// , onDraw
invalidate();
} else if (e.getAction() == MotionEvent.ACTION_UP) {
Log.d("LOGCAT","ACTION UP:"+progressHigh+"-"+progressLow);
mThumbLow.setState(STATE_NORMAL);
mThumbHigh.setState(STATE_NORMAL);
if(miniGap>0 && progressHigh<progressLow+miniGap){
progressHigh=progressLow+miniGap;
this.defaultScreenHigh = progressHigh;
mOffsetHigh = formatInt(progressHigh / 100 * (mDistance)) + mThumbWidth / 2;
invalidate();
}
}
return true;
}
/**
* , AB
* @param _b
*/
public void setEditable(boolean _b){
editable=_b;
invalidate();
}
/**
*
*/
public int getAreaFlag(MotionEvent e) {
int top = mThumbMarginTop;
int bottom = mThumbWidth + mThumbMarginTop;
if (e.getY() >= top && e.getY() <= bottom && e.getX() >= (mOffsetLow - mThumbWidth / 2) && e.getX() <= mOffsetLow + mThumbWidth / 2) {
return CLICK_ON_LOW;
} else if (e.getY() >= top && e.getY() <= bottom && e.getX() >= (mOffsetHigh - mThumbWidth / 2) && e.getX() <= (mOffsetHigh + mThumbWidth / 2)) {
return CLICK_ON_HIGH;
} else if (e.getY() >= top
&& e.getY() <= bottom
&& ((e.getX() >= 0 && e.getX() < (mOffsetLow - mThumbWidth / 2)) || ((e.getX() > (mOffsetLow + mThumbWidth / 2))
&& e.getX() <= ((double) mOffsetHigh + mOffsetLow) / 2))) {
return CLICK_IN_LOW_AREA;
} else if (e.getY() >= top && e.getY() <= bottom && (((e.getX() > ((double) mOffsetHigh + mOffsetLow) / 2) && e.getX() < (mOffsetHigh - mThumbWidth / 2)) || (e.getX() > (mOffsetHigh + mThumbWidth / 2) && e.getX() <= mScollBarWidth))) {
return CLICK_IN_HIGH_AREA;
} else if (!(e.getX() >= 0 && e.getX() <= mScollBarWidth && e.getY() >= top && e.getY() <= bottom)) {
return CLICK_OUT_AREA;
} else {
return CLICK_INVAILD;
}
}
/**
*
* @param progressLow
*/
public void setProgressLow(double progressLow) {
this.defaultScreenLow = progressLow;
mOffsetLow = formatInt(progressLow / 100 * (mDistance)) + mThumbWidth / 2;
invalidate();
}
/**
*
* @param progressHigh
*/
public void setProgressHigh(double progressHigh) {
this.defaultScreenHigh = progressHigh;
mOffsetHigh = formatInt(progressHigh / 100 * (mDistance)) + mThumbWidth / 2;
invalidate();
}
/**
*
* @param mListener
*/
public void setOnSeekBarChangeListener(OnSeekBarChangeListener mListener) {
this.mBarChangeListener = mListener;
}
/**
* ,
*/
public interface OnSeekBarChangeListener {
//
public void onProgressChanged(SeekRangeBar seekBar, double progressLow, double progressHigh);
}
/**
*
*/
private int formatInt(double value) {
BigDecimal bd = new BigDecimal(value);
BigDecimal bd1 = bd.setScale(0, BigDecimal.ROUND_HALF_UP);
return bd1.intValue();
}
}
그리고 프로그램 에서 사용 할 수 있 습 니 다.레이아웃 중
<util.SeekRangeBar
android:id="@+id/doubleSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"/>
호출
private SeekRangeBar doubleSeekbar;//
doubleSeekbar = (SeekRangeBar) findViewById(R.id.doubleSeekbar);
//
doubleSeekbar.setOnSeekBarChangeListener(new SeekRangeBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekRangeBar seekBar, double progressLow, double progressHigh) {
Log.d("LOGCAT"," :" + progressLow + " :" + progressHigh);
}
});
관련 GitHub 프로젝트 주소:https://github.com/codeqian/android-class-lib이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.