我已經創建了自定義的線性佈局,放大了我在垂直方向上使用ImageView添加圖像。在我使用ScrollView和HorizontalScrollView的圖像之間滾動。我的問題是,當我縮放自定義線性佈局scrollview大小未調整和自定義linearlayout的頂部和底部被剪切。我不知道我的邏輯出錯了。我沒有找到使用谷歌解決方案。請在下面的鏈接中找到我的應用程序,並讓我知道你的建議來解決這個問題。 SampleApp如何在linearlayout中縮放子視圖並使其可滾動?
回答
如果你想與ScrollView
做到這一點,使圖像垂直放在你的佈局,您需要創建HorizontalScrollView
和ScrollView
內LinearLayout
每個ImageView
。
而且您的比例設置爲LinearLayout
,當縮放一個圖像時,所有其他圖像將在同一時間縮放。我認爲你需要做的是縮放每個圖像。然後您可以實現每個ImageView
,例如創建一個 ScaleImageView。
而且因爲你要顯示的ImageView
在LinearLayout
列表,你可以例如這樣的代碼:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true"
android:id="@+id/sv">
<HorizontalScrollView android:layout_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true"
android:id="@+id/hsv">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root" />
</HorizontalScrollView>
</ScrollView>
後面的代碼:
private List<int> imageList = new List<int> { Resource.Drawable.image1, Resource.Drawable.image2, Resource.Drawable.image3, Resource.Drawable.image4 };
private LinearLayout root;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
root = FindViewById<LinearLayout>(Resource.Id.root);
root.SetClipChildren(false);
for (int i = 0; i < imageList.Count; i++)
{
Bitmap bitmap = BitmapFactory.DecodeResource(this.Resources, imageList[i]);
var lp = new LinearLayout.LayoutParams(bitmap.Width, bitmap.Height);
lp.SetMargins(0, 2, 0, 2);
lp.Gravity = Android.Views.GravityFlags.Center;
ScaleImageView img = new ScaleImageView(this, null);
img.SetImageBitmap(bitmap);
img.SetScaleType(ScaleType.Matrix);
root.AddView(img, i);
}
}
爲了避免ScrollView
和Zoom
之間的矛盾,像這樣修改OnTouchEvent
的ScaleImageView
:
public override bool OnTouchEvent(MotionEvent e)
{
if (m_GestureDetector.OnTouchEvent(e))
{
m_PreviousMoveX = (int)e.GetX();
m_PreviousMoveY = (int)e.GetY();
return true;
}
var touchCount = e.PointerCount;
switch (e.Action)
{
case MotionEventActions.Down:
case MotionEventActions.Pointer1Down:
case MotionEventActions.Pointer2Down:
{
if (touchCount >= 2)
{
var distance = this.Distance(e.GetX(0), e.GetX(1), e.GetY(0), e.GetY(1));
m_PreviousDistance = distance;
m_IsScaling = true;
var parent = this.Parent;
var scrollview = parent.Parent;
scrollview.RequestDisallowInterceptTouchEvent(true);
scrollview.Parent.RequestDisallowInterceptTouchEvent(true);
}
}
break;
case MotionEventActions.Move:
{
if (touchCount >= 2 && m_IsScaling)
{
var distance = this.Distance(e.GetX(0), e.GetX(1), e.GetY(0), e.GetY(1));
var scale = (distance - m_PreviousDistance)/this.DispDistance();
m_PreviousDistance = distance;
scale += 1;
scale = scale * scale;
this.ZoomTo(scale, m_Width/2, m_Height/2);
this.Cutting();
}
else if (!m_IsScaling)
{
var distanceX = m_PreviousMoveX - (int)e.GetX();
var distanceY = m_PreviousMoveY - (int)e.GetY();
m_PreviousMoveX = (int)e.GetX();
m_PreviousMoveY = (int)e.GetY();
m_Matrix.PostTranslate(-distanceX, -distanceY);
this.Cutting();
}
}
break;
case MotionEventActions.Up:
case MotionEventActions.Pointer1Up:
case MotionEventActions.Pointer2Up:
{
if (touchCount <= 1)
{
m_IsScaling = false;
var parent = this.Parent;
var scrollview = parent.Parent;
scrollview.RequestDisallowInterceptTouchEvent(false);
scrollview.Parent.RequestDisallowInterceptTouchEvent(false);
}
}
break;
}
return true;
}
此解決方案不適合我的要求。我想縮放整個線性佈局,並在水平和垂直方向滾動而不會丟失頂部和底部的圖像。你能幫我解決這個問題嗎? 簡單的例子:我想顯示我的圖像像Adobe Reader中顯示的PDF頁面一樣,需要以線性佈局執行縮放整個兒童 – SATHISH
@SATHISH,哦......我誤解了。我測試了你的代碼,起初我認爲它是'ImageView'的imgView.SetAdjustViewBounds(true);問題,導致ImageView的大小在縮放時不會改變。但是在我將它設置爲true後,我仍然遇到同樣的問題。現在繼續工作...當我想出來的時候,我會更新我的答案。 –
感謝您的更新 – SATHISH
- 1. 如何旋轉並縮放滾動視圖中的圖像?
- 2. 在滾動視圖中縮放圖像
- 3. 如何使LinearLayout可滾動
- 4. 使Linearlayout可滾動而不使用滾動視圖
- 5. 如何在滾動視圖中縮放/平移圖像
- 6. 不可變視圖內可縮放滾動視圖[react-native]
- 7. Swift:如何在滾動視圖中向上滾動時縮放圖像
- 8. 如何使用滾動視圖縮放到中心?
- 9. 如何使的LinearLayout滾動沒有放在裏面滾動型
- 10. iPhone:如何僅縮放滾動視圖中的圖像
- 11. 如何在可滾動的LinearLayout中創建EditText可滾動?
- 12. 滾動視圖的子視圖如何知道滾動視圖正在滾動
- 13. 如何製作可縮放的LinearLayout?
- 14. 在Qt中的可滾動,可縮放圖像上繪圖
- 15. 如何在離子滾動內縮放圖像
- 16. 在LinearLayout中縮放背景圖像
- 17. 如何縮放滾動的圖像?
- 18. 使UIScrollView的子視圖固定,而其他子視圖可滾動
- 19. 使用setScaleY縮放視圖不會移動其他視圖
- 20. 在android中縮放和滾動圖像
- 21. 視差滾動背景圖像自動縮放和縮小
- 22. 如何對齊LinearLayout中的對象/滾動視圖
- 23. 如何使用子視圖對其滾動scrollView
- 24. LinearLayout不填充滾動視圖
- 25. Kivy - 創建一個可縮放的滾動視圖
- 26. 在UIScrollView中縮放多個子視圖
- 27. 如何使模態視圖可滾動?
- 28. 如何使Linearlayout可滾動兩個列表視圖可滾動包裝他們的內容
- 29. 中心圖像縮放滾動圖像
- 30. 如何使滾動視圖滾動即使按鈕子視圖被按下
您是否嘗試過我的解決方案?任何更新? –
Hi @ GraceFeng-MSFT, 今天我會檢查並更新你。 – SATHISH