Android 线性布局后退地面比例可拉伸保持纵横比

Android 线性布局后退地面比例可拉伸保持纵横比,android,layout,Android,Layout,我正在尝试使用位图绘制为具有linearlayout的活动创建模糊背景。 原始位图和模糊位图类似于100x100。我想根据手机分辨率(例如1080p,它不是正方形)来统一缩放。我在运行时得到背景图标 LinearLayout layout = (LinearLayout) findViewById(R.id.mylinearlayout); Bitmap bmp = imgLoader.getBitmap(iconUrl); //Blur using this thr

我正在尝试使用位图绘制为具有linearlayout的活动创建模糊背景。 原始位图和模糊位图类似于100x100。我想根据手机分辨率(例如1080p,它不是正方形)来统一缩放。我在运行时得到背景图标

    LinearLayout layout = (LinearLayout) findViewById(R.id.mylinearlayout);

    Bitmap bmp = imgLoader.getBitmap(iconUrl);

    //Blur using this thread http://stackoverflow.com/questions/14780006/transparent-blurry-view-which-blurs-layout-underneath
    Bitmap blurredBmp = CommonUtils.blurBitmap(bmp, Constants.RADIUS);
    BitmapDrawable bmpdrawable = new BitmapDrawable(getResources(),blurredBmp);
    bmpdrawable.setAlpha(Constants.ALPHA);

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        layout.setBackgroundDrawable(bmpdrawable);
    } else {
        layout.setBackground(bmpdrawable);
    } 
LinearLayout布局=(LinearLayout)findViewById(R.id.mylinearlayout);
位图bmp=imgLoader.getBitmap(iconUrl);
//使用此线程进行模糊http://stackoverflow.com/questions/14780006/transparent-blurry-view-which-blurs-layout-underneath
位图blurredBmp=CommonUtils.blurBitmap(bmp,常量.RADIUS);
BitmapDrawable bmpdrawable=新的BitmapDrawable(getResources(),blurredBmp);
bmpdrawable.setAlpha(常数.ALPHA);
if(android.os.Build.VERSION.SDK\u INT
现在,问题是: 当我设置背景时,100x100基本上被拉伸到非方形手机分辨率,看起来很奇怪

我想要的是:用拉伸位图填充整个活动背景。它的一部分,如果被剪辑,但它的高宽比需要保持

我尝试的:设置布局背景的许多线程建议使用imageview作为元素之一+使用重力和填充父元素。我不想这样做,因为我有其他的图像显示在屏幕上

创建模糊位图或发送不同的宽度/高度时,我尝试了Bitmap.createScaledBitmap,但它不起作用。任何帮助都将不胜感激

仅仅将此标记为另一个线程的副本而不了解问题也不酷

谢谢

好的,这很有效

    LinearLayout layout = (LinearLayout) findViewById(R.id.testreportlinearlayout);

    Bitmap bmp = imgLoader.getBitmap(iconUrl);
    Bitmap blurredBmp = CommonUtils.blurBitmap(bmp, Constants.RADIUS);

    Bitmap scaledBmp = Bitmap.createScaledBitmap(blurredBmp, MAX_OF_NEW_XY, MAX_OF_NEW_XY, false);
    BitmapDrawable scaledBmpdrawable = new BitmapDrawable(getResources(), scaledBmp);
    scaledBmpdrawable.setAlpha(Constants.ALPHA);
    scaledBmpdrawable.setGravity(Gravity.CENTER);

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        layout.setBackgroundDrawable(scaledBmpdrawable);
    } else {
        layout.setBackground(scaledBmpdrawable);
    }
LinearLayout布局=(LinearLayout)findViewById(R.id.testreportlinearlayout);
位图bmp=imgLoader.getBitmap(iconUrl);
位图blurredBmp=CommonUtils.blurBitmap(bmp,常量.RADIUS);
位图缩放BMP=Bitmap.createScaledBitmap(模糊BMP,最大值为新值,最大值为新值,为假);
BitmapDrawable scaledBmpdrawable=新的BitmapDrawable(getResources(),scaledBmp);
scaledBmpdrawable.setAlpha(常数.ALPHA);
缩放bmpdrawable.setGravity(重力.中心);
if(android.os.Build.VERSION.SDK\u INT
对于这个问题有很多解决方案

当您只设置为颜色时

ShapeDrawable bgShape = (ShapeDrawable )parentLayout.getBackground();
bgShape.getPaint().setColor(Color.BLACK);

扩展BitmapDrawable并在
draw(Canvas)
方法中进行自定义绘图,最简单的方法是使用Canvas.drawBitmap和矩阵参数,不要使用任何位图。createScaledBitmap,改为缩放画布(直接或间接)。这是一个糟糕的解决方案:不要使用Bitmap.createScaledBitmap,因为它会增加内存使用,使用画布缩放代替位图缩放