2017-01-16 4 views
1

このトピックについて多くの質問があります。しかし、私はまだ助けが必要です。 TableLayoutに私のImageViewが等しいと表示されたいのですが、widthheightです。しかし、さまざまなデバイスで私はこれを行うことができませんでした。Android - ウェブサービスからの画像が異なるデバイスで異なるサイズを持っています

ここではURLAsyncTaskに、そしてImageViewonPostExecuteに設定して画像を取得します。ここで

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

は私の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> 

に位置し、私のXMLファイル(news.xml)である私が側、のようなTableRowの内側に、私はTableLayoutを作成し、(上記の)Viewを見つけることができると考えました(ビューは上にあります)

これを行うには、画面の幅を取得し、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に上下にスペースがあります。私は9つのパッチチャンクを変換してビットマップに変換しようとしましたが、違いはありませんでした。 Webサービスから画像を取得できない場合は、他の解像度画像をmipmap-hpdimipmap-mdpiなどのフォルダに入れてもエラーは発生しません。しかし、画像を動的に取得しているので、どのようにしてImageViewの画面幅の半分が得られるのですか? ありがとうございます。ここで

+0

レイアウトには2つのものしかありません。 –

+0

はい、私のnews.xmlには、画像ビューとテキストビューが1つあり、質問の中で見ることができます。 – Hilal

答えて

0

私はあなたのxml

自分のイメージ図とTextViewには、小さくても大きく作ることlayout_weightを変更、あなたの必要性に応じて重量を変更、重みでリニアレイアウトを試してみてくださいを編集しました。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingBottom="10dp" 
    android:orientation="vertical" 
    android:id="@+id/mylayout" 
    android:weightSum="10"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="3" 
     android:id="@+id/image" 
     android:src="@drawable/sd" 
     android:clickable="true"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:maxLines="4" 
     android:id="@+id/text" 
     android:clickable="true" 
     android:textSize="18sp" 
     android:background="#e3e3e3" 
     android:paddingTop="10dp" 
     android:gravity="center_vertical" 
     /> 


</LinearLayout> 
+0

静止画像ビューには、上下に空白がたくさんあります。 – Hilal

+1

bitmap.createScaledBitmap(); android:fitsSystemWindows = "true"あなたの画像ビューで –

+0

大丈夫、 'bitmap.createScaledBitmap()'がうまくいくようです – Hilal

関連する問題