ChartSweepView.java revision f54f435f1f3215b39798c671fc64344d1867de4e
1ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey/* 2ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * Copyright (C) 2011 The Android Open Source Project 3ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * 4ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License"); 5ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * you may not use this file except in compliance with the License. 6ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * You may obtain a copy of the License at 7ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * 8ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * http://www.apache.org/licenses/LICENSE-2.0 9ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * 10ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * Unless required by applicable law or agreed to in writing, software 11ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS, 12ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * See the License for the specific language governing permissions and 14ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * limitations under the License. 15ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey */ 16ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 17ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkeypackage com.android.settings.widget; 18ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 19ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkeyimport android.content.Context; 2052c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkeyimport android.content.res.TypedArray; 21ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkeyimport android.graphics.Canvas; 2252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkeyimport android.graphics.Rect; 2352c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkeyimport android.graphics.drawable.Drawable; 2452c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkeyimport android.util.AttributeSet; 2552c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkeyimport android.util.MathUtils; 26ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkeyimport android.view.MotionEvent; 27ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkeyimport android.view.View; 2852c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkeyimport android.widget.FrameLayout; 29ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 3052c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkeyimport com.android.settings.R; 31ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkeyimport com.google.common.base.Preconditions; 32ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 33ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey/** 34ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * Sweep across a {@link ChartView} at a specific {@link ChartAxis} value, which 35ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey * a user can drag. 36ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey */ 3752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkeypublic class ChartSweepView extends FrameLayout { 38ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 3952c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey // TODO: paint label when requested 40ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 4152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey private Drawable mSweep; 42f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey private Rect mSweepMargins = new Rect(); 43f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey 4452c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey private int mFollowAxis; 4552c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey private boolean mShowLabel; 4652c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 4752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey private ChartAxis mAxis; 48ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey private long mValue; 49ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 5052c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey public static final int HORIZONTAL = 0; 5152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey public static final int VERTICAL = 1; 5252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 53ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey public interface OnSweepListener { 54ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey public void onSweep(ChartSweepView sweep, boolean sweepDone); 55ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 56ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 57ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey private OnSweepListener mListener; 58ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey private MotionEvent mTracking; 59ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 6052c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey public ChartSweepView(Context context) { 6152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey this(context, null, 0); 6252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 63ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 6452c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey public ChartSweepView(Context context, AttributeSet attrs) { 6552c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey this(context, attrs, 0); 6652c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 67ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 6852c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey public ChartSweepView(Context context, AttributeSet attrs, int defStyle) { 6952c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey super(context, attrs, defStyle); 70ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 7152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey final TypedArray a = context.obtainStyledAttributes( 7252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey attrs, R.styleable.ChartSweepView, defStyle, 0); 738a50364a71e7c261b54840210f8bacff5abecb34Jeff Sharkey 7452c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey setSweepDrawable(a.getDrawable(R.styleable.ChartSweepView_sweepDrawable)); 7552c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey setFollowAxis(a.getInt(R.styleable.ChartSweepView_followAxis, -1)); 7652c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey setShowLabel(a.getBoolean(R.styleable.ChartSweepView_showLabel, false)); 7752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 7852c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey a.recycle(); 7952c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 8052c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey setClipToPadding(false); 8152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey setClipChildren(false); 8252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey setWillNotDraw(false); 8352c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 8452c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 8552c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey void init(ChartAxis axis) { 8652c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey mAxis = Preconditions.checkNotNull(axis, "missing axis"); 8752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 88ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 8952c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey public int getFollowAxis() { 9052c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey return mFollowAxis; 9152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 9252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 93f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey /** 94f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey * Return margins of {@link #setSweepDrawable(Drawable)}, indicating how the 95f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey * sweep should be displayed around a content region. 96f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey */ 97f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey public Rect getSweepMargins() { 98f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey return mSweepMargins; 99f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey } 100f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey 101f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey /** 102f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey * Return the number of pixels that the "target" area is inset from the 103f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey * {@link View} edge, along the current {@link #setFollowAxis(int)}. 104f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey */ 105f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey public float getTargetInset() { 106f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey if (mFollowAxis == VERTICAL) { 107f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey final float targetHeight = mSweep.getIntrinsicHeight() - mSweepMargins.top 108f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey - mSweepMargins.bottom; 109f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey return mSweepMargins.top + (targetHeight / 2); 110f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey } else { 111f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey final float targetWidth = mSweep.getIntrinsicWidth() - mSweepMargins.left 112f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey - mSweepMargins.right; 113f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey return mSweepMargins.left + (targetWidth / 2); 114f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey } 115ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 116ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 117ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey public void addOnSweepListener(OnSweepListener listener) { 118ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey mListener = listener; 119ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 120ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 121ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey private void dispatchOnSweep(boolean sweepDone) { 122ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey if (mListener != null) { 123ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey mListener.onSweep(this, sweepDone); 124ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 125ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 126ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 12752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey public void setSweepDrawable(Drawable sweep) { 12852c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey if (mSweep != null) { 12952c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey mSweep.setCallback(null); 13052c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey unscheduleDrawable(mSweep); 13152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 13252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 13352c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey if (sweep != null) { 13452c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey sweep.setCallback(this); 13552c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey if (sweep.isStateful()) { 13652c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey sweep.setState(getDrawableState()); 13752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 13852c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey sweep.setVisible(getVisibility() == VISIBLE, false); 13952c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey mSweep = sweep; 140f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey sweep.getPadding(mSweepMargins); 14152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } else { 14252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey mSweep = null; 14352c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 14452c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 14552c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey invalidate(); 14652c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 14752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 14852c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey public void setFollowAxis(int followAxis) { 14952c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey mFollowAxis = followAxis; 15052c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 15152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 15252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey public void setShowLabel(boolean showLabel) { 15352c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey mShowLabel = showLabel; 15452c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey invalidate(); 15552c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 15652c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 15752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey @Override 15852c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey public void jumpDrawablesToCurrentState() { 15952c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey super.jumpDrawablesToCurrentState(); 16052c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey if (mSweep != null) { 16152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey mSweep.jumpToCurrentState(); 16252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 16352c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 16452c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 16552c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey @Override 16652c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey public void setVisibility(int visibility) { 16752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey super.setVisibility(visibility); 16852c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey if (mSweep != null) { 16952c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey mSweep.setVisible(visibility == VISIBLE, false); 17052c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 17152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 17252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 17352c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey @Override 17452c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey protected boolean verifyDrawable(Drawable who) { 17552c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey return who == mSweep || super.verifyDrawable(who); 17652c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 17752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 178ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey public ChartAxis getAxis() { 179ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey return mAxis; 180ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 181ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 1828a50364a71e7c261b54840210f8bacff5abecb34Jeff Sharkey public void setValue(long value) { 1838a50364a71e7c261b54840210f8bacff5abecb34Jeff Sharkey mValue = value; 1848a50364a71e7c261b54840210f8bacff5abecb34Jeff Sharkey } 1858a50364a71e7c261b54840210f8bacff5abecb34Jeff Sharkey 186ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey public long getValue() { 187ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey return mValue; 188ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 189ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 190ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey public float getPoint() { 191ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey return mAxis.convertToPoint(mValue); 192ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 193ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 194ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey @Override 195ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey public boolean onTouchEvent(MotionEvent event) { 1968a50364a71e7c261b54840210f8bacff5abecb34Jeff Sharkey if (!isEnabled()) return false; 1978a50364a71e7c261b54840210f8bacff5abecb34Jeff Sharkey 198ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey final View parent = (View) getParent(); 199ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey switch (event.getAction()) { 200ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey case MotionEvent.ACTION_DOWN: { 201f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey 202f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey // only start tracking when in sweet spot 203f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey final boolean accept; 204f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey if (mFollowAxis == VERTICAL) { 205f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey accept = event.getX() > getWidth() - (mSweepMargins.right * 2); 206f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey } else { 207f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey accept = event.getY() > getHeight() - (mSweepMargins.bottom * 2); 208f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey } 209f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey 210f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey if (accept) { 211f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey mTracking = event.copy(); 212f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey return true; 213f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey } else { 214f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey return false; 215f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey } 216ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 217ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey case MotionEvent.ACTION_MOVE: { 2188a50364a71e7c261b54840210f8bacff5abecb34Jeff Sharkey getParent().requestDisallowInterceptTouchEvent(true); 2198a50364a71e7c261b54840210f8bacff5abecb34Jeff Sharkey 220f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey final Rect sweepMargins = mSweepMargins; 221f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey 222f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey // content area of parent 223f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey final Rect parentContent = new Rect(parent.getPaddingLeft(), parent.getPaddingTop(), 224f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey parent.getWidth() - parent.getPaddingRight(), 225f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey parent.getHeight() - parent.getPaddingBottom()); 226f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey 22752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey if (mFollowAxis == VERTICAL) { 228f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey final float currentTargetY = getTop() + getTargetInset(); 229f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey final float requestedTargetY = currentTargetY 230f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey + (event.getRawY() - mTracking.getRawY()); 231f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey final float clampedTargetY = MathUtils.constrain( 232f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey requestedTargetY, parentContent.top, parentContent.bottom); 233f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey setTranslationY(clampedTargetY - currentTargetY); 234f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey 235f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey mValue = mAxis.convertToValue(clampedTargetY - parentContent.top); 236ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey dispatchOnSweep(false); 237ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } else { 238f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey final float currentTargetX = getLeft() + getTargetInset(); 239f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey final float requestedTargetX = currentTargetX 240f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey + (event.getRawX() - mTracking.getRawX()); 241f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey final float clampedTargetX = MathUtils.constrain( 242f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey requestedTargetX, parentContent.left, parentContent.right); 243f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey setTranslationX(clampedTargetX - currentTargetX); 244f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey 245f54f435f1f3215b39798c671fc64344d1867de4eJeff Sharkey mValue = mAxis.convertToValue(clampedTargetX - parentContent.left); 246ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey dispatchOnSweep(false); 247ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 248ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey return true; 249ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 250ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey case MotionEvent.ACTION_UP: { 251ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey mTracking = null; 252ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey setTranslationX(0); 253ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey setTranslationY(0); 254ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey requestLayout(); 255ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey dispatchOnSweep(true); 256ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey return true; 257ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 258ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey default: { 259ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey return false; 260ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 261ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 262ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 263ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 264ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey @Override 26552c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey protected void drawableStateChanged() { 26652c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey super.drawableStateChanged(); 26752c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey if (mSweep.isStateful()) { 26852c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey mSweep.setState(getDrawableState()); 26952c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 27052c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey } 27152c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey 27252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey @Override 273ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 27452c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey setMeasuredDimension(mSweep.getIntrinsicWidth(), mSweep.getIntrinsicHeight()); 275ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 276ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 277ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey @Override 278ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey protected void onDraw(Canvas canvas) { 279ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey final int width = getWidth(); 280ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey final int height = getHeight(); 281ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 28252c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey mSweep.setBounds(0, 0, width, height); 28352c3f4461b806e4f1ce48455ee2ba0ac05dfdab4Jeff Sharkey mSweep.draw(canvas); 284ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey } 285ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey 286ab2d8d3a38857b8c155e6c6393c5821f5a341aaeJeff Sharkey} 287