• 王学岗自定义ViewGroup在哪里获取控件


     
    
            
    
                
                
    
                
            
    
        
    
    • 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

    上面的布局,我们自定义了HistogramView集成ViewGroup。

    public class HistogramView extends ViewGroup {}
    
    • 1

    那我们应该在哪里获取HistogramView 中的控件呢?
    答案是这个方法。

        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
             tvLeft = findViewById(R.id.tvLeft);
            tvRight = findViewById(R.id.tvRight);
            viewBg = findViewById(R.id.viewBg);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    注意这些方法执行的先后顺序是
    构造方法————>onFinishInflate——————>setHeightList————>onMeassure(若干次)————>onLayout————>onDraw()
    setHeightList是在在Activity中调用的方法。 histogramView.setHeightList(histogramDataBeanList, 100.7f);
    在这里插入图片描述

    下面是柱状图全部代码。可以看看

    package cn.xiaozhibo.com.kit.widgets;
    
    
    import android.animation.Animator;
    import android.animation.AnimatorSet;
    import android.animation.ValueAnimator;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.core.content.ContextCompat;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import cn.xiaozhibo.com.R;
    import cn.xiaozhibo.com.kit.bean.HistogramDataBean;
    import cn.xiaozhibo.com.kit.third.utils.UIUtils;
    
    public class HistogramView extends ViewGroup {
        //switchButton是否在右侧
        private boolean isRightNumberAndPeriod;
        private Context mContext;
        //  左侧文字的画笔
        private Paint textPaintLeft;
        //    底侧文字的画笔
        private Paint textPaintDown;
    
        private List heightList;
        //    最高的柱子,需要取整数
        private int heightest;
        //    需要绘制的柱子的数量
        private int columnCount;
        //每个柱子连同它左右的空间的距离
        private int spaceWithLeftRight;
        //    无透明度的画笔
        private Paint abovePaint;
        //    有透明度的画笔
        private Paint downPaint;
        //    左侧文字的大小
        private int leftTextSize;
        //    上侧文字的大小
        private int topTextSize;
        //    底侧文字的大小
        private int bottomTextSize;
        //    一个刻度的像素高度
        private int markSizePx;
        //    一个刻度的高度
        private int markSize;
        //    单位场次的画笔
        private Paint textPaintTop;
        private Paint textPaintTopNote;
        //    底部需要绘制的数据
        private List bottomList = new ArrayList<>();
        //    顶部要绘制的数据
        private String topUnit;
    
        private String topUnitNote;
    
        private Rect topUnitRect;
        private Rect leftTextRect;
        //    顶部两个控件的y轴
        private int topRegion;
        private TextView tvLeft;
        private TextView tvRight;
        private View viewBg;
        private final int animationTime = 2000;
        private ValueAnimator valueAnimatorColorFromBlackToWhite;
        private ValueAnimator valueAnimatorColorFromWhiteToBlack;
        private AnimatorSet animatorSetLeftClicked;
        private AnimatorSet animatorSetRightClicked;
        private ValueAnimator valueAnimatorToLeft;
        private ValueAnimator valueAnimatorToRight;
    
        public HistogramView(Context context) {
            this(context, null);
        }
    
        public HistogramView(Context context, @Nullable AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public HistogramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            this(context, attrs, defStyleAttr, 0);
        }
    
        public HistogramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            initView(context, attrs, defStyleAttr, defStyleRes);
        }
    
    
        private void initView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            this.mContext = context;
            setBackgroundResource(R.drawable.shape_white_5);
    //        左侧文字的画笔
            textPaintLeft = new Paint();
            textPaintLeft.setAntiAlias(true);
            textPaintLeft.setTextAlign(Paint.Align.RIGHT);
            textPaintLeft.setColor(ContextCompat.getColor(mContext, R.color.gray56));
            textPaintLeft.setTextSize(UIUtils.dip2px(mContext, 9));
    
    //        底侧文字的画笔
            textPaintDown = new Paint();
            textPaintDown.setAntiAlias(true);
            textPaintDown.setTextAlign(Paint.Align.CENTER);
            textPaintDown.setColor(ContextCompat.getColor(mContext, R.color.gray56));
            textPaintDown.setTextSize(UIUtils.dip2px(mContext, 9));
    
    //        顶侧文字的画笔
            textPaintTop = new Paint();
            textPaintTop.setAntiAlias(true);
            textPaintTop.setTextAlign(Paint.Align.LEFT);
            textPaintTop.setColor(ContextCompat.getColor(mContext, R.color.load_more_text));
            textPaintTop.setTextSize(UIUtils.dip2px(mContext, 10));
    
    //        顶侧文字的注释画笔
            textPaintTopNote = new Paint();
            textPaintTopNote.setAntiAlias(true);
            textPaintTopNote.setTextAlign(Paint.Align.LEFT);
            textPaintTopNote.setColor(ContextCompat.getColor(mContext, R.color.gray56));
            textPaintTopNote.setTextSize(UIUtils.dip2px(mContext, 10));
    
    //        不带透明度的画笔
            abovePaint = new Paint();
            abovePaint.setAntiAlias(true);
    //        设置笔帽
            abovePaint.setStrokeCap(Paint.Cap.BUTT);
            abovePaint.setStrokeWidth(UIUtils.dip2px(mContext, 20));
            abovePaint.setColor(ContextCompat.getColor(mContext, R.color.orange5));
    
    //         带透明度的画笔
            downPaint = new Paint();
            downPaint.setAntiAlias(true);
            //设置笔帽
            downPaint.setStrokeCap(Paint.Cap.BUTT);
            downPaint.setStrokeWidth(UIUtils.dip2px(mContext, 20));
            downPaint.setColor(ContextCompat.getColor(mContext, R.color.orangeWithAlpha));
    
            leftTextSize = UIUtils.dip2px(mContext, 31);
            topTextSize = UIUtils.dip2px(mContext, 28);
            bottomTextSize = UIUtils.dip2px(mContext, 25);
            topUnitRect = new Rect();
            leftTextRect = new Rect();
        }
    
        private void initViews() {
            tvLeft = findViewById(R.id.tvLeft);
            tvRight = findViewById(R.id.tvRight);
            viewBg = findViewById(R.id.viewBg);
            tvLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isRightNumberAndPeriod) {
                        animatorSetLeftClicked.start();
                    }
                }
            });
            tvRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!isRightNumberAndPeriod) {
                        animatorSetRightClicked.start();
                    }
                }
            });
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            setMeasuredDimension(UIUtils.dip2px(mContext, 355), UIUtils.dip2px(mContext, 255));
            if (heightList == null && heightList.isEmpty()) {
                return;
            }
            View viewChild = getChildAt(0);
            measureChild(viewChild, widthMeasureSpec, heightMeasureSpec);
            this.columnCount = heightList.size();
            this.spaceWithLeftRight = (getMeasuredWidth() - leftTextSize) / columnCount;
            this.markSizePx = (getMeasuredHeight() - topTextSize - bottomTextSize) / 10;
            this.markSize = this.heightest / 10;
            //        求文字的高度,文字随便写
            textPaintLeft.getTextBounds("content", 0, "content".length(), leftTextRect);
            topRegion = getMeasuredHeight() - UIUtils.dip2px(getContext(), 28) - 10 * markSizePx - leftTextRect.height();
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            View childAt = getChildAt(0);
            int right = getMeasuredWidth() - UIUtils.dip2px(getContext(), 12);
            int bottom = topRegion + leftTextRect.height();
            childAt.layout(right - childAt.getMeasuredWidth(), bottom - childAt.getMeasuredHeight(), right, bottom);
            initAnimator();
        }
    
        @Override
        protected void onDraw(@NonNull Canvas canvas) {
            super.onDraw(canvas);
            if (heightList == null && heightList.isEmpty()) {
                return;
            }
            for (int i = 0; i < heightList.size(); i++) {
                boolean isWithAlpha = heightList.get(i).getHeight() < this.heightest / 2;
                int startX = (int) (leftTextSize + spaceWithLeftRight * i + (spaceWithLeftRight - abovePaint.getStrokeWidth()) / 2);
                int startY = getHeight() - bottomTextSize;
                int endX = startX;
                int endY = (int) (startY - heightList.get(i).getHeight() / markSize * markSizePx);
                canvas.drawLine(startX, startY, endX, endY, isWithAlpha ? downPaint : abovePaint);
                canvas.drawText(bottomList.get(i), startX, getMeasuredHeight() - UIUtils.dip2px(getContext(), 12), textPaintDown);
            }
            for (int i = 0; i < 11; i++) {
                canvas.drawText(String.valueOf(i * markSize), UIUtils.dip2px(getContext(), 24), getMeasuredHeight() - UIUtils.dip2px(getContext(), 23) - i * markSizePx, textPaintLeft);
            }
    
            textPaintTop.getTextBounds(topUnit, 0, topUnit.length(), topUnitRect);
            canvas.drawText(topUnit, UIUtils.dip2px(getContext(), 10),
                    topRegion, textPaintTop);
            if (topUnitNote != null && !topUnitNote.isEmpty()) {
                canvas.drawText(topUnitNote, UIUtils.dip2px(getContext(), 10) + topUnitRect.width(),
                        topRegion, textPaintTopNote);
            }
        }
    
        public void setHeightList(List heightList, float heightest) {
            this.heightList = heightList;
    //        把float转换为大于该值得int
            this.heightest = (int) heightest + 1;
            String[] stringArray;
            if (!isRightNumberAndPeriod) {
                stringArray = getContext().getResources().getStringArray(R.array.number);
                topUnitNote = null;
            } else {
                stringArray = getContext().getResources().getStringArray(R.array.period);
                topUnitNote = getContext().getResources().getString(R.string.unit_session_note);
            }
            this.columnCount = heightList.size();
            this.spaceWithLeftRight = (getMeasuredWidth() - leftTextSize) / columnCount;
            topUnit = getContext().getResources().getString(R.string.unit_session);
            bottomList = Arrays.asList(stringArray);
            invalidate();
        }
    
        private void initAnimator() {
            valueAnimatorColorFromBlackToWhite = ValueAnimator.ofArgb(ContextCompat.getColor(mContext, R.color.black_333), ContextCompat.getColor(mContext, R.color.white));
            valueAnimatorColorFromBlackToWhite.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int animationValue = (int) animation.getAnimatedValue();
                    if (isRightNumberAndPeriod) {
                        tvLeft.setTextColor(animationValue);
                    } else {
                        tvRight.setTextColor(animationValue);
                    }
                }
            });
            valueAnimatorColorFromWhiteToBlack = ValueAnimator.ofArgb(ContextCompat.getColor(mContext, R.color.white), ContextCompat.getColor(mContext, R.color.black_333));
            valueAnimatorColorFromWhiteToBlack.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int animationValue = (int) animation.getAnimatedValue();
                    if (isRightNumberAndPeriod) {
                        tvRight.setTextColor(animationValue);
                    } else {
                        tvLeft.setTextColor(animationValue);
                    }
                }
            });
    
    //        注意,此时tvUp还没有改变位置。
            valueAnimatorToLeft = ValueAnimator.ofFloat(viewBg.getLeft() + viewBg.getWidth(), viewBg.getLeft());
            valueAnimatorToLeft.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                    float animatedValue = (float) animation.getAnimatedValue();
                    viewBg.layout((int) animatedValue, viewBg.getTop(), (int) animatedValue + viewBg.getWidth(), viewBg.getBottom());
                }
            });
            valueAnimatorToRight = ValueAnimator.ofFloat(viewBg.getLeft(), viewBg.getLeft() + viewBg.getWidth());
            valueAnimatorToRight.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                    float animatedValue = (float) animation.getAnimatedValue();
                    viewBg.layout((int) animatedValue, viewBg.getTop(), (int) animatedValue + viewBg.getWidth(), viewBg.getBottom());
                }
            });
            animatorSetLeftClicked = new AnimatorSet();
            AnimatorSet.Builder builderLeftClicked = animatorSetLeftClicked.play(valueAnimatorToLeft);
            builderLeftClicked.with(valueAnimatorColorFromBlackToWhite);
            builderLeftClicked.with(valueAnimatorColorFromWhiteToBlack);
            animatorSetLeftClicked.setDuration(animationTime);
            animatorSetLeftClicked.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
    
                }
    
                @Override
                public void onAnimationEnd(Animator animation) {
                    isRightNumberAndPeriod = false;
    //                initData(true);
                    switchButtonListenner.isRightClicked(false);
                    tvLeft.setTextColor(Color.WHITE);
                    tvRight.setTextColor(ContextCompat.getColor(mContext, R.color.black_333));
                }
    
                @Override
                public void onAnimationCancel(Animator animation) {
    
                }
    
                @Override
                public void onAnimationRepeat(Animator animation) {
    
                }
            });
    
            animatorSetRightClicked = new AnimatorSet();
            AnimatorSet.Builder builderRightClicked = animatorSetRightClicked.play(valueAnimatorToRight);
            builderRightClicked.with(valueAnimatorColorFromBlackToWhite);
            builderRightClicked.with(valueAnimatorColorFromWhiteToBlack);
            animatorSetRightClicked.setDuration(animationTime);
            animatorSetRightClicked.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
    
                }
    
                @Override
                public void onAnimationEnd(Animator animation) {
    //                initData2(false);
                    isRightNumberAndPeriod = true;
                    switchButtonListenner.isRightClicked(true);
                    tvRight.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                    tvLeft.setTextColor(ContextCompat.getColor(mContext, R.color.black_333));
                }
    
                @Override
                public void onAnimationCancel(Animator animation) {
    
                }
    
                @Override
                public void onAnimationRepeat(Animator animation) {
    
                }
            });
        }
    
        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
            initViews();
        }
    
        public interface SwitchButtonListenner {
            void isRightClicked(boolean isRight);
    
        }
    
        private SwitchButtonListenner switchButtonListenner;
    
        public void setSwitchButtonListenner(SwitchButtonListenner switchButtonListenner) {
            this.switchButtonListenner = switchButtonListenner;
        }
    }
    
    
    • 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

    Activity中调用

     private void initZhuZhuangChart() {
            histogramView.setSwitchButtonListenner(new HistogramView.SwitchButtonListenner() {
                @Override
                public void isRightClicked(boolean isRight) {
                    if (isRight) {
                        initData2();
                    } else {
                        initData();
                    }
                }
            });
            initData();
        }
    
        private void initData() {
            List histogramDataBeanList = new ArrayList<>();
            histogramDataBeanList.add(new HistogramDataBean(60.7f, true));
            histogramDataBeanList.add(new HistogramDataBean(100.7f, true));
            histogramDataBeanList.add(new HistogramDataBean(70.7f, true));
            histogramDataBeanList.add(new HistogramDataBean(10.7f, false));
            histogramDataBeanList.add(new HistogramDataBean(90.7f, true));
            histogramDataBeanList.add(new HistogramDataBean(40.7f, false));
            histogramDataBeanList.add(new HistogramDataBean(30.7f, false));
            histogramView.setHeightList(histogramDataBeanList, 100.7f);
        }
    
        private void initData2() {
            List histogramDataBeanList = new ArrayList<>();
            histogramDataBeanList.add(new HistogramDataBean(60.7f, true));
            histogramDataBeanList.add(new HistogramDataBean(100.7f, true));
            histogramDataBeanList.add(new HistogramDataBean(10.7f, false));
            histogramDataBeanList.add(new HistogramDataBean(90.7f, true));
            histogramDataBeanList.add(new HistogramDataBean(40.7f, false));
            histogramDataBeanList.add(new HistogramDataBean(30.7f, false));
            histogramView.setHeightList(histogramDataBeanList, 100.7f);
        }
    
    • 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
  • 相关阅读:
    Flink + Hudi 实现多流拼接(大宽表)
    forms组件补充与ModelForm简单使用与cookie与session
    C++怎么读取XML文件?
    【毕业设计】大数据电商销售预测分析 - python 数据分析
    LabVIEW和NIUSRP硬件加快了认知无线电开发
    webpack编译运行了两次
    用户订阅付费如何拆解分析?看这篇就够了
    java毕业生设计校园社团管理系统计算机源码+系统+mysql+调试部署+lw
    详解SurfaceView和TextureView
    AI类APP上线需要注意的问题
  • 原文地址:https://blog.csdn.net/qczg_wxg/article/details/134330821