3

I've set an image as background on a linearlayout . This is the code :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/mainbg"
    android:orientation="vertical"
    android:padding="5dp"
    >

The problem is , the width of the image is about 2000px , I don't want to scale down the with to fit the screen , I just want to show as much as it take as background and not strech it .

how can I do so ?

thanks

navidjons
  • 501
  • 4
  • 9
  • 15

3 Answers3

7

add an image view in your layout and set its scaletype to centerCrop

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="5dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/mainbg" />

</LinearLayout>

check image scale types: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

Milad Nouri
  • 1,587
  • 1
  • 21
  • 32
5

You can use RelativeLayout and put ImageView with scaleType inside it and LinearLayout having all your content.

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<ImageView 
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:src="@drawable/mainbg"
   android:scaleType="centerCrop"/>

  <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     android:padding="5dp">

    -----Your Content----

  </LinearLayout>
</RelativeLayout>
Ankii Rawat
  • 2,070
  • 17
  • 17
  • This answer works for me, just be careful with the L letter in the middle of Relativelayout closing tag needs to be capital L. – Techi50 Feb 11 '19 at 10:53
0

You can do things in two way...

  1. Fit the full image (without cropping/leaving portion of photo). Use Vertical ScrollView + Horizontal ScrollView combined and use you layout as first child.

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ScrollView>
    <HorizontalScrollView
        android:id="@+id/horizontalScrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/a"
        android:orientation="vertical">
    </LinearLayout>
    
  2. You can use custom view

customScrollView

package com.scrollable.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class VScroll extends ScrollView {

    public VScroll(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VScroll(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VScroll(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }
}

And you can refer Scrollview vertical and horizontal in android...

Community
  • 1
  • 1
Tushar Sheth
  • 391
  • 4
  • 18