如何重新缩放位图图像大小?

时间:2016-02-22 08:33:03

标签: android android-bitmap bitmapfactory

大家好我正在尝试从网址加载图片并在加载后我正在尝试重新缩放它以使其适合整个屏幕,之后文本和按钮存在在图像下方可用使用滚动视图

这是我的片段

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() );

    }
}

我的Util类

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文件

<?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>

在查看相对布局后调用getBitmapScaling因子的吐司内幕

希望你们能帮助我解决我的问题

1 个答案:

答案 0 :(得分:0)

尝试添加这些

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);

在im.setImageBitmap(newBitmap)下面; onPostExecute