Make bitmap drawable tile in x but stretch in y

前端 未结 4 2015
渐次进展
渐次进展 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

    This method invokes creation of new bitmap but it looks like it's acrhiving your goal

            View view = findViewById(R.id.layout);
            BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.tile);
            int width = view.getWidth();
            int intrinsicHeight = bd.getIntrinsicHeight();
            Rect bounds = new Rect(0,0,width,intrinsicHeight);
            bd.setTileModeX(TileMode.REPEAT);
            bd.setBounds(bounds);
            Bitmap bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), bd.getBitmap().getConfig());
            Canvas canvas = new Canvas(bitmap);
            bd.draw(canvas);
            BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
            view.setBackground(bitmapDrawable);
    

    Please note that it only works if view was already layouted, so a method lile onWindowFocusChanged is a good place for this code.

提交回复
热议问题