一、实现效果
滑块在进度条上滑动,不同进度值表示所选择的值,以当前表示的值应用到自己所需应用的方面。
二、自定义控件
public class CustomProgressBarWidthNew extends LinearLayout {
private CustomProgressBarPickerListener mListener;
private Context mContext;
private View mMainView;
// private TextView mTextStart;
// private TextView mTextEnd;
private ImageView mImgBar;
private TextView mTextBarBtn;
private int mProgressBarNowSelected;
private float mItemWidth;
private int mBtnW;
private int mBtnH;
private float mStartMargin;
private int mLineWidth;
private int mMaxProgress;
private int mMinProgress;
public void setOnProgressChangedListener(CustomProgressBarPickerListener listener) {
this.mListener = listener;
}
public CustomProgressBarWidthNew(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
init();
}
public CustomProgressBarWidthNew(Context context) {
super(context);
this.mContext = context;
init();
}
/**
* 初始化布局
*
*/
private void init() {
mMainView = LayoutInflater.from(mContext).inflate(R.layout.view_custom_progressbar_width_new, null);
// mTextStart = (TextView) mMainView.findViewById(R.id.text_customprogressbar_start);
// mTextEnd = (TextView) mMainView.findViewById(R.id.text_customprogressbar_end);
mTextBarBtn = (TextView) mMainView.findViewById(R.id.text_customprogressbar_bar);
mImgBar = (ImageView) mMainView.findViewById(R.id.view_customprogressbar_bar);
mImgBar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
updateBar(ev.getX(), ev.getY());
break;
case MotionEvent.ACTION_MOVE:
updateBar(ev.getX(), ev.getY());
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
if (mListener != null) {
mListener.onCustomProgressBarPicker(mProgressBarNowSelected);
}
break;
default:
break;
}
return true;
}
});
addView(mMainView);
}
private void updateBar(float x, float y) {
float marginl = x - mBtnW;
if (marginl > 0) {
if (marginl - mLineWidth + mBtnW > 0) {
marginl = mLineWidth - mBtnW;
}
android.widget.RelativeLayout.LayoutParams lpbtn = new android.widget.RelativeLayout.LayoutParams(mBtnW, mBtnH);
lpbtn.setMargins((int) (marginl + mStartMargin), 0, 0, 0);
lpbtn.addRule(android.widget.RelativeLayout.CENTER_VERTICAL);
mTextBarBtn.setLayoutParams(lpbtn);
mProgressBarNowSelected = (int) (marginl / mItemWidth) + mMinProgress;
} else {
android.widget.RelativeLayout.LayoutParams lpbtn = new android.widget.RelativeLayout.LayoutParams(mBtnW, mBtnH);
lpbtn.setMargins((int) mStartMargin, 0, 0, 0);
lpbtn.addRule(android.widget.RelativeLayout.CENTER_VERTICAL);
mTextBarBtn.setLayoutParams(lpbtn);
mProgressBarNowSelected = mMinProgress;
}
mTextBarBtn.setText(mProgressBarNowSelected + "");
}
public void show(int maxheight, int width, int minProgress, int maxProgress) {
this.mMaxProgress = maxProgress;
this.mMinProgress = minProgress;
// mTextStart.setText(minProgress + "");
// int textW = (int) getResources().getDimension(R.dimen.width_30);
// int margin = (int) getResources().getDimension(R.dimen.width_10);
int barh = (int) getResources().getDimension(R.dimen.width_30);
barh = barh > maxheight ? maxheight : barh;
int barw = (int) getResources().getDimension(R.dimen.width_30);
int paddingtb = (int) getResources().getDimension(R.dimen.width_5);
// int linew = width - textW * 2 - margin * 2;
int linew = width;
mBtnH = barh;
mBtnW = barw;
// mStartMargin = textW + margin;
mLineWidth = linew;
mItemWidth = (float) (linew - mBtnW) / (float) (maxProgress - minProgress);
// int height = maxheight*60/100;
// int textWH = (int)getResources().getDimension(R.dimen.width_20);
// int textLineH = height/2-textWH;
//
//
// android.widget.RelativeLayout.LayoutParams lpXi = new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT, height);
// mLayoutXi.setLayoutParams(lpXi);
//
//
// android.widget.LinearLayout.LayoutParams lpText = new android.widget.LinearLayout.LayoutParams(textWH, textWH);
// mMainView.findViewById(R.id.text_customprogressbarwidth_xi).setLayoutParams(lpText);
//
// android.widget.LinearLayout.LayoutParams lpTextLine = new android.widget.LinearLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT, textLineH);
// mMainView.findViewById(R.id.text_customprogressbarwidth_xi).setLayoutParams(lpText);
// android.view.ViewGroup.LayoutParams lptextstart = mTextStart.getLayoutParams();
// lptextstart.width = textW + margin;
// lptextstart.height = textW;
// mTextStart.setLayoutParams(lptextstart);
// mTextStart.setPadding(0, 0, margin, 0);
//
// android.view.ViewGroup.LayoutParams lptextend = mTextEnd.getLayoutParams();
// lptextend.width = textW + margin;
// lptextend.height = textW;
// mTextEnd.setLayoutParams(lptextend);
// mTextEnd.setPadding(margin, 0, 0, 0);
// mTextEnd.setText(maxProgress + "");
android.view.ViewGroup.LayoutParams lpmain = mMainView.getLayoutParams();
lpmain.height = maxheight;
lpmain.width = width;
mMainView.setLayoutParams(lpmain);
android.view.ViewGroup.LayoutParams lpBar = mImgBar.getLayoutParams();
lpBar.width = linew;
lpBar.height = barh;
mImgBar.setLayoutParams(lpBar);
mImgBar.setPadding(0, paddingtb, 0, paddingtb);
android.widget.RelativeLayout.LayoutParams lpbtn = new android.widget.RelativeLayout.LayoutParams(mBtnW, mBtnH);
lpbtn.addRule(android.widget.RelativeLayout.CENTER_VERTICAL);
lpbtn.setMargins((int) mStartMargin, 0, 0, 0);
mTextBarBtn.setLayoutParams(lpbtn);
mTextBarBtn.setText(0 + "");
}
public void setProgress(int progressbar) {
if (progressbar < mMinProgress) {
progressbar = mMinProgress;
} else if (progressbar > mMaxProgress) {
progressbar = mMaxProgress;
}
if (progressbar == mMinProgress) {
android.widget.RelativeLayout.LayoutParams lpbtn = new android.widget.RelativeLayout.LayoutParams(mBtnW, mBtnH);
lpbtn.addRule(android.widget.RelativeLayout.CENTER_VERTICAL);
lpbtn.setMargins((int) mStartMargin, 0, 0, 0);
mTextBarBtn.setLayoutParams(lpbtn);
} else if (progressbar == mMaxProgress) {
android.widget.RelativeLayout.LayoutParams lpbtn = new android.widget.RelativeLayout.LayoutParams(mBtnW, mBtnH);
lpbtn.addRule(android.widget.RelativeLayout.CENTER_VERTICAL);
lpbtn.setMargins((int) (mStartMargin + mLineWidth - mBtnW), 0, 0, 0);
mTextBarBtn.setLayoutParams(lpbtn);
} else {
float marginl = mItemWidth * (progressbar - mMinProgress) + mStartMargin;
android.widget.RelativeLayout.LayoutParams lpbtn = new android.widget.RelativeLayout.LayoutParams(mBtnW, mBtnH);
lpbtn.addRule(android.widget.RelativeLayout.CENTER_VERTICAL);
lpbtn.setMargins((int) marginl, 0, 0, 0);
mTextBarBtn.setLayoutParams(lpbtn);
}
mTextBarBtn.setText(progressbar + "");
}
public interface CustomProgressBarPickerListener {
public void onCustomProgressBarPicker(int progress);
}
}
三、效果展示
控件使用:布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
tools:context="com.future.progressloadingdemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="笔触:" />
<com.future.progressloadingdemo.view.CustomProgressBarWidthNew
android:id="@+id/paint_progress_bar"
android:layout_width="match_parent"
android:layout_height="30dp" />
</LinearLayout>
使用代码:
public class MainActivity extends AppCompatActivity {
private CustomProgressBarWidthNew progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (CustomProgressBarWidthNew) findViewById(R.id.paint_progress_bar);
MainApplication context = MainApplication.getInstace();
int widthTemp = context.getWidth();
progressBar.show(90, MainApplication.getInstace().getWidth() * 2 / 3, 30, 100);
// progressBar.show(110, 600, 30, 100);
progressBar.setProgress(50);
progressBar.setOnProgressChangedListener(new CustomProgressBarWidthNew.CustomProgressBarPickerListener() {
@Override
public void onCustomProgressBarPicker(int progress) {
progressBar.setProgress(progress);
}
});
}
}
实现效果:
有问题就是有机会:
①自己的问题,就是我们成长机会;
②公司问题,就是我们晋升机会;
③同事问题,就是我们建立人脉机会;
④客户问题,就是我们销售机会;
⑤老板问题,就是我们赢得信任机会;
⑥竞争对手问题,就是我们变强机会。
生活就是问题与机会的旅程。
有问题就是有机会:
①自己的问题,就是我们成长机会;
②公司问题,就是我们晋升机会;
③同事问题,就是我们建立人脉机会;
④客户问题,就是我们销售机会;
⑤老板问题,就是我们赢得信任机会;
⑥竞争对手问题,就是我们变强机会。
生活就是问题与机会的旅程。