Android 는 ViewDragHelper 를 사용 하여 QQ 6.0 사 이 드 슬라이딩 인터페이스(1)를 모방 합 니 다.
9248 단어 viewdraghelper옆으로 미끄러지다
이 안의 주요 유형 중 하 나 는 View DragHelper 라 는 것 을 알 고 있 습 니 다.그러면 먼저 이 View DragHelper 류 를 알 아 보 겠 습 니 다.이른바 뱀 을 잡 고 7 촌 을 때 리 는 것 입 니 다.우 리 는 먼저 공식 문서 가 어떻게 소개 되 었 는 지,어떤 특이 한 기능 이 있 는 지 알 아 보 겠 습 니 다.
우선 상속:
java.lang.Object
android.support.v4.widget.ViewDragHelper
직접 부 류 는 Object 입 니 다.
클래스 개술
ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number of useful operations and state tracking for allowing a user to drag and reposition views within their parent ViewGroup.
그 는 사용자 정의 ViewGroup 을 만 드 는 도구 클래스 입 니 다.이 성 은 부모 ViewGroup 에서 의 궤적 과 위 치 를 끌 고 그 릴 수 있 는 유용 한 방법 과 상 태 를 제공 합 니 다.
Nested Classes(끼 워 넣 기 클래스)
ViewDragHelper.Callback
A Callback is used as a communication channel with the ViewDragHelper back to the parent view using it.
하나의 반전 은 View DragHelper 와 그의 아버지 view 의 통신 인터페이스 이다.
공개 정적 방법:
뷰 드 래 곤 헬 퍼 는 create()방법 으로 구 성 된 것 임 을 알 수 있다.이것 은 뒤에서 상세 하 게 소개 할 것 이다.
필요 한 몇 가지 방법 을 살 펴 보 자.
public boolean tryCaptureView(View child, int pointerId) {}
public int getViewHorizontalDragRange(View child) {}
public int clampViewPositionHorizontal(View child, int left, int dx) {}
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {}
public void onViewReleased(View releasedChild, float xvel, float yvel) {}
위의 몇 가지 방법 은 코드 에 상세 한 주석 이 있 을 것 이다.여기 서 우리 가 다시 써 야 할 방법 만 볼 뿐이다.자,이렇게 많은 말 을 했 으 니 우 리 는 먼저 간단 한 것 을 하 자.바로 끌 어 당 길 수 있다 는 것 이다.
첫 번 째 드래그 기능 실현(간단 3 단계 실현)
//1、
mDragHelper = ViewDragHelper.create(this, mCallback);
/**
* 2、
*
* @param ev
* @return
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//
return mDragHelper.shouldInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
try {
//
mDragHelper.processTouchEvent(event);
} catch (Exception e) {
e.printStackTrace();
}
// true, ,
return true;
}
ViewDragHelper.Callback mCallback = new ViewDragHelper.Callback() {
/**
* child
* , ,
* @param child view
* @param pointerId id
* @return true
* child == mLeftContent
* child == mMainContent
*/
@Override
public boolean tryCaptureView(View child, int pointerId) {
// true,
return true;
}
/**
* ( )
*
* @param child view
* @param left oldLeft + dx
* @param dx
* @return
*/
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
// , ,
return left;
}
};
자,첫 번 째 효과 도 는 나 올 수 있 습 니 다.바로 끌 어 당 길 수 있 습 니 다.쉽게 이 루어 지지 않 았 습 니까?하지만 이렇게 임 무 를 맡 기 면 일 하기 싫 은 거 아니 야?자,이제 우리 가 끌 어 당 기 는 위 치 를 컨트롤 해 볼 게.함부로 끌 어 당 겨 서 는 안 돼.................................................
두 번 째 단계,드래그 범 위 를 제어 합 니 다.
드래그 범 위 를 제어 하려 면 먼저 이 두 개의 컨트롤 을 가 져 와 야 합 니 다.이 두 개의 컨트롤 과 관련 된 속성 을 가 져 와 야 조작 할 수 있 습 니 다.그래서 우 리 는 다시 한 번 방법 을 썼 다.
/**
* Finalize inflating a view from XML. This is called as the last phase
* of inflation, after all child views have been added.
* xml ,
* <p>Even if the subclass overrides onFinishInflate, they should always be
* sure to call the super method, so that we get called.
*/
@Override
protected void onFinishInflate() {
super.onFinishInflate();
/**
*
*/
/**
*
*/
mLeftContent = (ViewGroup) getChildAt(0);
/**
* main
*/
mMainContent = (ViewGroup) getChildAt(1);
}
그 다음 에 우 리 는 너비 와 높이 를 얻 을 것 이다.우 리 는 onMessure()에서 얻 을 수 있다 는 것 을 알 고 있 지만 아래 의 이 방법 도 얻 을 수 있다.
/**
*
* This is called during layout when the size of this view has changed
* @param w
* @param h
* @param oldw
* @param oldh
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
/**
*
*/
mHeight = getMeasuredHeight();
mWidth = getMeasuredWidth();
/**
* view
*/
mRange = (int) (mWidth * 0.7);
}
자,우리 가 필요 로 하 는 너비,높이,드래그 거리 에 대해 우 리 는 이미 얻 었 습 니 다.그러면 우 리 는 이제 제한 을 해 야 합 니 다.우 리 는 clampView Position Horizontal()방법 에 이것 만 추가 하면 메 인 패 널 이 왼쪽으로 이동 하지 못 하도록 제한 할 수 있 습 니 다.왼쪽 패 널 은 오른쪽 mRange 위치 로 만 이동 할 수 있 습 니 다.(여기 한번 해 볼 수 있 겠 군)
if (child == mMainContent) {
left = fixLeft(left);
}
/**
*
* view
*
* @param left
* @return
*/
private int fixLeft(int left) {
if (left < 0) {
return 0;
} else if (left > mRange) {
return mRange;
}
return left;
}
이때 우 리 는 기본적으로 대체적으로 프레임 워 크 가 나 왔 지만 문제 가 하나 있다.그것 은 우리 가 미 끄 러 진 효과(오른쪽 이 정 해 졌 지만 우리 가 Left View 를 미 끄 러 뜨 렸 을 때 오른쪽으로 미 끄 러 질 수 있다)를 제한 하 는 방법 을 생각해 야 한 다 는 것 이다.바로 제한 이다.왼쪽 view 에서 저 는 오른쪽 으로 만 mRange 의 거 리 를 미 끄 러 질 수 있 습 니 다.이 럴 때 우 리 는 방법 을 확인 해 야 합 니 다.어떤 방법 으로 변 화 된 position 의 값 을 얻 을 수 있 는 지 확인 한 다음 에 바 꿀 수 있 습 니 다.
/**
* view , , , ,
*
* view
*
* @param changedView view
* @param left
* @param top
* @param dx
* @param dy
*/
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
super.onViewPositionChanged(changedView, left, top, dx, dy);
int newLeft = left;
//
if (changedView == mLeftContent) {
//
newLeft = mMainContent.getLeft() + dx;
}
// ( )
newLeft = fixLeft(newLeft);
if (changedView == mLeftContent) {
// ,
mLeftContent.layout(0, 0, 0 + mWidth, 0 + mHeight);
mMainContent.layout(newLeft, 0, newLeft + mWidth, 0 + mHeight);
}
//
invalidate();
}
자,이때 우리 의 대체적인 효과 가 이미 나 왔 으 니 효과 도 를 보 세 요.여기 중간 에 나 오 는 흰색 은 녹화 문제 에 속 하 므 로 직접 측정 하 는 것 은 문제 가 없다)여기까지 대충 끌 어 당 기 는 것 은 이미 실현 할 수 있 습 니 다.물론 입 니 다.제 현재 구 조 는 바로 아래 의 간단 한 실현 입 니 다.
<?xml version="1.0" encoding="utf-8"?>
<com.example.qqsliding.DragLayout
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"
tools:context="com.example.qqsliding.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/sidebar_bg">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#0CB8F6"
android:gravity="center_vertical">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="15dp"
android:src="@mipmap/icon_avatar_white"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Header"/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:src="@mipmap/icon_search"/>
</RelativeLayout>
</LinearLayout>
</com.example.qqsliding.DragLayout>
그러나 세 심하게 보면 우리 가 끌 어 당 길 때 매우 딱딱 하 다 는 것 을 알 수 있 습 니 다.그것 은 우리 가 애니메이션 효 과 를 추가 하지 않 고 생생 하 게 끌 어 냈 기 때 문 입 니 다.구체 적 으로 애니메이션 을 넣 고 리 셋 효 과 를 정의 합 니 다.본 고 를 통 해 학습 하 십시오Android 슬라이딩 최적화 QQ 6.0 사 이 드 슬라이드 메뉴(2),본 고 를 통 해 공유 하 는 것 이 여러분 에 게 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Android 는 ViewDragHelper 를 사용 하여 QQQ6.X 최신 버 전 사 이 드 슬라이드 인터페이스 효과 인 스 턴 스 코드 를 구현 합 니 다.이 방법 을 실현 하 는 것 은 수평 방향 이 미 끄 러 지 는 것 을 나타 내 는 동시에 방법 에서 경계 값 을 판단 할 것 이다.예 를 들 어 위의 main view 가 왼쪽 으로 경 계 를 이동 한 것 을 제외...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.