티스토리 뷰

android

드래그 해서 뷰 크기 조절하기

노명규 2018. 9. 14. 16:50


구글링을 못하는 건지 아무리 검색해봐도 찾지 못해서 직접 만들었다.




private PointF mCenter;
private float mRadius;
private float mMaxDist;

btn2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Rect rect = new Rect();
v.getGlobalVisibleRect(rect);

mRadius = btn2.getWidth() / 2;
mMaxDist = btn2.getHeight() / 2;
float x = rect.left + mRadius;
float y = rect.top + mMaxDist;
mCenter = new PointF(x, y);

float a = event.getX();
float b = event.getY();
float c = event.getRawX();
float d = event.getRawY();

if ( mCenter.x > event.getRawX() ) {
a = mCenter.x - c + btn2.getWidth() / 2;
}
if ( mCenter.y > event.getRawY() ) {
b = mCenter.y - d + btn2.getHeight() / 2;
}

if ( a > b ){
b = a;
}else {
a = b;
}

btn2.setLayoutParams(new RelativeLayout.LayoutParams((int)a,(int)b ));

if (event.getAction() == event.ACTION_UP){
btn2.setLayoutParams(new RelativeLayout.LayoutParams(300,300 ));
}



return false;
}
});

<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignTop="@+id/btn3"
android:layout_centerHorizontal="true">

<RelativeLayout
android:id="@+id/abcde"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">

<Button
android:id="@+id/btn2"
android:layout_width="50dp"
android:layout_height="50dp" />


</RelativeLayout>
</RelativeLayout>