2016-12-17 31 views
0

我已經將一個ImageView添加到Horizo​​ntalScrollView內部的一個FrameLayout內部的一個相對佈局內部的一個ScrollView以編程方式。我添加了ScaleGestureDetector,以檢測放大和縮小。如何獲得大於屏幕尺寸的可縮放ImageView,並且可以垂直和水平滾動?

private void SetTouchListener(ImageView iv) 
{ 
    if (iv != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
    { 
     mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); 
     iv.setOnTouchListener(new OnTouchListener() 
     { 
      @TargetApi(Build.VERSION_CODES.FROYO) 
      @Override 
      public boolean onTouch(View v, MotionEvent event) 
      { 
       boolean res = mScaleDetector.onTouchEvent(event); 
       return res; 
      } 
     }); 
    } 
} 
@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener 
{ 
    @Override 
    public boolean onScale(ScaleGestureDetector detector) { 
     float fscaleX = frmLayoutImage.getScaleX() * detector.getScaleFactor(); 
     float fscaleY = frmLayoutImage.getScaleY()* detector.getScaleFactor(); 
     frmLayoutImage.setScaleX(fscaleX); 
     frmLayoutImage.setScaleY(fscaleY); 
     return true; 
    } 
} 

當在圖像縮放變得越來越小且居中,但縮小時圖像被裁剪爲ImageView的界限,不能被垂直或水平滾動。我怎樣才能讓ImageView調整它的大小?

下面是代碼

private void showBitmap (Bitmap b) 
{ 
    boolean isNew = false; 
    if (b!= null) 
    { 
     if (iv == null) 
     { 
      iv = new ImageView(context); 
      SetTouchListener(iv); 

     } 

     if (sv == null) 
     { 
      sv = new HorizontalScrollView(context); 

      isNew = true; 
     } 

     if (frmLayoutImage == null) 
     { 
      frmLayoutImage = new ScalingFrameLayout(context); 
      frmLayoutImage.setClipChildren(false); 

      isNew = true; 
     } 

     b = resizeBM(b); 
     iv.setImageBitmap(b); 
     if (iv.getParent() == null) 
     { 
      try 
      { 
       LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
       p.gravity = Gravity.CENTER_HORIZONTAL; 
       //p.weight = 1.0f; 
       iv.setLayoutParams(p); 
       iv.setScaleType(ImageView.ScaleType.FIT_CENTER); 
       iv.setClickable(true); 
       iv.setFocusable(true); 
       iv.setFocusableInTouchMode(true); 
       iv.setAdjustViewBounds(true); 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) 
       { 
        iv.setCropToPadding(false); 
       } 


       RelativeLayout.LayoutParams pold = (RelativeLayout.LayoutParams) _txtMeaning1.getLayoutParams(); 
       RelativeLayout.LayoutParams pnew = new RelativeLayout.LayoutParams (LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); 
       pnew.addRule(RelativeLayout.BELOW, R.id.txtMeaning1); 
       pnew.setMargins(pold.leftMargin,pold.topMargin,pold.rightMargin, pold.topMargin); 

       sv.setLayoutParams(pnew); 
       sv.setFillViewport(true); 
       sv.setHorizontalFadingEdgeEnabled(false); 
       sv.setVerticalFadingEdgeEnabled(false); 

       FrameLayout.LayoutParams pp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
       pp.gravity = Gravity.CENTER_HORIZONTAL; 
       frmLayoutImage.setLayoutParams(pp); 


       frmLayoutImage.addView(iv); 

       sv.addView(frmLayoutImage); 

       rellayoutMain.addView(sv); 



      } 
      catch (Exception ex) 
      { 
       Log.e("addImageView",ex.getMessage(),ex); 
      } 
     } 
     else 
     { 
      Log.d("ImageView","exists"); 
     } 
     _txtMeaning1.setVisibility(View.GONE); 
     iv.setVisibility(View.VISIBLE); 
     sv.setVisibility(View.VISIBLE); 
     frmLayoutImage.setVisibility(View.VISIBLE); 
     String status ="main "+ rellayoutMain.getWidth() + " sv " + sv.getWidth() + " llimg " + frmLayoutImage.getWidth() + " iv " + iv.getWidth(); 
     lib.setgstatus(status); 
    } 
} 

