2013-07-04 3 views
-1

이미지 사이를 스 와이프하기 위해 내 파편에 ImageView를 표시합니다. this 포스트 덕분에 이미지와 이미지 뷰를 원본 이미지의 폭과 높이에 맞출 수있었습니다. 그러나 이미지가 항상 왼쪽 상단 구석에있을 경우. 그런 다음 조각의 레이아웃을 변경하고 imageView를 가로 및 세로 가운데에 맞 춥니 다.레이아웃 변경 원인 illegalStateException

조각 :

ImageView image; 
Bitmap bitmap = null; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) 
{ 
    image = (ImageView) (ImageView) inflater.inflate(R.layout.fragment_image_page, container, false); 
    // ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_image_page, container, false); 
    // image = (ImageView) rootView.findViewById(R.id.imageView); 
    new ImageLoad().execute(""); 
    return image; 
} 


private class ImageLoad extends AsyncTask<String, Void, Boolean> 
{ 
    int width; 
    int height; 
    @Override 
    protected Boolean doInBackground(String... params) 
    { 
     URL url; 
     try 
     { 
      url = new URL(
        "http://www.....jpg"); 
      // !!! ERROR IN FOLLOWING LINE !!! 
      bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
      width = bitmap.getWidth(); 
      height = bitmap.getHeight(); 
      int bounding = getResources().getDisplayMetrics().widthPixels; 

      if (bounding < width) 
      { 
       float ratio = width/height; 
       width = bounding; 
       height = (int) (width/ratio); 
       bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height); 
      } 
     } catch (MalformedURLException e) 
     { 
      e.printStackTrace(); 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected void onPostExecute(Boolean doInBackground) 
    { 
     FrameLayout.LayoutParams param = (FrameLayout.LayoutParams) image.getLayoutParams(); 
     param.width = width; 
     param.height = height; 
     image.setLayoutParams(param); 
     image.setImageBitmap(bitmap); 
    } 
} 

XML의 작업 :

<?xml version="1.0" encoding="utf-8"?> 
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/imageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:contentDescription="BILD" /> 

XML하지하지만 RelativeLayout의 나있는 LinearLayout과 주변 후에, 나는 의견의 작업 IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

하지 코드를 가지고 취업 :

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerInParent="true" > 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="BILD" /> 

</RelativeLayout> 

답변

0

XML 파일을 변경 한 후, 루트를 반환 할 필요는 view.Which이 뷰 그룹입니다 팽창.

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) 
    { 

     ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_image_page, container, false); 
     image = (ImageView) rootView.findViewById(R.id.imageView); 
     new ImageLoad().execute(""); 
     return rootView; 
    } 
+0

이 바보 같은, 어리석은 실수 * 부끄러운 * – lis

0

onCreateView()에는 container 대신 null을 입력하십시오. ImageView 번으로 캐스팅하지 않아도됩니다. 이처럼

:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    image = (ImageView) inflater.inflate(R.layout.fragment_image_page, null, false); 
    ... 
    return image; 
}