Android实现手势控制ImageView图片大小

时间:2022-06-26 07:21:30

本文实例实现的主要功能是在imageview中识别手势用以控制图片放大或缩小,具有一定的参考价值,分享给大家。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
public class matriximageview extends imageview {
  private gesturedetector mgesturedetector;
  private matrix mmatrix = new matrix();
  private float mimagewidth;
  private float mimageheight;
  private float mscale;
  private onmovinglistener mmovelistener;
  private onsingletaplistener msingletaplistener;
  public matriximageview(context context, attributeset attrs) {
    super(context, attrs);
    init();
  }
  public matriximageview(context context) {
    super(context, null);
    init();
  }
  private void init() {
    matrixtouchlistener listener = new matrixtouchlistener();
    setontouchlistener(listener);
    mgesturedetector = new gesturedetector(getcontext(),
        new gesturelistener(listener));
    setbackgroundcolor(color.black);
    setscaletype(scaletype.fit_center);
  }
  public void setonmovinglistener(onmovinglistener listener) {
    mmovelistener = listener;
  }
  public void setonsingletaplistener(onsingletaplistener onsingletaplistener) {
    this.msingletaplistener = onsingletaplistener;
  }
  @override
  public void setimagebitmap(bitmap bm) {
    super.setimagebitmap(bm);
    if (getwidth() == 0) {
 
      viewtreeobserver vto = getviewtreeobserver();
      vto.addonpredrawlistener(new viewtreeobserver.onpredrawlistener() {
        public boolean onpredraw() {
 
          initdata();
          matriximageview.this.getviewtreeobserver()
              .removeonpredrawlistener(this);
          return true;
        }
 
      });
    } else {
      initdata();
    }
  }
  private void initdata() {
    mmatrix.set(getimagematrix());
    float[] values = new float[9];
    mmatrix.getvalues(values);
    mimagewidth = getwidth() / values[matrix.mscale_x];
    mimageheight = (getheight() - values[matrix.mtrans_y] * 2)
        / values[matrix.mscale_y];
    mscale = values[matrix.mscale_x];
  }
  public class matrixtouchlistener implements ontouchlistener {
    private static final int mode_drag = 1;
    private static final int mode_zoom = 2;
    private static final int mode_unable = 3;
    private static final float max_scale = 6;
    private static final float double_click_sacle = 2;
    private int mmode = 0;
    private float mstartdis;
    private matrix mcurrentmatrix = new matrix();
    private boolean mleftdragable;
    private boolean mrightdragable;
    private boolean mfirstmove = false;
    private pointf mstartpoint = new pointf();
    @override
    public boolean ontouch(view v, motionevent event) {
      switch (event.getactionmasked()) {
      case motionevent.action_down:
        mmode = mode_drag;
        mstartpoint.set(event.getx(), event.gety());
        ismatrixenable();
        startdrag();
        checkdragable();
        break;
      case motionevent.action_up:
      case motionevent.action_cancel:
        resetmatrix();
        stopdrag();
        break;
      case motionevent.action_move:
        if (mmode == mode_zoom) {
          setzoommatrix(event);
        } else if (mmode == mode_drag) {
          setdragmatrix(event);
        } else {
          stopdrag();
        }
        break;
      case motionevent.action_pointer_down:
        if (mmode == mode_unable)
          return true;
        mmode = mode_zoom;
        mstartdis = distance(event);
        break;
      case motionevent.action_pointer_up:
        break;
      default:
        break;
      }
 
      return mgesturedetector.ontouchevent(event);
 
    }
 
    private void startdrag() {
 
      if (mmovelistener != null)
 
        mmovelistener.startdrag();
 
    }
 
    private void stopdrag() {
 
      if (mmovelistener != null)
 
        mmovelistener.stopdrag();
 
    }
 
