android耗时操作也是操作UI的,如何处理

查看:718
本文介绍了android耗时操作也是操作UI的,如何处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

private void addPolylineInPlayGround() {

    List<Integer> colorList = new ArrayList<Integer>();
    List<BitmapDescriptor> bitmapDescriptors = new ArrayList<BitmapDescriptor>();
    int[] colors = new int[]{Color.argb(255, 0, 255, 0), Color.argb(255, 255, 255, 0), Color.argb(255, 255, 0, 0)};

    //用一个数组来存放纹理
    List<BitmapDescriptor> textureList = new ArrayList<BitmapDescriptor>();
    textureList.add(BitmapDescriptorFactory.fromResource(R.drawable.custtexture));

    List<Integer> texIndexList = new ArrayList<Integer>();
    texIndexList.add(0);//对应上面的第0个纹理
    texIndexList.add(1);
    texIndexList.add(2);

    Random random = new Random();
    for (int i = 0; i < latLngs.size(); i++) {
        colorList.add(colors[random.nextInt(3)]);
        bitmapDescriptors.add(textureList.get(0));
    }
    mPolyline = mAMap.addPolyline(new PolylineOptions().setCustomTexture(BitmapDescriptorFactory.fromResource(R.drawable.custtexture)) //setCustomTextureList(bitmapDescriptors)
            .setCustomTextureIndex(texIndexList)
            .addAll(latLngs)
            .useGradient(true)
            .width(18));
    dismissLoading();
    LatLngBounds bounds = new LatLngBounds(latLngs.get(0), latLngs.get(latLngs.size() - 2));
    mAMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 200));
}

//先是通过网络请求获取比较大量的经纬度点,然后我会在地图上根据点绘制成线。现在关键问题就是线在绘制过程中会比较耗时,页面会卡住(这样并不好)。因为数据量会大一点,但关键子线程的画 这个线又画不出来,在UI线程又会卡死。求助....

ps:试过在子线程中用runonuithread 去操作 中间的 addPolyLine ,但也不行。

这个方法在请求完数据后就直接执行了。

解决方案

先在子线程画,然后通过handler在主线程刷新界面。
你要用rxjava的话实现就很方便了

这篇关于android耗时操作也是操作UI的,如何处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