use of android.view.TouchDelegate in project AndroidStudy by tinggengyan.
the class TouchDelegateActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_touchdelegate);
// Get the parent view
View parentView = findViewById(R.id.parent_layout);
final ImageButton myButton = (ImageButton) findViewById(R.id.touchdelegateButton);
parentView.post(new Runnable() {
// Post in the parent's message queue to make sure the parent lays out its children before you call getHitRect()
@Override
public void run() {
// The bounds for the delegate view (an ImageButton in this example)
Rect delegateArea = new Rect();
myButton.setEnabled(true);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(TouchDelegateActivity.this, "Touch occurred within ImageButton touch region.", Toast.LENGTH_SHORT).show();
}
});
// The hit rectangle for the ImageButton
myButton.getHitRect(delegateArea);
// Extend the touch area of the ImageButton beyond its bounds
// on the right and bottom.
delegateArea.right += 200;
delegateArea.bottom += 200;
// Instantiate a TouchDelegate.
// "delegateArea" is the bounds in local coordinates of
// the containing view to be mapped to the delegate view.
// "myButton" is the child view that should receive motion
// events.
TouchDelegate touchDelegate = new TouchDelegate(delegateArea, myButton);
// within the touch delegate bounds are routed to the child.
if (View.class.isInstance(myButton.getParent())) {
((View) myButton.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}
use of android.view.TouchDelegate in project FABsMenu by jahirfiquitiva.
the class FABsMenu method onLayout.
@SuppressLint("DrawAllocation")
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
switch(expandDirection) {
case EXPAND_UP:
case EXPAND_DOWN:
boolean expandUp = expandDirection == EXPAND_UP;
touchDelegateGroup.clearTouchDelegates();
int addButtonY = expandUp ? b - t - menuButton.getMeasuredHeight() : 0;
if (expandUp)
addButtonY -= menuBottomMargin;
else
addButtonY += menuTopMargin;
// Ensure menuButton is centered on the line where the buttons should be
int buttonsHorizontalCenter = labelsPosition == LABELS_ON_LEFT_SIDE ? r - l - maxButtonWidth / 2 : maxButtonWidth / 2;
buttonsHorizontalCenter -= labelsPosition == LABELS_ON_LEFT_SIDE ? menuRightMargin : -menuLeftMargin;
int addButtonLeft = buttonsHorizontalCenter - menuButton.getMeasuredWidth() / 2;
menuButton.layout(addButtonLeft, addButtonY, addButtonLeft + menuButton.getMeasuredWidth(), addButtonY + menuButton.getMeasuredHeight());
int labelsOffset = maxButtonWidth / 2 + labelsMargin;
int labelsXNearButton = labelsPosition == LABELS_ON_LEFT_SIDE ? buttonsHorizontalCenter - labelsOffset : buttonsHorizontalCenter + labelsOffset;
int nextY = expandUp ? addButtonY - buttonSpacing : addButtonY + menuButton.getMeasuredHeight() + buttonSpacing;
for (int i = buttonsCount - 1; i >= 0; i--) {
final View child = getChildAt(i);
if (child.equals(menuButton) || child.getVisibility() == GONE)
continue;
int childX = buttonsHorizontalCenter - child.getMeasuredWidth() / 2;
int childY = expandUp ? nextY - child.getMeasuredHeight() : nextY;
child.layout(childX, childY, childX + child.getMeasuredWidth(), childY + child.getMeasuredHeight());
float collapsedTranslation = addButtonY - childY;
float expandedTranslation = 0f;
child.setTranslationY(expanded ? expandedTranslation : collapsedTranslation);
child.setAlpha(expanded ? 1f : 0f);
LayoutParams params = (LayoutParams) child.getLayoutParams();
params.mCollapseDir.setFloatValues(expandedTranslation, collapsedTranslation);
params.mExpandDir.setFloatValues(collapsedTranslation, expandedTranslation);
params.setAnimationsTarget(child);
View label = (View) child.getTag(R.id.fab_label);
if (label != null) {
int labelXAwayFromButton = labelsPosition == LABELS_ON_LEFT_SIDE ? labelsXNearButton - label.getMeasuredWidth() : labelsXNearButton + label.getMeasuredWidth();
int labelLeft = labelsPosition == LABELS_ON_LEFT_SIDE ? labelXAwayFromButton : labelsXNearButton;
int labelRight = labelsPosition == LABELS_ON_LEFT_SIDE ? labelsXNearButton : labelXAwayFromButton;
int labelTop = childY - labelsVerticalOffset + (child.getMeasuredHeight() - label.getMeasuredHeight()) / 2;
label.layout(labelLeft, labelTop, labelRight, labelTop + label.getMeasuredHeight());
Rect touchArea = new Rect(Math.min(childX, labelLeft), childY - buttonSpacing / 2, Math.max(childX + child.getMeasuredWidth(), labelRight), childY + child.getMeasuredHeight() + buttonSpacing / 2);
touchDelegateGroup.addTouchDelegate(new TouchDelegate(touchArea, child));
label.setTranslationY(expanded ? expandedTranslation : collapsedTranslation);
label.setAlpha(expanded ? 1f : 0f);
LayoutParams labelParams = (LayoutParams) label.getLayoutParams();
labelParams.mCollapseDir.setFloatValues(expandedTranslation, collapsedTranslation);
labelParams.mExpandDir.setFloatValues(collapsedTranslation, expandedTranslation);
labelParams.setAnimationsTarget(label);
}
nextY = expandUp ? childY - buttonSpacing : childY + child.getMeasuredHeight() + buttonSpacing;
}
break;
case EXPAND_LEFT:
case EXPAND_RIGHT:
boolean expandLeft = expandDirection == EXPAND_LEFT;
int addButtonX = expandLeft ? r - l - menuButton.getMeasuredWidth() : 0;
if (expandLeft)
addButtonX -= menuRightMargin;
else
addButtonX += menuLeftMargin;
// Ensure menuButton is centered on the line where the buttons should be
int addButtonTop = b - t - maxButtonHeight + (maxButtonHeight - menuButton.getMeasuredHeight()) / 2;
addButtonTop -= menuBottomMargin;
menuButton.layout(addButtonX, addButtonTop, addButtonX + menuButton.getMeasuredWidth(), addButtonTop + menuButton.getMeasuredHeight());
int nextX = expandLeft ? addButtonX - buttonSpacing : addButtonX + menuButton.getMeasuredWidth() + buttonSpacing;
for (int i = buttonsCount - 1; i >= 0; i--) {
final View child = getChildAt(i);
if (child.equals(menuButton) || child.getVisibility() == GONE)
continue;
int childX = expandLeft ? nextX - child.getMeasuredWidth() : nextX;
int childY = addButtonTop + (menuButton.getMeasuredHeight() - child.getMeasuredHeight()) / 2;
child.layout(childX, childY, childX + child.getMeasuredWidth(), childY + child.getMeasuredHeight());
float collapsedTranslation = addButtonX - childX;
float expandedTranslation = 0f;
child.setTranslationX(expanded ? expandedTranslation : collapsedTranslation);
child.setAlpha(expanded ? 1f : 0f);
LayoutParams params = (LayoutParams) child.getLayoutParams();
params.mCollapseDir.setFloatValues(expandedTranslation, collapsedTranslation);
params.mExpandDir.setFloatValues(collapsedTranslation, expandedTranslation);
params.setAnimationsTarget(child);
nextX = expandLeft ? childX - buttonSpacing : childX + child.getMeasuredWidth() + buttonSpacing;
}
break;
default:
// Do Nothing
break;
}
}
use of android.view.TouchDelegate in project FABsMenu by jahirfiquitiva.
the class TouchDelegateGroup method onTouchEvent.
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (!mEnabled)
return false;
TouchDelegate delegate = null;
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
for (int i = 0; i < mTouchDelegates.size(); i++) {
TouchDelegate touchDelegate = mTouchDelegates.get(i);
if (touchDelegate.onTouchEvent(event)) {
mCurrentTouchDelegate = touchDelegate;
return true;
}
}
break;
case MotionEvent.ACTION_MOVE:
delegate = mCurrentTouchDelegate;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
delegate = mCurrentTouchDelegate;
mCurrentTouchDelegate = null;
break;
default:
// Do Nothing
break;
}
return delegate != null && delegate.onTouchEvent(event);
}
use of android.view.TouchDelegate in project robolectric by robolectric.
the class ShadowTouchDelegateTest method setUp.
@Before
public void setUp() throws Exception {
rect = new Rect(1, 2, 3, 4);
view = new View(ApplicationProvider.getApplicationContext());
TouchDelegate realTD = new TouchDelegate(rect, view);
td = Shadows.shadowOf(realTD);
}
use of android.view.TouchDelegate in project PhotoNoter by yydcdut.
the class FloatingActionsMenu method onLayout.
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
switch(mExpandDirection) {
case EXPAND_UP:
case EXPAND_DOWN:
boolean expandUp = mExpandDirection == EXPAND_UP;
if (changed) {
mTouchDelegateGroup.clearTouchDelegates();
}
int addButtonY = expandUp ? b - t - mAddButton.getMeasuredHeight() : 0;
// Ensure mAddButton is centered on the line where the buttons should be
int buttonsHorizontalCenter = mLabelsPosition == LABELS_ON_LEFT_SIDE ? r - l - mMaxButtonWidth / 2 : mMaxButtonWidth / 2;
int addButtonLeft = buttonsHorizontalCenter - mAddButton.getMeasuredWidth() / 2;
mAddButton.layout(addButtonLeft, addButtonY, addButtonLeft + mAddButton.getMeasuredWidth(), addButtonY + mAddButton.getMeasuredHeight());
int labelsOffset = mMaxButtonWidth / 2 + mLabelsMargin;
int labelsXNearButton = mLabelsPosition == LABELS_ON_LEFT_SIDE ? buttonsHorizontalCenter - labelsOffset : buttonsHorizontalCenter + labelsOffset;
int nextY = expandUp ? addButtonY - mButtonSpacing : addButtonY + mAddButton.getMeasuredHeight() + mButtonSpacing;
for (int i = mButtonsCount - 1; i >= 0; i--) {
final View child = getChildAt(i);
if (child == mAddButton || child.getVisibility() == GONE) {
continue;
}
int childX = buttonsHorizontalCenter - child.getMeasuredWidth() / 2;
int childY = expandUp ? nextY - child.getMeasuredHeight() : nextY;
child.layout(childX, childY, childX + child.getMeasuredWidth(), childY + child.getMeasuredHeight());
float collapsedTranslation = addButtonY - childY;
float expandedTranslation = 0f;
child.setTranslationY(mExpanded ? expandedTranslation : collapsedTranslation);
child.setAlpha(mExpanded ? 1f : 0f);
LayoutParams params = (LayoutParams) child.getLayoutParams();
params.mCollapseDir.setFloatValues(expandedTranslation, collapsedTranslation);
params.mExpandDir.setFloatValues(collapsedTranslation, expandedTranslation);
params.setAnimationsTarget(child);
View label = (View) child.getTag(R.id.fab_label);
if (label != null) {
int labelXAwayFromButton = mLabelsPosition == LABELS_ON_LEFT_SIDE ? labelsXNearButton - label.getMeasuredWidth() : labelsXNearButton + label.getMeasuredWidth();
int labelLeft = mLabelsPosition == LABELS_ON_LEFT_SIDE ? labelXAwayFromButton : labelsXNearButton;
int labelRight = mLabelsPosition == LABELS_ON_LEFT_SIDE ? labelsXNearButton : labelXAwayFromButton;
int labelTop = childY - mLabelsVerticalOffset + (child.getMeasuredHeight() - label.getMeasuredHeight()) / 2;
label.layout(labelLeft, labelTop, labelRight, labelTop + label.getMeasuredHeight());
Rect touchArea = new Rect(Math.min(childX, labelLeft), childY - mButtonSpacing / 2, Math.max(childX + child.getMeasuredWidth(), labelRight), childY + child.getMeasuredHeight() + mButtonSpacing / 2);
mTouchDelegateGroup.addTouchDelegate(new TouchDelegate(touchArea, child));
label.setTranslationY(mExpanded ? expandedTranslation : collapsedTranslation);
label.setAlpha(mExpanded ? 1f : 0f);
LayoutParams labelParams = (LayoutParams) label.getLayoutParams();
labelParams.mCollapseDir.setFloatValues(expandedTranslation, collapsedTranslation);
labelParams.mExpandDir.setFloatValues(collapsedTranslation, expandedTranslation);
labelParams.setAnimationsTarget(label);
}
nextY = expandUp ? childY - mButtonSpacing : childY + child.getMeasuredHeight() + mButtonSpacing;
}
break;
case EXPAND_LEFT:
case EXPAND_RIGHT:
boolean expandLeft = mExpandDirection == EXPAND_LEFT;
int addButtonX = expandLeft ? r - l - mAddButton.getMeasuredWidth() : 0;
// Ensure mAddButton is centered on the line where the buttons should be
int addButtonTop = b - t - mMaxButtonHeight + (mMaxButtonHeight - mAddButton.getMeasuredHeight()) / 2;
mAddButton.layout(addButtonX, addButtonTop, addButtonX + mAddButton.getMeasuredWidth(), addButtonTop + mAddButton.getMeasuredHeight());
int nextX = expandLeft ? addButtonX - mButtonSpacing : addButtonX + mAddButton.getMeasuredWidth() + mButtonSpacing;
for (int i = mButtonsCount - 1; i >= 0; i--) {
final View child = getChildAt(i);
if (child == mAddButton || child.getVisibility() == GONE) {
continue;
}
int childX = expandLeft ? nextX - child.getMeasuredWidth() : nextX;
int childY = addButtonTop + (mAddButton.getMeasuredHeight() - child.getMeasuredHeight()) / 2;
child.layout(childX, childY, childX + child.getMeasuredWidth(), childY + child.getMeasuredHeight());
float collapsedTranslation = addButtonX - childX;
float expandedTranslation = 0f;
child.setTranslationX(mExpanded ? expandedTranslation : collapsedTranslation);
child.setAlpha(mExpanded ? 1f : 0f);
LayoutParams params = (LayoutParams) child.getLayoutParams();
params.mCollapseDir.setFloatValues(expandedTranslation, collapsedTranslation);
params.mExpandDir.setFloatValues(collapsedTranslation, expandedTranslation);
params.setAnimationsTarget(child);
nextX = expandLeft ? childX - mButtonSpacing : childX + child.getMeasuredWidth() + mButtonSpacing;
}
break;
}
}
Aggregations