    private void checkdragable() {
 
      mleftdragable = true;
 
      mrightdragable = true;
 
      mfirstmove = true;
 
      float[] values = new float[9];
 
      getimagematrix().getvalues(values);
 
      if (values[matrix.mtrans_x] >= 0)
 
        mrightdragable = false;
 
      if ((mimagewidth) * values[matrix.mscale_x]
 
          + values[matrix.mtrans_x] <= getwidth()) {
 
        mleftdragable = false;
 
      }
 
    }
 
    public void setdragmatrix(motionevent event) {
 
      if (iszoomchanged()) {
 
        float dx = event.getx() - mstartpoint.x;
 
        float dy = event.gety() - mstartpoint.y;
 
        if (math.sqrt(dx * dx + dy * dy) > 10f) {
 
          mstartpoint.set(event.getx(), event.gety());
 
          mcurrentmatrix.set(getimagematrix());
 
          float[] values = new float[9];
 
          mcurrentmatrix.getvalues(values);
 
          dy = checkdybound(values, dy);
 
          dx = checkdxbound(values, dx, dy);
 
          mcurrentmatrix.posttranslate(dx, dy);
 
          setimagematrix(mcurrentmatrix);
 
        }
      } else {
        stopdrag();
      }
    }
    private boolean iszoomchanged() {
      float[] values = new float[9];
      getimagematrix().getvalues(values);
      float scale = values[matrix.mscale_x];
      return scale != mscale;
    }
    private float checkdybound(float[] values, float dy) {
      float height = getheight();
      if (mimageheight * values[matrix.mscale_y] < height)
        return 0;
      if (values[matrix.mtrans_y] + dy > 0)
 
        dy = -values[matrix.mtrans_y];
 
      else if (values[matrix.mtrans_y] + dy < -(mimageheight
          * values[matrix.mscale_y] - height))
 
        dy = -(mimageheight * values[matrix.mscale_y] - height)
 
            - values[matrix.mtrans_y];
 
      return dy;
 
    }
 
    private float checkdxbound(float[] values, float dx, float dy) {
 
      float width = getwidth();
 
      if (!mleftdragable && dx < 0) {
 
        if (math.abs(dx) * 0.4f > math.abs(dy) && mfirstmove) {
 
          stopdrag();
 
        }
 
        return 0;
 
      }
      if (!mrightdragable && dx > 0) {
        if (math.abs(dx) * 0.4f > math.abs(dy) && mfirstmove) {
          stopdrag();
        }
        return 0;
      }
      mleftdragable = true;
 
      mrightdragable = true;
 
      if (mfirstmove)
 
        mfirstmove = false;
 
      if (mimagewidth * values[matrix.mscale_x] < width) {
 
        return 0;
 
      }
 
      if (values[matrix.mtrans_x] + dx > 0) {
 
        dx = -values[matrix.mtrans_x];
 
      } else if (values[matrix.mtrans_x] + dx < -(mimagewidth
 
          * values[matrix.mscale_x] - width)) {
 
        dx = -(mimagewidth * values[matrix.mscale_x] - width)
 
            - values[matrix.mtrans_x];
 
      }
 
      return dx;
 
    }
 
