0

Hi guys I am trying to load a image from a url and after loading that i am trying to re scale it such that it fits the whole screen after that the text and the buttons present are available below the image which are wrapped around using scroll view

this is my fragment

public class FirstFragment extends Fragment {

    ImageView im;
    Bitmap bitmap;
    Drawable dr;
    Bitmap bitap;
    Bitmap newBitmap;
    RelativeLayout rel;
    View v;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         v = inflater.inflate(R.layout.first_frag, container, false);

        rel = (RelativeLayout) v.findViewById(R.id.relativla);
        rel.setVisibility(View.INVISIBLE);


        new LoadImage().execute("http://opinions.esy.es/bg.jpg");

        Button b = (Button) v.findViewById(R.id.navi);
         im = (ImageView) v.findViewById(R.id.imageView);




        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), Navi.class);
                startActivity(i);
            }
        });


        return v;
    }

    public static FirstFragment newInstance(String text) {

        FirstFragment f = new FirstFragment();
        Bundle b = new Bundle();
        b.putString("msg", text);

        f.setArguments(b);

        return f;
    }
    private class LoadImage extends AsyncTask<String, String, Bitmap> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();


        }
        protected Bitmap doInBackground(String... args) {
            try {
                bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());

            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }


        protected void onPostExecute(Bitmap image) {

            if(image != null){
                dr = new BitmapDrawable(getResources(),image);
                bitap = ((BitmapDrawable) dr).getBitmap();
                float scalingFactor = getBitmapScalingFactor(bitap);
                Bitmap newBitmap = Util.ScaleBitmap(bitmap, scalingFactor);

               im.setImageBitmap(newBitmap);





            }else{


                Toast.makeText(getContext(), "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();

            }

        }
    }
    private float getBitmapScalingFactor(Bitmap bm) {
        Toast.makeText(getContext(),"entered here",Toast.LENGTH_LONG).show();
        // Get display width from device
        int displayWidth = getActivity().getWindowManager().getDefaultDisplay().getWidth();

        // Get margin to use it for calculating to max width of the ImageView
        RelativeLayout.LayoutParams layoutParams =
                (RelativeLayout.LayoutParams)this.im.getLayoutParams();
        int leftMargin = layoutParams.leftMargin;
        int rightMargin = layoutParams.rightMargin;

        // Calculate the max width of the imageView
        int imageViewWidth = displayWidth - (leftMargin + rightMargin);
        rel.setVisibility(View.VISIBLE);
        // Calculate scaling factor and return it
        return ( (float) imageViewWidth / (float) bm.getWidth() );

    }
}

My Util class

public class Util {
    public static Bitmap ScaleBitmap(Bitmap bm, float scalingFactor) {
        int scaleHeight = (int) (bm.getHeight() * scalingFactor);
        int scaleWidth = (int) (bm.getWidth() * scalingFactor);

        return Bitmap.createScaledBitmap(bm, scaleWidth, scaleHeight, true);
    }

}

XML File

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView"
    android:fillViewport="true"

    >

    <RelativeLayout

        android:id="@+id/relativla"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        >
        <Button
            android:layout_width="30dp"
            android:layout_height="30dp"

            android:background="@drawable/hamburger"
            android:id="@+id/navi"
            android:padding="10dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:id="@+id/imageView"

            android:scaleType="fitCenter"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Diet Plans"

            android:padding="10dp"
            android:layout_marginTop="10dp"
            android:textColor="@android:color/black"
            android:textSize="25sp"
            android:id="@+id/textView5"
            android:layout_below="@+id/imageView"
            />

        <TextView
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:text="@string/descrpitiondietplan"
            android:textColor="#000000"
            android:gravity="center"
            android:layout_gravity="center_vertical"
            android:textSize="15sp"
            android:padding="10dp"
            android:id="@+id/textView6"
            android:layout_below="@+id/textView5"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Our Diet Plans"
            android:textColor="@android:color/black"
            android:padding="10dp"
            android:textSize="25sp"
            android:id="@+id/textView7"
            android:layout_below="@+id/textView6"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/textView7"
            android:layout_centerHorizontal="true"
            android:padding="10dp">

            <Button

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Basic Diet Plan"
                android:textColor="@android:color/white"
                android:id="@+id/normmal"
                android:background="@color/btn_login"
                android:layout_marginBottom="10dp"
                android:layout_gravity="center_horizontal" />

        </LinearLayout>


    </RelativeLayout>

</ScrollView>

It the toast insider the getBitmapScaling factor is called after the relative layout is viewed

Hope you guys can help me solve my issue

New
  • 3
  • 5
  • You should change from async task to modern image loader library like Fresco, Glide, etc. It is better for memory consumption, cache, transition, etc – HendraWD Feb 22 '16 at 08:46
  • please include your R.layout.first_frag.xml code, your current layout screenshot, and if can, the layout that you want to achieve too – HendraWD Feb 22 '16 at 08:49
  • the layout which i want to achieve is like i have a image view a text view a button so the image view should be scaled to the size of the screen lets say you have 1920 x1080 phone so the image is scaled to that size and the rest of the contents like the text and buttons are below is which can be viewed when you scroll up – New Feb 22 '16 at 09:15
  • The method you made will just resize the width of the image to the device's width, and the height will resize proportionally based on it's width. If you need to resize the height too based on device's height, you can set a RelativeLayout.LayoutParams with device's width and height to image view – HendraWD Feb 23 '16 at 07:38

1 Answers1

0

try add these

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(dm.widthPixels, dm.heightPixels);
im.setLayoutParams(params);

below im.setImageBitmap(newBitmap); onPostExecute

HendraWD
  • 2,984
  • 2
  • 31
  • 45
  • Thanks sir i tried to use one of your method you suggested in the comment section like using glide and fresco I went ahead with picasso and the .fit() did the trick for me thank you for making me aware of the image libraries – New Feb 23 '16 at 13:19
  • You're welcome, there are many useful android libraries out there, not just image loader only. You should check them out :) – HendraWD Feb 24 '16 at 03:57