Open wenzhihao123 opened 5 years ago
自定义指示器的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))
踩过的坑,希望能帮助到其他人~
自定义指示器的xml
设置进去无效,不显示. 后来看源码看到,UltraViewPagerIndicator里面是直接decode资源的:
这里需要考虑资源可能是GradientDrawable,因此需要把Drawable转成Bitmap:
然后改调用:
踩过的坑,希望能帮助到其他人~