├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── styles.xml
│ │ │ │ └── colors.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── drawable-xhdpi
│ │ │ │ ├── stretch_right.png
│ │ │ │ └── stretch_bottom.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── drawable
│ │ │ │ ├── bg_view_select.xml
│ │ │ │ ├── bg_dottedline.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── layout
│ │ │ │ └── activity_main.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── dragscaleview
│ │ │ ├── MainActivity.java
│ │ │ └── view
│ │ │ └── DragRectView.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── dragscaleview
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── dragscaleview
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
└── README.md
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DragScaleView
3 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/stretch_right.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/drawable-xhdpi/stretch_right.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/stretch_bottom.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/drawable-xhdpi/stretch_bottom.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/androideoe/DragScaleView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DragScaleView
2 | 一个可以自由拖拽、边缘拉伸的view
3 | 
4 | 博客地址:[Android 自定义View 一个可以自由拖拽、边缘拉伸的view](https://blog.csdn.net/sinat_41268473/article/details/107138229)
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 1dp
5 | 2dp
6 | 3dp
7 | 10dp
8 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bg_view_select.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #6200EE
4 | #3700B3
5 | #03DAC5
6 |
7 |
8 | #00000000
9 |
10 | #0497ff
11 |
12 | #FFF4F5F6
13 |
14 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bg_dottedline.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/test/java/com/example/dragscaleview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.example.dragscaleview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/dragscaleview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.example.dragscaleview;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 | assertEquals("com.example.dragscaleview", appContext.getPackageName());
25 | }
26 | }
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 29
5 | buildToolsVersion "29.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.example.dragscaleview"
9 | minSdkVersion 16
10 | targetSdkVersion 29
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15 | }
16 |
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: "libs", include: ["*.jar"])
27 | implementation 'androidx.appcompat:appcompat:1.2.0'
28 | implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
29 | testImplementation 'junit:junit:4.12'
30 | androidTestImplementation 'androidx.test.ext:junit:1.1.2'
31 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
32 |
33 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
16 |
22 |
23 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/dragscaleview/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.dragscaleview;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 | import android.widget.RelativeLayout;
6 |
7 | import com.example.dragscaleview.view.DragRectView;
8 |
9 | import androidx.appcompat.app.AppCompatActivity;
10 |
11 | public class MainActivity extends AppCompatActivity {
12 | private RelativeLayout drawLayout;
13 | private DragRectView dragRectView;
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | setContentView(R.layout.activity_main);
19 | drawLayout = findViewById(R.id.draw_view);
20 | dragRectView = findViewById(R.id.drag_rect_view);
21 |
22 | RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(200, 200);
23 | lp.setMargins(50, 50, 0, 0);
24 | dragRectView.setLayoutParams(lp);
25 | dragRectView.setClickable(true);
26 | dragRectView.setSelected(true);
27 | dragRectView.setMyTouchListener(new DragRectView.OnMyTouchListener() {
28 | @Override
29 | public void onClick() {
30 | dragRectView.setSelected(true);
31 | }
32 | });
33 |
34 | drawLayout.setOnClickListener(new View.OnClickListener() {
35 | @Override
36 | public void onClick(View v) {
37 | dragRectView.setSelected(false);
38 | }
39 | });
40 |
41 |
42 | // findViewById(R.id.create).setOnClickListener(new View.OnClickListener() {
43 | // @Override
44 | // public void onClick(View v) {
45 | // DragRectView dragRectView = new DragRectView(MainActivity.this);
46 | // RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(200, 200);
47 | // lp.setMargins(50, 50, 0, 0);
48 | // dragRectView.setLayoutParams(lp);
49 | // dragRectView.setClickable(true);
50 | // dragRectView.setSelected(true);
51 | // drawLayout.addView(dragRectView);
52 | //
53 | // }
54 | // });
55 |
56 |
57 | }
58 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/dragscaleview/view/DragRectView.java:
--------------------------------------------------------------------------------
1 | package com.example.dragscaleview.view;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.graphics.Canvas;
7 | import android.graphics.Paint;
8 | import android.graphics.Rect;
9 | import android.util.AttributeSet;
10 | import android.util.Log;
11 | import android.view.MotionEvent;
12 | import android.view.View;
13 | import android.view.ViewTreeObserver;
14 | import android.widget.RelativeLayout;
15 |
16 | import com.example.dragscaleview.R;
17 |
18 | import androidx.annotation.Nullable;
19 | import androidx.core.app.NotificationManagerCompat;
20 | import androidx.core.view.ViewCompat;
21 |
22 | import static android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM;
23 | import static android.widget.RelativeLayout.ALIGN_PARENT_RIGHT;
24 | import static android.widget.RelativeLayout.ALIGN_PARENT_TOP;
25 | import static android.widget.RelativeLayout.CENTER_HORIZONTAL;
26 | import static android.widget.RelativeLayout.CENTER_IN_PARENT;
27 | import static android.widget.RelativeLayout.CENTER_VERTICAL;
28 |
29 | /**
30 | * Created by zero_android on 2020/6/29.
31 | *
32 | * 一个可以自由拖拽、拉伸的view
33 | */
34 | public class DragRectView extends View implements View.OnTouchListener {
35 | private static final String TAG = "DragRectView";
36 | // view移动方向
37 | private static final int LEFT_TOP = 0x17;
38 | private static final int RIGHT_TOP = 0x18;
39 | private static final int LEFT_BOTTOM = 0x19;
40 | private static final int RIGHT_BOTTOM = 0x20;
41 | private static final int TOP = 0x21;
42 | private static final int LEFT = 0x22;
43 | private static final int BOTTOM = 0x23;
44 | private static final int RIGHT = 0x24;
45 | private static final int CENTER = 0x25;
46 |
47 | private int offset = 40;
48 |
49 |
50 | private int dbottom;
51 | private int dleft;
52 | private int downx;
53 | private int downy;
54 | private int dragDirection;
55 | private int dright;
56 | private int dtop;
57 | private int starh;
58 | private int starw;
59 | private int endh;
60 | private int endw;
61 | private int lastX;
62 | private int lastY;
63 |
64 | // view的宽、高初始化
65 | private int width = 0;
66 | private int height = 0;
67 | // 线条的宽度
68 | private int mLineSize = 3;
69 | // 图片大小
70 | private int mRectSize = 50;
71 | private Paint mPaint;
72 | private Rect mRect;
73 | private Paint.Style mStyle;
74 | private RelativeLayout.LayoutParams mlp;
75 | // 拉伸的view
76 | private Bitmap mRightBmp;
77 | private Rect mRightRect;
78 | private Bitmap mBottomBmp;
79 | private Rect mBottomRect;
80 | // private boolean isMove = false;
81 |
82 | private OnMyTouchListener myTouchListener;
83 |
84 | public DragRectView(Context context) {
85 | this(context, null);
86 | }
87 |
88 | public DragRectView(Context context, @Nullable AttributeSet attrs) {
89 | this(context, attrs, 0);
90 | }
91 |
92 | public DragRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
93 | super(context, attrs, defStyleAttr);
94 | init();
95 |
96 | }
97 |
98 | /**
99 | * 初始化view
100 | */
101 | private void init() {
102 | setBackground(getResources().getDrawable(R.drawable.bg_view_select));
103 | setOnTouchListener(this);
104 |
105 | mPaint = new Paint();
106 | mStyle = Paint.Style.STROKE;
107 | mPaint.setColor(ViewCompat.MEASURED_STATE_MASK);
108 | mPaint.setAntiAlias(true);
109 |
110 | getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
111 | public void onGlobalLayout() {
112 | width = 200;
113 | height = 200;
114 | mlp = (RelativeLayout.LayoutParams) getLayoutParams();
115 | invalidate();
116 | getViewTreeObserver().removeOnGlobalLayoutListener(this);
117 | }
118 | });
119 | mRightBmp = BitmapFactory.decodeResource(getResources(), R.drawable.stretch_right);
120 | mBottomBmp = BitmapFactory.decodeResource(getResources(), R.drawable.stretch_bottom);
121 | mRightRect = new Rect();
122 | mBottomRect = new Rect();
123 | mRect = new Rect();
124 |
125 | }
126 |
127 | public interface OnMyTouchListener {
128 | void onClick();
129 | }
130 |
131 | public void setMyTouchListener(OnMyTouchListener myTouchListener) {
132 | this.myTouchListener = myTouchListener;
133 | }
134 |
135 | @Override
136 | public boolean onTouch(View v, MotionEvent event) {
137 | v.getParent().requestDisallowInterceptTouchEvent(true);
138 | if (myTouchListener != null) {
139 | myTouchListener.onClick();
140 | }
141 | int action = event.getAction();
142 | if (action == MotionEvent.ACTION_DOWN) {
143 | dleft = v.getLeft();
144 | dright = v.getRight();
145 | dtop = v.getTop();
146 | dbottom = v.getBottom();
147 | downx = (int) event.getRawX();
148 | downy = (int) event.getRawY();
149 | lastX = (int) event.getRawX();
150 | lastY = (int) event.getRawY();
151 | starw = width;
152 | starh = height;
153 | dragDirection = getDirection(v, (int) event.getX(), (int) event.getY());
154 | }
155 | handleDrag(v, event, action);
156 | invalidate();
157 | return false;
158 | }
159 |
160 | @Override
161 | protected void onDraw(Canvas canvas) {
162 | super.onDraw(canvas);
163 | canvas.save();
164 | mPaint.setStyle(mStyle);
165 | mPaint.setStrokeWidth((float) mLineSize);
166 | mRect.set(15, 15, width, height);
167 | canvas.drawRect(mRect, mPaint);
168 | mlp.width = (int) (width + 20.0f);
169 | mlp.height = (int) (height + 20.0f);
170 | setLayoutParams(mlp);
171 | canvas.restore();
172 | if (isSelected()) {
173 | mRightRect.set(getWidth() - mRectSize, (getHeight() / 2) - (mRectSize / 2), getWidth(), (getHeight() / 2) + (mRectSize / 2));
174 | canvas.drawBitmap(mRightBmp, null, mRightRect, this.mPaint);
175 | mBottomRect.set((getWidth() / 2) - (mRectSize / 2), getHeight() - mRectSize, (getWidth() / 2) + (mRectSize / 2), getHeight());
176 | canvas.drawBitmap(mBottomBmp, null, mBottomRect, this.mPaint);
177 | }
178 | }
179 |
180 |
181 | private void handleDrag(View v, MotionEvent event, int action) {
182 | switch (action) {
183 | case MotionEvent.ACTION_UP:
184 | endw = width;
185 | endh = height;
186 | if (dragDirection == CENTER) {
187 | if (Math.abs(lastX - downx) > 10 || Math.abs(lastY - downy) > 10) {
188 | move(v);
189 | }
190 | } else {
191 | if (Math.abs(endw - starw) > 5 || Math.abs(endh - starh) > 5) {
192 | handleSizeChanged(v);
193 | }
194 | }
195 | dragDirection = 0;
196 | break;
197 | case MotionEvent.ACTION_MOVE:
198 | int dx = ((int) event.getRawX()) - lastX;
199 | int dy = ((int) event.getRawY()) - lastY;
200 | switch (dragDirection) {
201 | case BOTTOM:
202 | bottom(v, dy);
203 | break;
204 | case RIGHT:
205 | right(v, dx);
206 | break;
207 | case CENTER:
208 | center(v, dx, dy);
209 | break;
210 | // default:
211 | // break;
212 | }
213 | lastX = (int) event.getRawX();
214 | lastY = (int) event.getRawY();
215 | break;
216 | default:
217 | break;
218 | }
219 | }
220 |
221 | /**
222 | * 处理view 大小变化
223 | *
224 | * @param v
225 | */
226 | private void handleSizeChanged(View v) {
227 | Log.d(TAG, "...handleSizeChanged ...");
228 | mlp = (RelativeLayout.LayoutParams) v.getLayoutParams();
229 | mlp.width = endw + mRectSize / 2;
230 | mlp.height = endh + mRectSize / 2;
231 | setHeights((float) endh);
232 | setWidths((float) endw);
233 | v.setLayoutParams(mlp);
234 | }
235 |
236 | /**
237 | * 处理view move
238 | *
239 | * @param v
240 | */
241 | private void move(View v) {
242 | Log.d(TAG, "...move ...");
243 | mlp = (RelativeLayout.LayoutParams) v.getLayoutParams();
244 | cleanParams(mlp);
245 | mlp.setMargins(v.getLeft(), v.getTop(), NotificationManagerCompat.IMPORTANCE_UNSPECIFIED,
246 | NotificationManagerCompat.IMPORTANCE_UNSPECIFIED);
247 | setLayoutParams(mlp);
248 | }
249 |
250 | /**
251 | * 通过坐标和移动距离,确定移动方向
252 | *
253 | * @param v
254 | * @param x
255 | * @param y
256 | * @return
257 | */
258 | private int getDirection(View v, int x, int y) {
259 | int left = v.getLeft();
260 | int right = v.getRight();
261 | int bottom = v.getBottom();
262 | int top = v.getTop();
263 | if (x < offset && y < offset) {
264 | return LEFT_TOP;
265 | }
266 | if (y < offset && (right - left) - x < offset) {
267 | return RIGHT_TOP;
268 | }
269 | if (x < offset && (bottom - top) - y < offset) {
270 | return LEFT_BOTTOM;
271 | }
272 | if ((right - left) - x < offset && (bottom - top) - y < offset) {
273 | return RIGHT_BOTTOM;
274 | }
275 | if (x < offset) {
276 | return LEFT;
277 | }
278 | if (y < offset) {
279 | return TOP;
280 | }
281 | if ((right - left) - x < offset) {
282 | return RIGHT;
283 | }
284 | if ((bottom - top) - y < offset) {
285 | return BOTTOM;
286 | }
287 | return CENTER;
288 | }
289 |
290 | public void cleanParams(RelativeLayout.LayoutParams lp) {
291 | lp.addRule(CENTER_IN_PARENT, 0);
292 | lp.addRule(ALIGN_PARENT_RIGHT, 0);
293 | lp.addRule(ALIGN_PARENT_BOTTOM, 0);
294 | lp.addRule(ALIGN_PARENT_TOP, 0);
295 | lp.addRule(CENTER_VERTICAL, 0);
296 | lp.addRule(CENTER_HORIZONTAL, 0);
297 | }
298 |
299 | public void setHeights(float height) {
300 | this.height = (int) (0.5f + height);
301 | }
302 |
303 | public void setWidths(float width) {
304 | this.width = (int) (0.5f + width);
305 | }
306 |
307 | private void bottom(View v, int dy) {
308 | Log.d(TAG, "...bottom ..." + dy);
309 | height += dy;
310 | if (height < 100) {
311 | height = 100;
312 | }
313 | }
314 |
315 | private void right(View v, int dx) {
316 | Log.d(TAG, "...right ..." + dx);
317 | width += dx;
318 | if (width < 100) {
319 | width = 100;
320 | }
321 | }
322 |
323 | private void center(View v, int dx, int dy) {
324 | Log.d(TAG, "...center ..." + dx + "/" + dy);
325 | int left = v.getLeft() + dx;
326 | int top = v.getTop() + dy;
327 | int right = v.getRight() + dx;
328 | int bottom = v.getBottom() + dy;
329 | cleanParams(mlp);
330 | mlp.setMargins(left, top, NotificationManagerCompat.IMPORTANCE_UNSPECIFIED, NotificationManagerCompat.IMPORTANCE_UNSPECIFIED);
331 | setLayoutParams(mlp);
332 | }
333 |
334 |
335 | }
336 |
--------------------------------------------------------------------------------