和佈局:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/layoutMainParent" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="#FFFFFF" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:orientation="vertical" 
tools:context="org.de.jmg.learn.MainActivity" 
> 
<ScrollView 
    android:id="@+id/layoutMain" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/layoutButtons" 
    android:background="#FFFFFF" 
    android:fillViewport="true"> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/rellayoutMain" 
     > 
     <LinearLayout 
      android:id="@+id/LayWord" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" > 

      <org.de.jmg.lib.BorderedTextView 
       android:id="@+id/word" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:maxLines="3" 
       android:scrollHorizontally="false" 
       android:text="@string/hello_world" 
       android:textColor="#000000" 
       android:textSize="60px" 
       android:textStyle="bold" 
       android:typeface="normal" 
       /> 

      <org.de.jmg.lib.BorderedEditText 
       android:id="@+id/edword" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:maxLines="3" 
       android:scrollHorizontally="false" 
       android:inputType="text" 
       android:text="@string/hello_world" 
       android:textColor="#000000" 
       android:textSize="60px" 
       android:textStyle="bold" 
       android:typeface="normal" 
       android:visibility="gone" /> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/LayCom" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/LayWord" 
       android:layout_centerHorizontal="true" > 

      <org.de.jmg.lib.BorderedTextView 
       android:id="@+id/Comment" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:maxLines="3" 
       android:scrollHorizontally="false" 
       android:text="@string/hello_world" 
       android:textColor="#000000" 
       android:textSize="35px" 
       /> 

      <org.de.jmg.lib.BorderedEditText 
       android:id="@+id/edComment" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:maxLines="3" 
       android:inputType="text" 
       android:scrollHorizontally="false" 
       android:text="@string/hello_world" 
       android:textColor="#000000" 
       android:textSize="35px" 
       android:visibility="gone" /> 

     </LinearLayout> 

     <org.de.jmg.lib.BorderedEditText 
      android:id="@+id/txtMeaning1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/LayCom" 
      android:layout_marginTop="120px" 
      android:inputType="textMultiLine|textNoSuggestions" 
      android:lines="2" 
      android:maxLines="20" 
      android:minLines="2" 
      android:text="@string/hello_world" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="#000000" 
      android:textSize="40px" 
      > 

      <requestFocus android:layout_width="wrap_content" /> 

     </org.de.jmg.lib.BorderedEditText> 

     <org.de.jmg.lib.BorderedEditText 
      android:id="@+id/txtMeaning2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/txtMeaning3" 
      android:layout_below="@+id/txtMeaning1" 
      android:layout_marginTop="56px" 
      android:inputType="textMultiLine|textNoSuggestions" 
      android:lines="2" 
      android:maxLines="20" 
      android:minLines="2" 
      android:text="@string/hello_world" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="#000000" 
      android:textSize="40px" 
      /> 

     <org.de.jmg.lib.BorderedEditText 
      android:id="@+id/txtMeaning3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/txtMeaning2" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="56px" 
      android:inputType="textMultiLine|textNoSuggestions" 
      android:lines="2" 
      android:maxLines="20" 
      android:minLines="2" 
      android:text="@string/hello_world" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="#000000" 
      android:textSize="40px" 
      /> 
     </RelativeLayout> 
    </ScrollView> 
    <RelativeLayout 
     android:id="@+id/layoutButtons" 
     android:layout_alignParentBottom="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
    > 
     <RelativeLayout 
      android:id="@+id/layoutButtonsInner" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" > 

      <Button 
       android:id="@+id/btnSkip" 
       android:layout_width="100px" 
       android:layout_height="60px" 
       android:layout_alignParentBottom="false" 
       android:layout_marginBottom="0px" 
       android:text="@string/btnSkip" 
       android:textSize="20px" 
       android:padding="0dp" /> 

      <Button 
       android:id="@+id/btnRight" 
       android:layout_width="110px" 
       android:layout_height="60px" 
       android:layout_toRightOf="@+id/btnSkip" 
       android:layout_marginBottom="0px" 
       android:text="@string/btnRight" 
       android:textSize="20px" 
       android:padding="0dp" /> 

      <Button 
       android:id="@+id/btnView" 
       android:layout_width="100px" 
       android:layout_height="60px" 
       android:layout_toRightOf="@+id/btnRight" 
       android:layout_marginBottom="0px" 
       android:text="@string/btnView" 
       android:textSize="20px" 
       android:padding="0dp" /> 

      <Button 
       android:id="@+id/btnWrong" 
       android:layout_width="120px" 
       android:layout_height="60px" 
       android:layout_toRightOf="@+id/btnView" 
       android:layout_marginBottom="0px" 
       android:text="@string/btnWrong" 
       android:textSize="20px" 
       android:padding="0dp" /> 

      <Button 
       android:id="@+id/btnEdit" 
       android:layout_width="100px" 
       android:layout_height="60px" 
       android:layout_toRightOf="@+id/btnWrong" 
       android:layout_marginBottom="0px" 
       android:text="@string/btnEdit" 
       android:textSize="20px" 
       android:padding="0dp" /> 
     </RelativeLayout>  

     <org.de.jmg.lib.BorderedTextView 
      android:id="@+id/txtStatus" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello_world" 
      android:textSize="20px" 
      android:layout_below="@+id/layoutButtonsInner" 
       android:layout_centerHorizontal="true" 
     /> 
    </RelativeLayout>   
</RelativeLayout> 

回答

相關問題