【问题标题】:How to get an scalable ImageView that is larger than the screen size and can be scrolled vertically and horizontically?如何获得一个大于屏幕尺寸且可以垂直和水平滚动的可伸缩ImageView?
【发布时间】:2016-12-18 01:24:16
【问题描述】:

我已将 ImageView 添加到 FrameLayout 内的 Horizo​​ntalScrollView 内的相对布局内,该布局位于 ScrollView 内以编程方式。我添加了一个 ScaleGestureDetector,用于检测放大和缩小。

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>

【问题讨论】:

    标签: android imageview


    【解决方案1】:

    参考这个链接,它可能会有所帮助。 https://github.com/chrisbanes/PhotoView

    您可以定义默认比例大小并水平和垂直滚动图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2014-01-11
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      相关资源
      最近更新 更多