简体   繁体   English

将Drawable调整为全屏

[英]Resize a Drawable to full screen

I'm downloading a bitmap from internet, then I cast this bitmap as a Drawable to set it as a background in my LinearLayout. 我正在从互联网上下载一个位图,然后我将这个位图转换为Drawable,将其设置为我的LinearLayout中的背景。

I see that unfortunately the image is scaled to fill the view, how can I scale the image until the smallest size reach the border (so it's not stretched)? 我看到不幸的是图像被缩放以填充视图,我如何缩放图像直到最小尺寸到达边界(因此它没有被拉伸)? (And possibly centered) (并且可能居中)

I've tried something like this but without any success. 我尝试过类似的东西但没有任何成功。 Am I on the way? 我在路上吗?

        LinearLayout layout = (LinearLayout) context.findViewById(R.id.main);
        Drawable picture = new BitmapDrawable(context.getResources(), bitmap);
        Display display = context.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        picture.setBounds(0, 0, size.x, size.y);
        layout.setBackgroundDrawable(picture);

I think you need something like this? 我想你需要这样的东西?

 setScaleType(ScaleType.CENTER_CROP);

See http://developer.android.com/reference/android/widget/ImageView.ScaleType.html 请参阅http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

I think,It is not advisable use scaled images on an app. 我认为,不建议在应用程序上使用缩放图像。 It's a best practice use a repeated pattern instead. 最佳实践是使用重复模式。 So, I would try something like that: 所以,我会尝试这样的事情:

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background-image-repeatable-pattern.png" 
android:tileMode="repeat" />

Save this to drawable/background.xml file (for example) 将其保存为drawable / background.xml文件(例如)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM