我知道這個主題有很多問題。但我仍然需要幫助。我想在我的TableLayout
中顯示我的ImageView
等於width
和height
。但在不同的設備上,我無法做到這一點。Android - 來自網絡服務的圖片在不同設備上具有不同的尺寸
這裏是我在AsyncTask
中使用URL
的圖像,並在onPostExecute
上設置了ImageView
方法。
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
// set the imageview
bmImage.setImageBitmap(result);
}
這裏是一個TableRow
像,一邊裏面在我的ImageView的位於
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:paddingBottom="10dp"
android:id="@+id/mylayout"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:clickable="true"
android:background="@drawable/border"/>
<TextView
android:layout_width="match_parent"
android:layout_height="75dp"
android:maxLines="4"
android:id="@+id/text"
android:layout_below="@+id/image"
android:layout_alignEnd="@+id/image"
android:layout_alignRight="@+id/image"
android:clickable="true"
android:textSize="18sp"
android:background="#e3e3e3"
android:paddingTop="10dp"
android:gravity="center_vertical"
/>
</RelativeLayout>
我以爲我可以創建一個TableLayout
,並找到View
(上)我的XML文件(news.xml) (view is above)
要做到這一點,我得到屏幕寬度,並減去paddings
併除以2,並將該寬度設置爲ImageView
,以便能夠爲所有圖像獲得相同的大小e觀點。
這裏是我的代碼,
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
tableLayout = (TableLayout) view.findViewById(R.id.tableLayout);
Log.e("screenwidth",":"+screenWidth);
// here is my paddings converted to pixel and will be subtracted from screen width
int pixValue = (int) (20 * Resources.getSystem().getDisplayMetrics().density);
int imageWidthPix = (screenWidth - pixValue)/2;
Log.e("imageWidthPix ",":"+imageWidthPix);
for (int i = 0; i < categoryNews.get(myString).size(); i++) {
if (i % 2 == 0) {
tr = new TableRow(getActivity());
tableLayout.addView(tr);
}
//here is where my ImageView layout (news.xml)
View oneNews = inflater.inflate(R.layout.news, null);
ImageView imageView = (ImageView) oneNews.findViewById(R.id.image);
TextView textView = (TextView) oneNews.findViewById(R.id.text);
//here I set the width as I want
imageView.setLayoutParams(new RelativeLayout.LayoutParams(imageWidthPix, RelativeLayout.LayoutParams.WRAP_CONTENT));
String imagePath = "http://my.url.com/" + categoryNews.get(myString).get(i).getImagePath();
new DownloadImageTask(imageView,getActivity().getApplicationContext())
.execute(imagePath);
我得到了我的手機上了完美的結果。但是在具有不同屏幕尺寸的不同設備上,ImageView
頂部和底部都有空格。我試圖轉換九塊補丁塊並將其轉換爲位圖,但沒有區別。如果我無法從網絡服務獲取圖片,我會將其他分辨率圖片放入mipmap-hpdi
,mipmap-mdpi
等文件夾中,這樣就不會出現錯誤。但是我正在動態獲取圖像,因此如何才能實現ImageView
屏幕寬度的一半? 在此先感謝。
你的佈局中只有兩件事情。 –
是在我的news.xml中有一個圖像視圖和一個相對佈局內的文本視圖,你可以在問題 – Hilal