【发布时间】:2016-02-22 08:33:03
【问题描述】:
大家好,我正在尝试从 url 加载图像,加载后我试图重新缩放它,使其适合整个屏幕,之后出现的文本和按钮在环绕的图像下方可用使用滚动视图
这是我的片段
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因子的toast内部
希望你们能帮我解决我的问题
【问题讨论】:
-
你应该从异步任务更改为现代图像加载器库,如 Fresco、Glide 等。它更适合内存消耗、缓存、过渡等
-
请包含您的 R.layout.first_frag.xml 代码,您当前的布局截图,如果可以,您也想实现的布局
-
我想要实现的布局就像我有一个图像视图一个文本视图一个按钮所以图像视图应该缩放到屏幕的大小假设你有 1920 x1080 手机所以图像缩放到该大小,其余内容(例如下面的文本和按钮)可以在向上滚动时查看
-
您所做的方法只是将图像的宽度调整为设备的宽度,高度将根据其宽度按比例调整大小。如果您也需要根据设备的高度调整高度,您可以将具有设备宽度和高度的 RelativeLayout.LayoutParams 设置为图像视图
标签: android android-bitmap bitmapfactory