admin
2020-10-10 8039a1b2fbfa3471b6f726d3e839d7867c81a84f
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
package com.youku.player.plugin;
 
import java.util.ArrayList;
 
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
 
import com.baseproject.utils.Logger;
import com.youku.player.goplay.Point;
import com.youku.player.ui.R;
 
public class HotPointView extends RelativeLayout {
 
    private static final String TAG = HotPointView.class.getSimpleName();
 
    private ArrayList<Point> points = new ArrayList<Point>();
    private int seekbar_thumb_img_width;
    private int seekbar_thumb_img_width_half;
    private int hotpoint_img_width;
    private int hotpoint_img_width_half;
 
    public HotPointView(Context context) {
        super(context);
        init();
    }
 
    public HotPointView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    public HotPointView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
 
    private void init() {
 
        Drawable seekbar_thumb_img = getContext().getResources().getDrawable(
                R.drawable.icon_scrubbarslider);
        seekbar_thumb_img_width = seekbar_thumb_img.getIntrinsicWidth();
        seekbar_thumb_img_width_half = seekbar_thumb_img_width / 2;
        Drawable hotpoint_img = getContext().getResources().getDrawable(
                R.drawable.hotpoint_img);
        hotpoint_img_width = hotpoint_img.getIntrinsicWidth();
        hotpoint_img_width_half = hotpoint_img_width / 2;
 
    }
 
    public void setPoints(ArrayList<Point> points) {
        if (points != null) {
            this.points.clear();
            this.points.addAll(points);
        }
 
        updatePointsPos();
    }
 
    private int max = 0;
 
    public void setMax(int max) {
        this.max = max;
    }
 
    private boolean showed = false;
    private int oldWidth = 0;
 
    private void updatePointsPos() {
 
        Logger.d(TAG, "updatePointsPos:");
 
        showed = false;
        removeAllViews();
 
        oldWidth = getWidth();
        if (oldWidth == 0) {
            Logger.e(TAG, "viewWidth = 0:");
            return;
        }
 
        if (max == 0) {
            Logger.e(TAG, "max  = 0:");
            throw new IllegalStateException("have not set Max value");
        }
 
        int length = points == null ? 0 : points.size();
        Logger.d(TAG, "精彩点个数:" + length);
        for (int i = 0; i < length; i++) {
            ImageView hotpoint_img = new ImageView(getContext());
            hotpoint_img.setTag(points.get(i));
            hotpoint_img.setScaleType(ScaleType.CENTER_INSIDE);
            hotpoint_img.setImageResource(R.drawable.hotpoint_img);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            int point_pos = (int) (1f * (getWidth() - seekbar_thumb_img_width)
                    * points.get(i).start / max);
            params.setMargins(point_pos + seekbar_thumb_img_width_half
                    - hotpoint_img_width_half, params.topMargin,
                    params.rightMargin, params.bottomMargin);
            addView(hotpoint_img, params);
        }
 
        showed = true;
    }
 
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        checkNeedUpdatePos();
    }
    
    public void setProgress(int currentPosition) {
        if(getWidth() ==0){
            return ;
        }
        
        checkNeedUpdatePos();
        
        int NEAR_POINT_MULTIPLE = seekbar_thumb_img_width / getWidth()  ;
        
        for (int i = 0; i < getChildCount(); i++) {
            ImageView hotpoint_img = (ImageView) getChildAt(i);
            Point point = (Point) hotpoint_img.getTag();
            if (point != null) {
 
                boolean clickable = !(Math.abs(point.start - currentPosition) <= max
                        / NEAR_POINT_MULTIPLE);
                if (clickable) {
                    hotpoint_img.setClickable(true);
                    hotpoint_img.setVisibility(View.VISIBLE);
                } else {
                    hotpoint_img.setClickable(false);
                    hotpoint_img.setVisibility(View.INVISIBLE);
                }
            }
        }
    }
 
    private void checkNeedUpdatePos() {
        if (!showed) {
            updatePointsPos();
        }
 
        if (oldWidth != getWidth()) {
            updatePointsPos();
        }
    }
 
}