misakuo / 3dTagCloudAndroid

Tagcloud component for android
MIT License
1.65k stars 291 forks source link

子线程更新星球需要反射 #34

Open jiarWang opened 3 years ago

jiarWang commented 3 years ago

主线程更新TagCloudView出现较多jank,可以尝试使用单独的非主线程进行维护进行优化,但是目前的TagClouldView限制了更新线程只能在主线程

解决方案:https://github.com/misakuo/3dTagCloudAndroid/pull/33/commits

如下方法可以自定义一个线程,并在线程中维护TagCloudView

//: MyThread.java    
@Override
    public void run() {
        Looper.prepare();
        mHandler = new Handler(Looper.myLooper());

        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT, Utils.dx2dp(400),
                TYPE_APPLICATION, 0, PixelFormat.TRANSPARENT);
        layoutParams.gravity = Gravity.TOP;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mWindowManager.addView(mFrameLayout = new FrameLayout(mContext),
                layoutParams
        );
        mFrameLayout.setVisibility(View.GONE);
        onViewCreated(mFrameLayout);

        Looper.loop();
    }

    private void onViewCreated(FrameLayout frameLayout) {
        frameLayout.setVisibility(View.VISIBLE);
        if (frameLayout.getChildCount() == 0){
            LayoutInflater.from(mContext.getApplicationContext())
                    .inflate(R.layout.layout_planet, frameLayout);
        }
        frameLayout.setVisibility(View.VISIBLE);
        initCloudView(frameLayout);
    }

    private void initCloudView(FrameLayout frameLayout) {
        if (frameLayout.getParent() == null ) return;
        TagCloudView tagCloudView = frameLayout.findViewById(R.id.tag_cloud);
        try {
            Field handler = tagCloudView.getClass().getDeclaredField("handler");
            handler.setAccessible(true);
            handler.set(tagCloudView, new Handler(Looper.myLooper()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        TagCloudAdapter adapter = new TagCloudAdapter();
        tagCloudView.setAdapter(adapter);
    }