lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
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
package cn.sinata.xldutils.view;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
 
import androidx.annotation.Nullable;
 
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.AspectRatioMeasure;
import com.facebook.drawee.view.DraweeHolder;
import com.facebook.drawee.view.MultiDraweeHolder;
 
import java.util.ArrayList;
import java.util.List;
 
import cn.sinata.xldutils.utils.DensityUtil;
 
/**
 *  网格展示多张图片的ImageView,图片通过Freso获取展示,调用setUrls(List<String> urls)设置数据源。
 * Created by liaoxiang on 16/7/27.
 */
@SuppressWarnings("unused")
public class MultiImageView extends View{
    private final AspectRatioMeasure.Spec mMeasureSpec = new AspectRatioMeasure.Spec();
    private float mAspectRatio = 1.0f;//默认正方形
    @SuppressWarnings("SpellCheckingInspection")
    private MultiDraweeHolder<GenericDraweeHierarchy> multiDraweeHolder;
    private boolean mInitialised = false;
    private Context mContext;
    private int padding = 0;
    private ArrayList<Drawable> drawables = new ArrayList<>();
    boolean mIsCapturingGesture;
    boolean mIsClickCandidate;
    long mActionDownTime;
    float mActionDownX;
    float mActionDownY;
    float mSingleTapSlopPx;
 
    public MultiImageView(Context context) {
        super(context);
        init(context);
    }
 
    public MultiImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
 
    public MultiImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
 
