Make bitmap drawable tile in x but stretch in y

前端 未结 4 2240
渐次进展
渐次进展 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条回答
  •  猫巷女王i
    2021-02-09 07:08

    I feel it is straight forward:(This code will tile in Y and repeat in x)

    In your onWindowFoucsChanged you call:

     public void onWindowFocusChanged(boolean hasFocus) {
            // TODO Auto-generated method stub
            super.onWindowFocusChanged(hasFocus);
            Drawable d = getRepeatingBG(this, R.drawable.image_that_you_want_to_repeat);
            body_view.setBackgroundDrawable(d);
    
        }
    
    private Drawable getRepeatingBG(Activity activity, int center)
        {   
    
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inScaled=true;
    
            Bitmap center_bmp = BitmapFactory.decodeResource(activity.getResources(), center, options);
            center_bmp.setDensity(Bitmap.DENSITY_NONE);
            center_bmp=Bitmap.createScaledBitmap(center_bmp, dm.widthPixels , center_bmp.getHeight(), true);
    
            BitmapDrawable center_drawable = new BitmapDrawable(activity.getResources(),center_bmp);
    //change here setTileModeY to setTileModeX if you want to repear in X
            center_drawable.setTileModeY(Shader.TileMode.REPEAT);
    
            return center_drawable;
        }
    

提交回复
热议问题