【Android】问题记录

这篇博客总结了Android开发中遇到的一些问题及其解决方案,包括RecyclerView的divider设置、BitmapDrawable转换、图片加载优化、Dialog布局限制、物理按键监听、系统栏透明设置以及RecyclerView的预加载策略,提供了相应的代码片段和库的推荐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.RecyclerView设置divider:
链接
代码方法的话比较麻烦,如果只是想设置简单的divider,直接在item的layout里画一条线就可以了

2.BitmapDrawable from TransitionDrawable

android.graphics.drawable.TransitionDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
链接
//in this case your solution would be right, one can just cast it, //but if not (and that is your case) one needs to do something like //that:


private Bitmap getBitmap(Drawable icon) {
        Bitmap result;
        if (icon instanceof BitmapDrawable) {
            result = ((BitmapDrawable) icon).getBitmap();
        } else {
            Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            icon.draw(canvas);
            result = bitmap;
        }
        return result;
    }

3.RecyclerView 加载url图片,平滑滚动的问题: Glide开源项目
listview 为了保持图片位置不错乱,需要setTag;
改用recycler后,从volley换到glide,需要去掉setTag的代码

You must not call setTag() on a view Glide is targeting

这里有picasso跟glide的性能测评
设置imageView的scaleType时需要注意的问题
Glide的高级应用

对Glide请求回来的图片设置事件、或者保存bitmap

 Glide.with(context)
         .load("http://goo.gl/h8qOq7")
         .asBitmap()
         .into(new BitmapImageViewTarget(imageView) {
                     @Override
                     protected void setResource(Bitmap resource) {
                         // Do bitmap magic here
                         super.setResource(resource);
                     }
         });

一些基于Glide的优秀库
1.glide-transformations

一个基于Glide的transformation库,拥有裁剪,着色,模糊,滤镜等多种转换效果,赞的不行不行的~~

2.GlidePalette

一个可以在Glide加载时很方便使用Palette的库。

4.Dialog的layout的布局元素不能设置xml属性

android:onClick ="onClick"

否则报错

5.监控物理按键:菜单、返回、home

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            // 监控返回键
//            }
//        }
//        else if(keyCode == KeyEvent.KEYCODE_MENU) {
//            // 监控菜单键
////            return false;
//        }
        return super.onKeyDown(keyCode, event);
    }

6.设置系统栏透明(not setted)
用到了开源项目 SystemBarTintManager,并且测试发现效果跟机型有关系;


    private SystemBarTintManager tintManager;

    @TargetApi(19)
    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintColor(getResources().getColor(R.color.white));
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarDarkMode(false, this);//false 状态栏字体颜色是白色 true 颜色是黑色
        }
    }

7.RecyclerView预加载/缓存的问题
项目需求,在item比较复杂,有图片需要从服务器get的时候,recyclerView在滚动时会出现卡顿的现象;
google了很久,找到一个解决方案:
重写LinearLayoutManager的如下方法:

Protected Methods
protected int getExtraLayoutSpace (RecyclerView.State state)

Returns the amount of extra space that should be laid out by LayoutManager. By default, LinearLayoutManager lays out 1 extra page of items while smooth scrolling and 0 otherwise. You can override this method to implement your custom layout pre-cache logic.

Laying out invisible elements will eventually come with performance cost. On the other hand, in places like smooth scrolling to an unknown location, this extra content helps LayoutManager to calculate a much smoother scrolling; which improves user experience.

You can also use this if you are trying to pre-layout your upcoming views.

Returns
The extra space that should be laid out (in pixels).
protected boolean isLayoutRTL ()

代码如下:

public class MyLinearLayoutManager extends LinearLayoutManager {
    public MyLinearLayoutManager(Context context) {
        super(context);
    }

    @Override
    protected int getExtraLayoutSpace(RecyclerView.State state) {
//        return super.getExtraLayoutSpace(state);

        // 2015-11-12 22:24:54 by Chen
        // recycler view 预加载item数目
        return 10;
    }
}

这样,recycler就会有预先缓存的效果了

如果预存也达不到平滑滚动的效果,或许可以看看我这里的笔记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值