Giter Site home page Giter Site logo

customviewstudy's Introduction

CustomViewStudy

主要参照Hongyang的CSDN博客所写,具体细节优化请见这里。如有错误欢迎指正,如有侵权,请联系我删除。

Blog Catalogue

Source Code

Picture

Code Optimization

1.1 关于文字显示优化:

//             int textWidth = mRect.width(); // 这样mRect.width()直接计算出来的会有误差
               float textWidth = mPaint.measureText(mTitleText);

//              int textHeight = mRect.height(); //直接计算出来的会有误差
                Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
                float textHeight = Math.abs((fontMetrics.bottom - fontMetrics.top));

1.2 onDraw里画Text时起点坐标优化:

canvas.drawText(mTitleText, getWidth() / 2 - mRect.width() / 2 - mRect.left, getHeight() / 2 + mRect.height() / 2, mPaint);

canvas.drawText(String text,float x,float y,Paint paint); x和y是绘制时的起点坐标(左下角);

" - mRect.left": 就很标准,居中显示(csdn:yql_running解决)

2. 圆环交替 等待效果优化

2.1 新开线程画线,离开页面时线程未关闭优化

// 用来开关线程
    private boolean isContinue;
    
    // 绘图线程
        new Thread() {
            public void run() {
                while (isContinue) {
                    mProgress++;
                    if (mProgress == 360) {
                        mProgress = 0;
                        isNext = !isNext;
                    }
                    Log.e("--------", "在执行..");
                    postInvalidate();

                    try {
                        Thread.sleep(100 / mSpeed);// 这里优化了一下,值越大,速度越快
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }.start();

Activity相关代码:

@Override
    protected void onStop() {
        super.onStop();
        customProgressBar01.setContinue(false);
        customProgressBar02.setContinue(false);
        customProgressBar03.setContinue(false);
    }

2.2 用户宽高若设置wrap_content时优化

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);

        if (modeWidth == MeasureSpec.EXACTLY) {
            width = sizeWidth;
        } else {//默认宽度200dp
            width = (int) getContext().getResources().getDimension(R.dimen.width);
        }
        Log.e("------------->", "width:" + width);
        setMeasuredDimension(width, width);
    }

Download

Reference

Thanks

customviewstudy's People

Contributors

youlookwhat avatar jinbeen avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.