banner icon indicating copy to clipboard operation
banner copied to clipboard

图片指示器 两张图片大小不一致的处理

Open QiaoYang0923 opened this issue 2 years ago • 0 comments

`public class MyDrawableIndicator extends BaseIndicator { private Bitmap normalBitmap; private Bitmap selectedBitmap;

/**
 * 实例化Drawable指示器 ,也可以通过自定义属性设置
 *
 * @param context
 * @param normalResId
 * @param selectedResId
 */
public MyDrawableIndicator(Context context, @DrawableRes int normalResId, @DrawableRes int selectedResId) {
    super(context);
    normalBitmap = BitmapFactory.decodeResource(getResources(), normalResId);
    selectedBitmap = BitmapFactory.decodeResource(getResources(), selectedResId);
}

public MyDrawableIndicator(Context context) {
    this(context, null);
}

public MyDrawableIndicator(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public MyDrawableIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DrawableIndicator);
    if (a != null) {
        BitmapDrawable normal = (BitmapDrawable) a.getDrawable(R.styleable.DrawableIndicator_normal_drawable);
        BitmapDrawable selected = (BitmapDrawable) a.getDrawable(R.styleable.DrawableIndicator_selected_drawable);
        normalBitmap = normal.getBitmap();
        selectedBitmap = selected.getBitmap();
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int count = config.getIndicatorSize();
    if (count <= 1) {
        return;
    }
    setMeasuredDimension(normalBitmap.getWidth() * (count - 1) +
                    selectedBitmap.getWidth() + config.getIndicatorSpace() * (count - 1),

            Math.max(normalBitmap.getHeight(), selectedBitmap.getHeight()));
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int count = config.getIndicatorSize();
    if (count <= 1 || normalBitmap == null || selectedBitmap == null) {
        return;
    }

    float left = 0;
    for (int i = 0; i < count; i++) {
        canvas.drawBitmap(config.getCurrentPosition() == i ? selectedBitmap : normalBitmap, left, 0, mPaint);
        left += (config.getCurrentPosition() == i ? selectedBitmap.getWidth() : normalBitmap.getWidth()) + config.getIndicatorSpace();
    }
}

} `

QiaoYang0923 avatar May 24 '22 02:05 QiaoYang0923