Android: Cloning a drawable in order to make a StateListDrawable with filters

前端 未结 7 816
梦谈多话
梦谈多话 2020-11-29 18:32

I\'m trying to make a general framework function that makes any Drawable become highlighted when pressed/focused/selected/etc.

My function takes a D

7条回答
  •  遥遥无期
    2020-11-29 18:52

    Get clone drawable using newDrawable() but make sure it is mutable otherwise your clone effect gone, I used these few lines of code and it is working as expected. getConstantState() may be null as suggested by annotation, so handle this RunTimeException while you cloning drawable.

    Drawable.ConstantState state = d.mutate().getConstantState();
    if (state != null) {
        Drawable drawable = state.newDrawable().mutate();
    }
    

提交回复
热议问题