Android:水平线性布局项之间的细线

时间:2014-11-15 13:41:20

标签: android imageview line android-linearlayout

我在自己制作的zoomview中得到了一个tablelayout。 当我平移/拖动tablelayout时,我有时会看到一条很细的线 我可以看到背景颜色的图像视图(水平和垂直),如何删除此行?我也尝试使用列而不是水平线性布局,同样的问题

<ZoomView 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:id="@+id/zoomView">

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:weightSum="2"
        android:shrinkColumns="0,1">
        <TableRow
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1">
            <LinearLayout
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:weightSum="2">
            <ImageView
                android:src="@drawable/parkZoomOut"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:scaleType="fitEnd"
                android:layout_weight="1"/>
            <ImageView
                android:src="@drawable/parkZoomOut2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:scaleType="fitEnd"
                android:layout_weight="1" />
                </LinearLayout>

        </TableRow>
        <TableRow
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1">
             <LinearLayout
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:weightSum="2">
            <ImageView
                android:src="@drawable/parkZoomOut3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitStart"
                 android:layout_weight="1" />
            <ImageView
                android:src="@drawable/parkZoomOut4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitStart" 
                 android:layout_weight="1"/>
                </LinearLayout>
        </TableRow>
    </TableLayout>
</ZoomView>

zoomview类:

public class ZoomView : LinearLayout
{

    private static  String TAG = "ZoomLayout";
    private static  float MIN_ZOOM = 1.0f;
    private static  float MAX_ZOOM = 7.0f;

    private enum Mode 
    {
        NONE,
        DRAG,
        ZOOM
    }
    private Mode mode = Mode.NONE;

    private static float scale = 1.0f;
    private static float lastScaleFactor = 0f;
    private float initialScale=0f;
    // Where the finger first  touches the screen
    private float startX = 0f;
    private float startY = 0f;

    // How much to translate the canvas
    private float dx = 0f;
    private float dy = 0f;
    private float prevDx = 0f;
    private float prevDy = 0f;

    private int screenHeight=0;
    private int screenWidth=0;

    ScaleGestureDetector scaleDetector;


    public ZoomView (Context context) :    
    base (context)
    {
        Initialize (context);    
    }

    public ZoomView (Context context, IAttributeSet attrs) :base (context,attrs)    
    {
        Initialize (context);    
    }

    public ZoomView (Context context, IAttributeSet attrs, int defStyle) :base (context, attrs, defStyle)
    {
        Initialize (context);
    }
    void Initialize (Context context)
    {
        scaleDetector = new ScaleGestureDetector(context, new MyScaleListener());
    }

    public void SetInitialScale()
    {
        var metrics = Resources.DisplayMetrics;

        double originalPictureWidth = 2498;
        double originalPictureHeight = 2048;

        screenWidth = metrics.WidthPixels;
        screenHeight = metrics.HeightPixels-105;//dit nog omzetten van dp

        double sc = screenWidth / originalPictureWidth;
        var height = sc * originalPictureHeight;
        var rsc = screenHeight/height; 
        //Console.Write ("scale");
        //Console.WriteLine (rsc.ToString ());
        scale = (float)rsc;
        initialScale = scale;
        ApplyScaleAndTranslation();
        MIN_ZOOM = scale;

    }

    public bool OnTouch(View v, MotionEvent e)
    {
        return OnTouchEvent(e);
    }

    public override bool OnTouchEvent(MotionEvent e)
    {
        switch (e.Action)
        {
        case MotionEventActions.Down:
            Console.WriteLine ("down");
                    if (scale >= MIN_ZOOM) 
                    {
                        mode = Mode.DRAG;
                        startX = e.GetX() - prevDx;
                        startY = e.GetY() - prevDy;
                    }
                    break;
                case MotionEventActions.Move:
                Console.WriteLine("move");
                    if (mode == Mode.DRAG) 
                    {
                        dx = e.GetX() - startX;
                        dy = e.GetY() - startY;
                    }
                    break;
                case MotionEventActions.Pointer2Down:
                Console.WriteLine("pointerdown");
                    mode = Mode.ZOOM;
                    break;
                case MotionEventActions.Pointer2Up:
                Console.WriteLine("pointerup");
                    mode = Mode.DRAG;
                    break;
                case MotionEventActions.Up:
                Console.WriteLine("up");
                    mode = Mode.NONE;
                    prevDx = dx;
                    prevDy = dy;
                    break;
                   }

        scaleDetector.OnTouchEvent(e);

        if ((mode == Mode.DRAG && scale >= MIN_ZOOM) || mode == Mode.ZOOM) 
        {
            Parent.RequestDisallowInterceptTouchEvent(true);
            float maxDx = (Child().Width  - (Child().Width / scale)) / 2 * scale;

            float maxDy = 0;
            if (scale != initialScale)
            {
                var sc = scale/initialScale;
                maxDy = (Child().Height  - (Child().Height / sc)) / 2 * sc;
            }


            //Console.WriteLine("uo");
            //Console.WriteLine (scale.ToString ());
            //Console.WriteLine (maxDy);
            //Console.WriteLine (dy);
            dx = Math.Min(Math.Max(dx, -maxDx), maxDx);
            dy = Math.Min(Math.Max(dy, -maxDy), maxDy);

            ApplyScaleAndTranslation();
        }
        return true;
    }


    private void ApplyScaleAndTranslation() 
    {
        Child().ScaleX=scale;
        Child().ScaleY=scale;
        Child().TranslationX=dx;
        Child().TranslationY=dy;
    }

    private View Child() 
    {
        return GetChildAt(0);
    }

    private class MyScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener
    {

    public override bool OnScale(ScaleGestureDetector scaleDetector)
    {
        float scaleFactor = scaleDetector.ScaleFactor;

        if (lastScaleFactor == 0 || (Math.Sign(scaleFactor) == Math.Sign(lastScaleFactor)))
            {
            scale *= scaleFactor;
                Console.Write ("scalefactor");
                Console.WriteLine(scale.ToString());
            scale = Math.Max(MIN_ZOOM, Math.Min(scale, MAX_ZOOM));
            lastScaleFactor = scaleFactor;
        } 
        else
        {
            lastScaleFactor = 0;
        }
        return true;
    }

    public override bool OnScaleBegin(ScaleGestureDetector s)
    {
        return true;
    }

    public override void OnScaleEnd(ScaleGestureDetector s)
    {

    }

}

}}

0 个答案:

没有答案