Make bitmap drawable tile in x but stretch in y

前端 未结 4 2177
渐次进展
渐次进展 2021-02-09 06:48

I have an image which I\'m using as a background for a RelativeLayout. The image needs to be tiled horizontally to make a pattern.

I\'m able to get the image to tile ho

4条回答
  •  耶瑟儿~
    2021-02-09 07:16

    Far too late for you, but may be useful for others.

    You can create a custom View in order to do this. Simply scale your source bitmap to be as high as your view and then draw it repeatedly on the canvas:

    public class RepeatingXImageView extends View {
    
    Bitmap bitmap;
    Paint paint;
    
    public RepeatingXImageView(Context context) {
        super(context);
    }
    
    public RepeatingXImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public RepeatingXImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if(changed) {
            paint = new Paint();
            bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.seekbar_overlay);
            bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bottom - top, false);
        }
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(bitmap == null) return;
        int left = 0;
        while(left < getWidth()) {
            canvas.drawBitmap(bitmap, left, 0, paint);
            left += bitmap.getWidth();
        }
    }
    }
    

提交回复
热议问题