2014-06-10 63 views
0

我目前使用的庫TouchImageView這裏:Android:Square TouchImageView?

https://github.com/MikeOrtiz/TouchImageView

時,我充滿了整個手機與TouchImageView屏幕這工作完全正常,但我會如何限制可視面積爲正方形?

我已經試過:

public class SquareTouchImageView extends TouchImageView { 
    public SquareTouchImageView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    public SquareTouchImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SquareTouchImageView(Context context, AttributeSet attrs, 
      int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int width = getMeasuredWidth(); 
     setMeasuredDimension(width, width); 
    } 
} 

但是,這並不讓我向下滾動,顯示圖像的其餘部分(如果它長得比它寬)

有沒有一種方法,我可以啓用方形TouchImageView?

如果是這樣,我怎麼能做到這一點?

+0

您是否嘗試過簡單地把一個 「機器人:的onclick = [()函數]」 直接對XML的ImageView的? – durbnpoisn

+0

是的,這並沒有幫助,基本上,由於某種原因,TouchImageView中的圖像被錨定在錯誤的位置。 –

+0

作爲另一種想法,您可以製作完全透明的PNG,並將其固定在任何位置 - 有效製作傳感器。 – durbnpoisn

回答

0

我已經通過下面的方法解決了這個問題,我希望這可以幫助任何人在將來可能會遇到這個問題。

public class SquareTouchImageView extends TouchImageView { 
    public SquareTouchImageView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    public SquareTouchImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SquareTouchImageView(Context context, AttributeSet attrs, 
      int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     Drawable drawable = getDrawable(); 
     if (drawable == null || drawable.getIntrinsicWidth() == 0 
       || drawable.getIntrinsicHeight() == 0) { 
      setMeasuredDimension(0, 0); 
      return; 
     } 

     int drawableWidth = drawable.getIntrinsicWidth(); 
     //int drawableHeight = drawable.getIntrinsicHeight(); 
     int widthSize = MeasureSpec.getSize(widthMeasureSpec); 
     int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
     //int heightSize = widthSize;// MeasureSpec.getSize(heightMeasureSpec); 
     //int heightMode = widthMode;// MeasureSpec.getMode(heightMeasureSpec); 
     viewWidth = setViewSize(widthMode, widthSize, drawableWidth); 
     viewHeight = viewWidth;// setViewSize(heightMode, heightSize, 
           // drawableHeight); 

     // 
     // Set view dimensions 
     // 
     setMeasuredDimension(viewWidth, viewHeight); 

     // 
     // Fit content within view 
     // 
     fitImageToView(); 
    } 
} 

您可能必須從private在TouchImageView改變一些變量protected

和xml:

<com.aura.app.widget.SquareTouchImageView 
     android:id="@+id/scrollingImage" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" /> 
相關問題