    private void setzoommatrix(motionevent event) {
 
      if (event.getpointercount() < 2)
        return;
 
      float enddis = distance(event);
      if (enddis > 10f) {
        float scale = enddis / mstartdis;
        mstartdis = enddis;
        mcurrentmatrix.set(getimagematrix());
        float[] values = new float[9];
        mcurrentmatrix.getvalues(values);
        scale = checkmaxscale(scale, values);
        pointf centerf = getcenter(scale, values);
        mcurrentmatrix.postscale(scale, scale, centerf.x, centerf.y);
        setimagematrix(mcurrentmatrix);
      }
    }
    private pointf getcenter(float scale, float[] values) {
      if (scale * values[matrix.mscale_x] < mscale || scale >= 1) {
        return new pointf(getwidth() / 2, getheight() / 2);
      }
      float cx = getwidth() / 2;
      float cy = getheight() / 2;
      if ((getwidth() / 2 - values[matrix.mtrans_x]) * scale < getwidth() / 2)
        cx = 0;
      if ((mimagewidth * values[matrix.mscale_x] + values[matrix.mtrans_x])
          * scale < getwidth())
 
        cx = getwidth();
 
      return new pointf(cx, cy);
 
    }
    private float checkmaxscale(float scale, float[] values) {
 
      if (scale * values[matrix.mscale_x] > max_scale)
        scale = max_scale / values[matrix.mscale_x];
      return scale;
    }
    private void resetmatrix() {
      if (checkrest()) {
        mcurrentmatrix.set(mmatrix);
        setimagematrix(mcurrentmatrix);
      } else {
        float[] values = new float[9];
        getimagematrix().getvalues(values);
        float height = mimageheight * values[matrix.mscale_y];
        if (height < getheight()) {
          float topmargin = (getheight() - height) / 2;
          if (topmargin != values[matrix.mtrans_y]) {
            mcurrentmatrix.set(getimagematrix());
            mcurrentmatrix.posttranslate(0, topmargin
                - values[matrix.mtrans_y]);
            setimagematrix(mcurrentmatrix);
          }
        }
      }
    }
    private boolean checkrest() {
      float[] values = new float[9];
      getimagematrix().getvalues(values);
      float scale = values[matrix.mscale_x];
      return scale < mscale;
    }
    private void ismatrixenable() {
      if (getscaletype() != scaletype.center) {
 
        setscaletype(scaletype.matrix);
 
      } else {
        mmode = mode_unable;
      }
 
    }
    private float distance(motionevent event) {
 
      float dx = event.getx(1) - event.getx(0);
 
      float dy = event.gety(1) - event.gety(0);
 
      return (float) math.sqrt(dx * dx + dy * dy);
    }
    public void ondoubleclick() {
      float scale = iszoomchanged() ? 1 : double_click_sacle;
      mcurrentmatrix.set(mmatrix);
      mcurrentmatrix.postscale(scale, scale, getwidth() / 2,
          getheight() / 2);
      setimagematrix(mcurrentmatrix);
    }
  }
  private class gesturelistener extends simpleongesturelistener {
    private final matrixtouchlistener mtouchlistener;
    public gesturelistener(matrixtouchlistener listener) {
      this.mtouchlistener = listener;
    }
    @override
    public boolean ondown(motionevent e) {
      return true;
    }
    @override
    public boolean ondoubletap(motionevent e) {
 
      mtouchlistener.ondoubleclick();
      return true;
    }
    @override
    public boolean onsingletapup(motionevent e) {
      return super.onsingletapup(e);
    }
    @override
    public void onlongpress(motionevent e) {
      super.onlongpress(e);
    }
    @override
    public boolean onscroll(motionevent e1, motionevent e2,
 
        float distancex, float distancey) {
      return super.onscroll(e1, e2, distancex, distancey);
 
    }
    @override
    public boolean onfling(motionevent e1, motionevent e2, float velocityx,
        float velocityy) {
      return super.onfling(e1, e2, velocityx, velocityy);
    }
    @override
    public void onshowpress(motionevent e) {
      super.onshowpress(e);
    }
    @override
    public boolean ondoubletapevent(motionevent e) {
      return super.ondoubletapevent(e);
    }
    @override
    public boolean onsingletapconfirmed(motionevent e) {
      if (msingletaplistener != null)
        msingletaplistener.onsingletap(e);
      return super.onsingletapconfirmed(e);
    }
  }
  public interface onmovinglistener {
    public void startdrag();
 
    public void stopdrag();
  }
  public interface onsingletaplistener {
    public void onsingletap(motionevent e);
  }
}

我对其中定义onsingletaplistener接口的方法稍作了一些修改,为onsingletap回调方法增加了motionevent类型的参数,来方便我们根据用户具体的事件内容作出对应的控制。

以上就是本文的全部内容,希望对大家学习android软件编程有所帮助。