    /** This method is idempotent so it only has effect the first time it's called */
    private void init(Context context) {
        final ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
        mSingleTapSlopPx = viewConfiguration.getScaledTouchSlop();
        mContext = context;
        if (mInitialised) {
            return;
        }
        padding = DensityUtil.dip2px(context,2);
        mInitialised = true;
 
        multiDraweeHolder = new MultiDraweeHolder<>();
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int size = multiDraweeHolder.size();
        int usedW = 0;
        int usedH = 0;
        drawables.clear();
        for (int i = 0; i < multiDraweeHolder.size(); ++i) {
            Drawable drawable = multiDraweeHolder.get(i).getTopLevelDrawable();
            if (drawable != null) {
                if (size == 1){
                    int w = getWidth();
                    int h = getHeight();
                    if (h<=0){
                        h = w;
                    }
                    drawable.setBounds(usedW,usedH,w+usedW,h);
                }else if (size < 4){
                    int w = (getWidth()- (padding*(size-1)))/size;
                    drawable.setBounds(usedW,usedH,w+usedW,w+usedH);
                    usedW += w+padding;
                }else if (size == 4){
                    //行首重置x坐标
                    if (i%2==0){
                        usedW = 0;
                    }
                    int w = (getWidth()-padding)/2;
                    usedH = (i/2)*(w+padding);
                    drawable.setBounds(usedW,usedH,w+usedW,w+usedH);
                    usedW += w+padding;
                }else {
                    //行首重置x坐标
                    if (i%3==0){
                        usedW = 0;
                    }
                    int w = (getWidth()- (padding*2))/3;
                    usedH = (i/3)*(w+padding);
                    drawable.setBounds(usedW,usedH,w+usedW,w+usedH);
                    usedW += (w+padding);
                }
                drawables.add(drawable);
            }
        }
        multiDraweeHolder.draw(canvas);
    }
 
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        onAttach();
    }
 
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        onDetach();
    }
 
    @Override
    public void onStartTemporaryDetach() {
        super.onStartTemporaryDetach();
        onDetach();
    }
 
    @Override
    public void onFinishTemporaryDetach() {
        super.onFinishTemporaryDetach();
        onAttach();
    }
 
    /** Called by the system to attach. Subclasses may override. */
    protected void onAttach() {
        doAttach();
    }
 
    /**  Called by the system to detach. Subclasses may override. */
    protected void onDetach() {
        doDetach();
    }
 
    /**
     * Does the actual work of attaching.
     *
     * Non-test subclasses should NOT override. Use onAttach for custom code.
     */
    protected void doAttach() {
        multiDraweeHolder.onAttach();
    }
 
    /**
     * Does the actual work of detaching.
     *
     * Non-test subclasses should NOT override. Use onDetach for custom code.
     */
    protected void doDetach() {
        drawables.clear();
        int size = multiDraweeHolder.size();
        for (int i = 0; i < size; i++) {
            multiDraweeHolder.get(i).getTopLevelDrawable().setCallback(null);
        }
        multiDraweeHolder.onDetach();
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //点击重新加载图片,如果开启需要打开此段代码
//        if (multiDraweeHolder.onTouchEvent(event)) {
//            return true;
//        }
 
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mIsCapturingGesture = true;
                mIsClickCandidate = true;
                mActionDownTime = event.getEventTime();
                mActionDownX = event.getX();
                mActionDownY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (Math.abs(event.getX() - mActionDownX) > mSingleTapSlopPx ||
                        Math.abs(event.getY() - mActionDownY) > mSingleTapSlopPx) {
                    mIsClickCandidate = false;
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                mIsCapturingGesture = false;
                mIsClickCandidate = false;
                break;
            case MotionEvent.ACTION_UP:
                mIsCapturingGesture = false;
                if (Math.abs(event.getX() - mActionDownX) > mSingleTapSlopPx ||
                        Math.abs(event.getY() - mActionDownY) > mSingleTapSlopPx) {
                    mIsClickCandidate = false;
                }
                if (mIsClickCandidate) {
                    if (event.getEventTime() - mActionDownTime <= ViewConfiguration.getLongPressTimeout()) {
                        clickCheck(event,0);
                    } else {
                        //长按
                        clickCheck(event,1);
                    }
                }
                mIsClickCandidate = false;
                break;
        }
        return true;
    }
 
    private void clickCheck(MotionEvent event,int type){
        //检查点击区域
        if (drawables.size()>0){
            for (int i = 0; i < drawables.size(); i++) {
                if (drawables.get(i).getBounds().contains((int)event.getX(),(int)event.getY())){
                    if (type == 0){//单击
                        if (imageClickListener!=null){
                            imageClickListener.onClick(i);
                            break;
                        }
                    }else {//长按
                        if (imageLongClickListener!=null){
                            imageLongClickListener.onLongClick(i);
                            break;
                        }
                    }
                }
            }
        }
    }
 
    /**
     * Sets the desired aspect ratio (w/h).
     */
    public void setAspectRatio(float aspectRatio) {
        if (aspectRatio == mAspectRatio) {
            return;
        }
        mAspectRatio = aspectRatio;
        requestLayout();
    }
 
    /**
     * Gets the desired aspect ratio (w/h).
     */
    public float getAspectRatio() {
        return mAspectRatio;
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mMeasureSpec.width = widthMeasureSpec;
        mMeasureSpec.height = heightMeasureSpec;
        updateMeasureSpec(
                mMeasureSpec,
                mAspectRatio,
                getLayoutParams(),
                getPaddingLeft() + getPaddingRight(),
                getPaddingTop() + getPaddingBottom());
        super.onMeasure(mMeasureSpec.width, mMeasureSpec.height);
    }
 
    private void updateMeasureSpec(
            AspectRatioMeasure.Spec spec,
            float aspectRatio,
            @Nullable ViewGroup.LayoutParams layoutParams,
            int widthPadding,
            int heightPadding) {
        if (aspectRatio <= 0 || layoutParams == null) {
            return;
        }
        int size = multiDraweeHolder.size();
        if (shouldAdjust(layoutParams.height)) {
            int widthSpecSize = MeasureSpec.getSize(spec.width);
            int desiredHeight = 0;
            if (size == 1 || size == 4 || size > 6){
                desiredHeight = (int) ((widthSpecSize - widthPadding) / aspectRatio + heightPadding);
            }else if (size == 2 ){
                desiredHeight = (int) ((widthSpecSize - widthPadding - padding)/ 2 / aspectRatio + heightPadding);
            }else if (size == 3){
                desiredHeight = (int) ((widthSpecSize - widthPadding - padding)/ 3 / aspectRatio + heightPadding);
            }else if (size<7){
                desiredHeight = (int) ((widthSpecSize - widthPadding - padding)/ 3 / aspectRatio*2 + padding + heightPadding);
            }
            int resolvedHeight = View.resolveSize(desiredHeight, spec.height);
            spec.height = MeasureSpec.makeMeasureSpec(resolvedHeight, MeasureSpec.EXACTLY);
        } else if (shouldAdjust(layoutParams.width)) {
            int heightSpecSize = MeasureSpec.getSize(spec.height);
            int desiredWidth = (int) ((heightSpecSize - heightPadding) * aspectRatio + widthPadding);
            int resolvedWidth = View.resolveSize(desiredWidth, spec.width);
            spec.width = MeasureSpec.makeMeasureSpec(resolvedWidth, MeasureSpec.EXACTLY);
        }
    }
 
    private boolean shouldAdjust(int layoutDimension) {
        // Note: wrap_content is supported for backwards compatibility, but should not be used.
        return layoutDimension == 0 || layoutDimension == ViewGroup.LayoutParams.WRAP_CONTENT;
    }
 
    @Override
    protected boolean verifyDrawable(Drawable who) {
        super.verifyDrawable(who);
        return multiDraweeHolder.verifyDrawable(who);
    }
 
    public void setUrls(List<String> urls){
        if (urls==null){
            return;
        }
        multiDraweeHolder.clear();
        for (int i = 0; i < urls.size(); i++) {
            GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(getResources())
                    .setPlaceholderImage(new ColorDrawable(Color.GRAY))
                    .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                    .build();
            //noinspection SpellCheckingInspection
            DraweeHolder<GenericDraweeHierarchy> mDraweeHolder = DraweeHolder.create(hierarchy, mContext);
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setUri(Uri.parse(urls.get(i)))
                    .setAutoPlayAnimations(true) // 设置加载图片完成后是否直接进行播放
                    .setOldController(mDraweeHolder.getController())
                    .build();
            mDraweeHolder.setController(controller);
            multiDraweeHolder.add(mDraweeHolder);
        }
        int size = multiDraweeHolder.size();
        for (int i = 0; i < size; i++) {
            multiDraweeHolder.get(i).getTopLevelDrawable().setCallback(this);
        }
    }
 
 
 
    private OnImageClickListener imageClickListener;
 
    public void setOnImageClickListener(OnImageClickListener imageClickListener){
        this.imageClickListener = imageClickListener;
    }
    private OnImageLongClickListener imageLongClickListener;
 
    public void setOnImageLongClickListener(OnImageLongClickListener imageLongClickListener){
        this.imageLongClickListener = imageLongClickListener;
    }
 
    public interface OnImageClickListener{
        void onClick(int position);
    }
 
    public interface OnImageLongClickListener{
        void onLongClick(int position);
    }
}