Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

setFocusResId()和setNormalResId()无效问题附解决办法 #128

Open
wenzhihao123 opened this issue May 9, 2019 · 0 comments
Open

setFocusResId()和setNormalResId()无效问题附解决办法 #128

wenzhihao123 opened this issue May 9, 2019 · 0 comments

Comments

@wenzhihao123
Copy link

自定义指示器的xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="10dp"
        android:height="1dp"/>
    <solid android:color="#99FFFFFF"/>
</shape>
...省略另一个...

设置进去无效,不显示.
后来看源码看到,UltraViewPagerIndicator里面是直接decode资源的:

focusBitmap = BitmapFactory.decodeResource(getResources(), focusResId);

这里需要考虑资源可能是GradientDrawable,因此需要把Drawable转成Bitmap:

/**
     * 根据resId获取到bitmap,自定义的
     *
     * @param context
     * @param resId
     * @return
     */
   public static Bitmap convertResToBitmap(Context context, int resId) {

        try {
            Drawable drawable = ContextCompat.getDrawable(context, resId);
            //这个是图片资源
            if (drawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) drawable).getBitmap();
            }
            //这个可能是自定义drawable
            if (drawable instanceof GradientDrawable) {
                Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                        drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
                drawable.draw(canvas);
                return bitmap;
            }
            return BitmapFactory.decodeResource(context.getResources(), resId);
        } catch (Exception e) {
        }
        return null;
    }

然后改调用:

setFocusIcon(convertResToBitmap(context, R.drawable.banner_indicator_selected))
setNormalIcon(convertResToBitmap(context, R.drawable.banner_indicator_normal))

踩过的坑,希望能帮助到其他人~

@wenzhihao123 wenzhihao123 changed the title setFocusResId()和setNormalResId()无效 setFocusResId()和setNormalResId()无效问题附解决办法 May 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant