新闻类App (MVP + RxJava + Retrofit+

2019-04-28  本文已影响0人  Peakmain

Github地址:新闻类App (MVP + RxJava + Retrofit+Dagger+ARouter)

列表页卡顿优化

常规方案

布局相关

图片相关

线程相关

TextView优化

@Deprecated
public class CustomTextView extends View {
    private String text="测试StaticLayout";
    private TextPaint mTextPaint;
    private StaticLayout mStaticLayout;
    public CustomTextView(Context context) {
        this(context,null);
    }

    public CustomTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLableView();
    }

    private void initLableView() {
        mTextPaint=new TextPaint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(16*getResources().getDisplayMetrics().densityDpi);
        mTextPaint.setColor(Color.BLACK);
        int width= (int) mTextPaint.measureText(text);
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                mStaticLayout = new StaticLayout(text, mTextPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);
                postInvalidate();
            }
        });
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mStaticLayout != null){
            canvas.save();
            canvas.translate(getPaddingLeft(), getPaddingTop());
            mStaticLayout.draw(canvas);
            canvas.restore();
        }
    }
}

其它

存储优化

常规方案

SharePreference相关

SharePreference替代者MMKV

 implementation 'com.tencent:mmkv:1.0.19'
        MMKV.initialize(this);
        MMKV.defaultMMKV().encode("times",100);
        MMKV.defaultMMKV().decodeInt("times");

日志存储优化

WebView异常监控

推荐库:https://github.com/Tencent/VasSonic

public class BlankDetect {

    /**
     * 判断Bitmap是否都是一个颜色
     */
    public static boolean isBlank(View view) {
        Bitmap bitmap = getBitmapFromView(view);

        if (bitmap == null) {
            return true;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        if (width > 0 && height > 0) {
            int originPix = bitmap.getPixel(0, 0);
            int[] target = new int[width];
            Arrays.fill(target, originPix);
            int[] source = new int[width];
            boolean isWhiteScreen = true;
            for (int col = 0; col < height; col++) {
                bitmap.getPixels(source, 0, width, 0, col, width, 1);
                if (!Arrays.equals(target, source)) {
                    isWhiteScreen = false;
                    break;
                }
            }
            return isWhiteScreen;
        }
        return false;
    }

    /**
     * 从View获取转换到的Bitmap
     */
    private static Bitmap getBitmapFromView(View view){
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        if (Build.VERSION.SDK_INT >= 11) {
            view.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(view.getHeight(), View.MeasureSpec.EXACTLY));
            view.layout((int) view.getX(), (int) view.getY(), (int) view.getX() + view.getMeasuredWidth(), (int) view.getY() + view.getMeasuredHeight());
        } else {
            view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        }
        view.draw(canvas);
        return bitmap;
    }
}
上一篇下一篇

猜你喜欢

热点阅读