浏览代码

Fixes #1903: Fix image scaling in navigation drawer

* Insted of setting a background in ViewGroup add a ImageView inside it, stretch it, set cropping scaleType and then set a background image downloaded from server.
* CenterCrop will scale the image to fill the View, if needed image will be cropped in the center but the aspect ration will be kept
* Cleanup code to use more general types instead of direct layout type, previously enforcing LinearLayout wasn't using anything special about this class.
Bartosz Przybylski 7 年之前
父节点
当前提交
0e1dc14016

+ 10 - 14
src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -46,6 +46,7 @@ import android.text.TextUtils;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
+import android.view.ViewGroup;
 import android.webkit.URLUtil;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
@@ -1042,7 +1043,7 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
     public void updateHeaderBackground() {
         if (getAccount() != null &&
                 getStorageManager().getCapability(getAccount().name).getServerBackground() != null) {
-            final LinearLayout navigationHeader = (LinearLayout) findNavigationViewChildById(R.id.drawer_header_view);
+            final ViewGroup navigationHeader = (ViewGroup) findNavigationViewChildById(R.id.drawer_header_view);
 
             if (navigationHeader != null) {
                 String background = getStorageManager().getCapability(getAccount().name).getServerBackground();
@@ -1055,12 +1056,7 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
                         public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
                             Drawable[] drawables = {new ColorDrawable(primaryColor), resource};
                             LayerDrawable layerDrawable = new LayerDrawable(drawables);
-                            
-                            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
-                                navigationHeader.setBackgroundDrawable(layerDrawable);
-                            } else {
-                                navigationHeader.setBackground(layerDrawable);
-                            }
+                            setNavigationHeaderBackground(layerDrawable, navigationHeader);
                         }
 
                         @Override
@@ -1068,12 +1064,7 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
                             Drawable[] drawables = {new ColorDrawable(primaryColor),
                                     getResources().getDrawable(R.drawable.background)};
                             LayerDrawable layerDrawable = new LayerDrawable(drawables);
-
-                            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
-                                navigationHeader.setBackgroundDrawable(layerDrawable);
-                            } else {
-                                navigationHeader.setBackground(layerDrawable);
-                            }
+                            setNavigationHeaderBackground(layerDrawable, navigationHeader);
                         }
                     };
 
@@ -1086,12 +1077,17 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
                             .into(target);
                 } else {
                     // plain color
-                    navigationHeader.setBackgroundColor(ThemeUtils.primaryColor(getAccount()));
+                    setNavigationHeaderBackground(new ColorDrawable(primaryColor), navigationHeader);
                 }
             }
         }
     }
 
+    private void setNavigationHeaderBackground(Drawable drawable, ViewGroup navigationHeader) {
+        final ImageView background = navigationHeader.findViewById(R.id.drawer_header_background);
+        background.setImageDrawable(drawable);
+    }
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

+ 9 - 3
src/main/res/layout/drawer_header.xml

@@ -18,13 +18,19 @@
   You should have received a copy of the GNU Affero General Public
   License along with this program. If not, see <http://www.gnu.org/licenses/>.
   -->
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:id="@+id/drawer_header_view"
               android:layout_width="match_parent"
               android:layout_height="@dimen/nav_drawer_header_height"
-              android:background="@drawable/background"
               android:fitsSystemWindows="true">
 
+    <ImageView
+        android:id="@+id/drawer_header_background"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:src="@drawable/background"
+        android:scaleType="centerCrop"/>
+
     <RelativeLayout
         android:id="@+id/drawer_active_user"
         android:layout_width="match_parent"
@@ -129,4 +135,4 @@
 
     </RelativeLayout>
 
-</LinearLayout>
+</FrameLayout>