Guest User

Untitled

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. public class MultiSliderFragment extends MSBaseFragment {
  2. private final static String TAG = "MultiSliderFragment";
  3. private MultiSliderView mMSView;
  4.  
  5. public MultiSliderFragment() {
  6. super();
  7. }
  8.  
  9. @Override
  10. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  11. Bundle savedInstanceState) {
  12.  
  13. View mMSContainer = inflater.inflate(R.layout.multislider_view, container, false);
  14. mMSView = (MultiSliderView) mMSContainer.findViewById(R.id.multislider_view);
  15. Bundle numsBundle = this.getArguments();
  16. ArrayList<Integer> sliderNums = numsBundle.getIntegerArrayList("nums");
  17.  
  18. ArrayList<SliderBar> sliders = new ArrayList<>();
  19. assert sliderNums != null;
  20. for (int num : sliderNums) {
  21. SliderBar bar = new SliderBar(getActivity());
  22. bar.setNum(String.valueOf(num));
  23. sliders.add(bar);
  24. }
  25.  
  26. int x = 0;
  27. for (SliderBar slider : sliders) {
  28. mMSViewLeft.addView(slider);
  29. }
  30.  
  31. return mMSContainer;
  32. }
  33.  
  34. private void setSliderProps(ArrayList<Integer> sliderNums) {
  35. MSApplication app = (MSApplication) getActivity().getApplication();
  36. Point screenDimensions = app.getDimensions();
  37. mMSView.setScreenDimensions(screenDimensions);
  38. mMSView.setSliderNums(sliderNums);
  39. }
  40. }
  41.  
  42. public class MultiSliderView extends LinearLayout {
  43. final static private String TAG = "MultiSliderView";
  44. private ArrayList<Integer> sliderNums;
  45. private Point screenDimensions;
  46.  
  47. public MultiSliderView(Context context) {
  48. super(context);
  49. init(null, 0);
  50. }
  51.  
  52. public MultiSliderView(Context context, AttributeSet attrs) {
  53. super(context, attrs);
  54. init(attrs, 0);
  55. }
  56.  
  57. public MultiSliderView(Context context, AttributeSet attrs, int defStyleAttr) {
  58. super(context, attrs, defStyleAttr);
  59. init(attrs, defStyleAttr);
  60. }
  61.  
  62. private void init(AttributeSet attrs, int defStyleAttr) {
  63. this.setOrientation(LinearLayout.HORIZONTAL);
  64. }
  65.  
  66. public void setSliderNums(ArrayList<Integer> sliderNums) {
  67. this.sliderNums = sliderNums;
  68. }
  69.  
  70. public void setScreenDimensions(Point dimensions) {
  71. this.screenDimensions = dimensions;
  72. }
  73.  
  74. @Override
  75. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  76. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  77.  
  78. int desiredWidth = getSuggestedMinimumWidth() + getPaddingLeft() + getPaddingRight();
  79. int desiredHeight = getSuggestedMinimumHeight() + getPaddingTop() + getPaddingBottom();
  80.  
  81. int measureWidth = measureDimension(desiredWidth, widthMeasureSpec);
  82. int measureHeight = measureDimension(desiredHeight, heightMeasureSpec);
  83. setMeasuredDimension(measureWidth, measureHeight);
  84. }
  85.  
  86. private int measureDimension(int desiredSize, int measureSpec) {
  87. int result;
  88. int specMode = MeasureSpec.getMode(measureSpec);
  89. int specSize = MeasureSpec.getSize(measureSpec);
  90.  
  91. if (specMode == MeasureSpec.EXACTLY) {
  92. result = specSize;
  93. } else {
  94. result = desiredSize;
  95. if (specMode == MeasureSpec.AT_MOST) {
  96. result = Math.min(result, specSize);
  97. }
  98. }
  99.  
  100. if (result < desiredSize) {
  101. Log.e(TAG, "The view is too small, the content might get cut");
  102. }
  103. return result;
  104. }
  105.  
  106. @Override
  107. protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  108. Log.d(TAG, "MultiSliderView on layout: " + left + ", " + top + ", " + right + ", " + bottom);
  109. int barWidth = getMeasuredWidth()/sliderNums.size();
  110. int barHeight = getMeasuredHeight();
  111. int x = 0;
  112. for (int i = 0; i < getChildCount(); i++) {
  113. SliderBar child = (SliderBar) getChildAt(i);
  114. child.layout(x, 0, x + barWidth, barHeight);
  115. // increment x by barWidth, otherwise bars are laid out
  116. // on top of each other, each at position 0 within MultiSliderView
  117. x += barWidth;
  118. }
  119. }
  120.  
  121. // manual interaction with the multislider (stub)
  122. // must report to the regarding SliderBar instance to redraw the slider
  123. @Override
  124. public boolean onTouchEvent(MotionEvent event) {
  125. performClick();
  126. this.getParent().requestDisallowInterceptTouchEvent(true);
  127.  
  128. int tempTouchX = (int) event.getX();
  129. int tempTouchY = (int) event.getY();
  130.  
  131. Log.d(TAG, "touch position: " + tempTouchX + ", " + tempTouchY);
  132. invalidate();
  133. return true;
  134. }
  135.  
  136. @Override
  137. public boolean performClick() {
  138. super.performClick();
  139. return false;
  140. }
  141.  
  142. public class SliderBar extends View {
  143.  
  144. final static String TAG = "SliderBar";
  145. Paint mPaint;
  146. Canvas mCanvas;
  147. String pixelNum;
  148. Typeface typeFace = Typeface.create("sans-serif-light", Typeface.NORMAL);
  149. int left, top, right, bottom;
  150. Rect mArea = new Rect(left, top, right, bottom);
  151. int touchY;
  152.  
  153. public SliderBar(Context context) {
  154. super(context);
  155. init(null, 0);
  156. }
  157.  
  158. public SliderBar(Context context, AttributeSet attrs) {
  159. super(context, attrs);
  160. init(attrs, 0);
  161. }
  162.  
  163. public SliderBar(Context context, AttributeSet attrs, int defStyle) {
  164. super(context, attrs, defStyle);
  165. init(attrs, defStyle);
  166. }
  167.  
  168. private void init(AttributeSet attrs, int defStyle) {
  169. mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  170. mCanvas = new Canvas();
  171. }
  172.  
  173. @Override
  174. protected void onDraw(Canvas canvas) {
  175. Log.d(TAG, "slider bar on draw: " + left + ", " + top + ", " + right + ", " + bottom);
  176. mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
  177. mPaint.setColor(0x66000000);
  178. if (touchY <= top) touchY = top;
  179. if (touchY > bottom) touchY = bottom;
  180. canvas.drawRect(left, touchY, right, bottom, mPaint);
  181. mPaint.setTextAlign(Paint.Align.CENTER);
  182. mPaint.setTypeface(typeFace);
  183. mPaint.setTextSize((float) 30);
  184. mPaint.setColor(0xffffffff);
  185. canvas.drawText(pixelNum, right/2, bottom - 20, mPaint);
  186. }
  187.  
  188. @Override
  189. protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  190. this.setLeft(left);
  191. this.setTop(top);
  192. this.setRight(right);
  193. this.setBottom(bottom);
  194. // reports the right values but sliders aren't positioned correctly
  195. Log.d(TAG, "slider position: " + this.getLeft() + ", " + this.getRight());
  196. this.left = left;
  197. this.top = top;
  198. this.right = right;
  199. this.bottom = bottom;
  200. }
  201.  
  202. public void setNum(String num) {
  203. this.pixelNum = num;
  204. }
  205. }
  206.  
  207. <?xml version="1.0" encoding="utf-8"?>
  208. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  209. android:id="@+id/multislider_view"
  210. android:orientation="horizontal"
  211. android:layout_width="match_parent"
  212. android:layout_height="match_parent"
  213. android:animateLayoutChanges="true"
  214. android:baselineAligned="false">
  215.  
  216. <net.myapp.views.MultiSliderView
  217. android:id="@+id/multislider_view_left"
  218. android:layout_width="match_parent"
  219. android:layout_height="match_parent"
  220. android:layout_marginBottom="10dp"
  221. android:layout_marginEnd="5dp"
  222. android:layout_marginLeft="10dp"
  223. android:layout_marginRight="5dp"
  224. android:layout_marginStart="10dp"
  225. android:layout_marginTop="10dp"
  226. android:background="@android:color/holo_blue_dark"
  227. android:orientation="horizontal"/>
  228.  
  229. </LinearLayout>
Add Comment
Please, Sign In to add comment