Android 사용자 정의 LineLayout 가 전체 화면 에서 임의의 드래그 기능 을 실현 하 는 예시 코드
6489 단어 Android사용자 정의 LineLayout드래그
개발 에 있어 서 컨트롤 이 화면 에서 마음대로 드래그 되 는 것 을 실현 해 야 합 니 다.이 는 사용자 정의 View 가 필요 합 니 다.그리고 OnTouch Event 이벤트 에서 Motion Event.ACTION 을 처리 해 야 합 니 다.MOVE 이벤트,그리고 좌표 점 을 통 해 onlayout 방법 에 값 을 전달 하여 컨트롤 의 임 의 드래그 를 실현 합 니 다.구체 적 인 코드 는 다음 과 같 습 니 다.
import android.content.Context;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.LinearLayout;
public class DragLineLayout extends LinearLayout {
private int mWidth;
private int mHeight;
private int mScreenWidth;
private int mScreenHeight;
private Context mContext;
private onLocationListener mLocationListener;/*listen to the Rect */
//
private boolean isDrag = false;
public boolean isDrag() {
return isDrag;
}
public DragView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
mScreenWidth = getScreenWidth(mContext);
mScreenHeight = getScreenHeight(mContext) - getStatusBarHeight();
}
public int getStatusBarHeight() {
int resourceId = mContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
return mContext.getResources().getDimensionPixelSize(resourceId);
}
public int getScreenWidth(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
return display.getWidth();
}
public int getScreenHeight(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
return display.getHeight();
}
private float mDownX;
private float mDownY;
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (this.isEnabled()) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isDrag = false;
mDownX = event.getX();
mDownY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
final float mXdistance = event.getX() - mDownX;
final float mYdistance = event.getY() - mDownY;
int l, r, t, b;
// 10,
if (Math.abs(mXdistance) > 10 || Math.abs(mYdistance) > 10) {
isDrag = true;
l = (int) (getLeft() + mXdistance);
r = l + mWidth;
t = (int) (getTop() + mYdistance);
b = t + mHeight;
// ,
if (l < 0) {
l = 0;
r = l + mWidth;
} else if (r > mScreenWidth) {
r = mScreenWidth;
l = r - mWidth;
}
if (t < 0) {
t = 0;
b = t + mHeight;
} else if (b > mScreenHeight) {
b = mScreenHeight;
t = b - mHeight;
}
//
if(mLocationListener!=null){
mLocationListener.locationRect((l+r)/2,(t+b)/2);
}
this.layout(l, t, r, b);
}
break;
case MotionEvent.ACTION_UP:
setPressed(false);
break;
case MotionEvent.ACTION_CANCEL:
setPressed(false);
break;
}
return true;
}
return false;
}
public void setLocationListener(onLocationListener LocationListener) {
this.mLocationListener = LocationListener;
}
public interface onLocationListener {
void locationRect(float locationX, float locationY);
}
}
2.코드 에서 의 활용
<com.xinrui.guestservice.view.DragLineLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/dp_200"
android:layout_height="@dimen/dp_110"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50">
<EditText
android:id="@+id/input_edt"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
android:background="@drawable/edit_bg" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_55"
android:layout_marginTop="@dimen/margin_5"
android:background="@drawable/paint_bg">
<TextView
android:id="@+id/paint_typeface"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="@dimen/margin_5"
android:background="@drawable/main_selector_write"
android:clickable="true" />
<TextView
android:id="@+id/paint_fontsize"
android:layout_width="@dimen/dp_50"
android:layout_height="@dimen/dp_50"
android:layout_alignParentTop="true"
android:layout_marginLeft="@dimen/dp_10"
android:layout_marginTop="@dimen/margin_5"
android:layout_toRightOf="@id/paint_typeface"
android:background="@drawable/main_selector_write"
android:clickable="true" />
</RelativeLayout>
</com.xinrui.guestservice.view.DragLineLayout>
3.이렇게 하면 Activity 에서 이 xml 를 불 러 와 서 임의의 드래그 기능 을 실현 할 수 있 습 니 다.총결산
안 드 로 이 드 사용자 정의 LineLayout 가 전체 화면 에서 임의의 드래그 기능 을 실현 하 는 예제 코드 에 관 한 글 은 여기까지 소개 되 었 습 니 다.더 많은 안 드 로 이 드 사용자 정의 LineLayout 가 전체 화면 에서 임의의 드래그 내용 을 실현 하 는 것 에 대해 서 는 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 지원 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.