Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MultiSliderFragment extends MSBaseFragment {
- private final static String TAG = "MultiSliderFragment";
- private MultiSliderView mMSView;
- public MultiSliderFragment() {
- super();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View mMSContainer = inflater.inflate(R.layout.multislider_view, container, false);
- mMSView = (MultiSliderView) mMSContainer.findViewById(R.id.multislider_view);
- Bundle numsBundle = this.getArguments();
- ArrayList<Integer> sliderNums = numsBundle.getIntegerArrayList("nums");
- ArrayList<SliderBar> sliders = new ArrayList<>();
- assert sliderNums != null;
- for (int num : sliderNums) {
- SliderBar bar = new SliderBar(getActivity());
- bar.setNum(String.valueOf(num));
- sliders.add(bar);
- }
- int x = 0;
- for (SliderBar slider : sliders) {
- mMSViewLeft.addView(slider);
- }
- return mMSContainer;
- }
- private void setSliderProps(ArrayList<Integer> sliderNums) {
- MSApplication app = (MSApplication) getActivity().getApplication();
- Point screenDimensions = app.getDimensions();
- mMSView.setScreenDimensions(screenDimensions);
- mMSView.setSliderNums(sliderNums);
- }
- }
- public class MultiSliderView extends LinearLayout {
- final static private String TAG = "MultiSliderView";
- private ArrayList<Integer> sliderNums;
- private Point screenDimensions;
- public MultiSliderView(Context context) {
- super(context);
- init(null, 0);
- }
- public MultiSliderView(Context context, AttributeSet attrs) {
- super(context, attrs);
- init(attrs, 0);
- }
- public MultiSliderView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init(attrs, defStyleAttr);
- }
- private void init(AttributeSet attrs, int defStyleAttr) {
- this.setOrientation(LinearLayout.HORIZONTAL);
- }
- public void setSliderNums(ArrayList<Integer> sliderNums) {
- this.sliderNums = sliderNums;
- }
- public void setScreenDimensions(Point dimensions) {
- this.screenDimensions = dimensions;
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- int desiredWidth = getSuggestedMinimumWidth() + getPaddingLeft() + getPaddingRight();
- int desiredHeight = getSuggestedMinimumHeight() + getPaddingTop() + getPaddingBottom();
- int measureWidth = measureDimension(desiredWidth, widthMeasureSpec);
- int measureHeight = measureDimension(desiredHeight, heightMeasureSpec);
- setMeasuredDimension(measureWidth, measureHeight);
- }
- private int measureDimension(int desiredSize, int measureSpec) {
- int result;
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
- if (specMode == MeasureSpec.EXACTLY) {
- result = specSize;
- } else {
- result = desiredSize;
- if (specMode == MeasureSpec.AT_MOST) {
- result = Math.min(result, specSize);
- }
- }
- if (result < desiredSize) {
- Log.e(TAG, "The view is too small, the content might get cut");
- }
- return result;
- }
- @Override
- protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
- Log.d(TAG, "MultiSliderView on layout: " + left + ", " + top + ", " + right + ", " + bottom);
- int barWidth = getMeasuredWidth()/sliderNums.size();
- int barHeight = getMeasuredHeight();
- int x = 0;
- for (int i = 0; i < getChildCount(); i++) {
- SliderBar child = (SliderBar) getChildAt(i);
- child.layout(x, 0, x + barWidth, barHeight);
- // increment x by barWidth, otherwise bars are laid out
- // on top of each other, each at position 0 within MultiSliderView
- x += barWidth;
- }
- }
- // manual interaction with the multislider (stub)
- // must report to the regarding SliderBar instance to redraw the slider
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- performClick();
- this.getParent().requestDisallowInterceptTouchEvent(true);
- int tempTouchX = (int) event.getX();
- int tempTouchY = (int) event.getY();
- Log.d(TAG, "touch position: " + tempTouchX + ", " + tempTouchY);
- invalidate();
- return true;
- }
- @Override
- public boolean performClick() {
- super.performClick();
- return false;
- }
- public class SliderBar extends View {
- final static String TAG = "SliderBar";
- Paint mPaint;
- Canvas mCanvas;
- String pixelNum;
- Typeface typeFace = Typeface.create("sans-serif-light", Typeface.NORMAL);
- int left, top, right, bottom;
- Rect mArea = new Rect(left, top, right, bottom);
- int touchY;
- public SliderBar(Context context) {
- super(context);
- init(null, 0);
- }
- public SliderBar(Context context, AttributeSet attrs) {
- super(context, attrs);
- init(attrs, 0);
- }
- public SliderBar(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init(attrs, defStyle);
- }
- private void init(AttributeSet attrs, int defStyle) {
- mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- mCanvas = new Canvas();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- Log.d(TAG, "slider bar on draw: " + left + ", " + top + ", " + right + ", " + bottom);
- mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
- mPaint.setColor(0x66000000);
- if (touchY <= top) touchY = top;
- if (touchY > bottom) touchY = bottom;
- canvas.drawRect(left, touchY, right, bottom, mPaint);
- mPaint.setTextAlign(Paint.Align.CENTER);
- mPaint.setTypeface(typeFace);
- mPaint.setTextSize((float) 30);
- mPaint.setColor(0xffffffff);
- canvas.drawText(pixelNum, right/2, bottom - 20, mPaint);
- }
- @Override
- protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
- this.setLeft(left);
- this.setTop(top);
- this.setRight(right);
- this.setBottom(bottom);
- // reports the right values but sliders aren't positioned correctly
- Log.d(TAG, "slider position: " + this.getLeft() + ", " + this.getRight());
- this.left = left;
- this.top = top;
- this.right = right;
- this.bottom = bottom;
- }
- public void setNum(String num) {
- this.pixelNum = num;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/multislider_view"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:animateLayoutChanges="true"
- android:baselineAligned="false">
- <net.myapp.views.MultiSliderView
- android:id="@+id/multislider_view_left"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="10dp"
- android:layout_marginEnd="5dp"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="5dp"
- android:layout_marginStart="10dp"
- android:layout_marginTop="10dp"
- android:background="@android:color/holo_blue_dark"
- android:orientation="horizontal"/>
- </LinearLayout>
Add Comment
Please, Sign In to add comment