Search code examples
androidandroid-bitmapbitmapfactory

How to re-scale Bitmap image size?


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


Solution

  • 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