缩放不在Imageview中工作

时间:2015-08-21 12:39:06

标签: java android android-layout

我正在尝试为图像添加缩放选项,我已经购买了图像视图。实际上,图像视图工作正常,代码正在运行,没有任何错误或问题。在缩放我的图像视图期间,它不会缩放。我的代码如下。

ZoomInZoomOut.java

import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class ZoomInZoomOut extends Activity implements OnTouchListener
{
    private static final String TAG = "Touch";
    @SuppressWarnings("unused")
    private static final float MIN_ZOOM = 1f,MAX_ZOOM = 1f;

    // These matrices will be used to scale points of the image
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();

    // The 3 states (events) which the user is trying to perform
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    // these PointF objects are used to record the point(s) the user is touching
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen_image);
        ImageView view = (ImageView) findViewById(R.id.imgFullscreen);
        view.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        ImageView view = (ImageView) v;
        view.setScaleType(ImageView.ScaleType.MATRIX);
        float scale;

        dumpEvent(event);
        // Handle touch events here...

        switch (event.getAction() & MotionEvent.ACTION_MASK)
        {
            case MotionEvent.ACTION_DOWN:   // first finger down only
                savedMatrix.set(matrix);
                start.set(event.getX(), event.getY());
                Log.d(TAG, "mode=DRAG"); // write to LogCat
                mode = DRAG;
                break;

            case MotionEvent.ACTION_UP: // first finger lifted

            case MotionEvent.ACTION_POINTER_UP: // second finger lifted

                mode = NONE;
                Log.d(TAG, "mode=NONE");
                break;

            case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down

                oldDist = spacing(event);
                Log.d(TAG, "oldDist=" + oldDist);
                if (oldDist > 5f) {
                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode = ZOOM;
                    Log.d(TAG, "mode=ZOOM");
                }
                break;

            case MotionEvent.ACTION_MOVE:

                if (mode == DRAG)
                {
                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); // create the transformation in the matrix  of points
                }
                else if (mode == ZOOM)
                {
                    // pinch zooming
                    float newDist = spacing(event);
                    Log.d(TAG, "newDist=" + newDist);
                    if (newDist > 5f)
                    {
                        matrix.set(savedMatrix);
                        scale = newDist / oldDist; // setting the scaling of the
                        // matrix...if scale > 1 means
                        // zoom in...if scale < 1 means
                        // zoom out
                        matrix.postScale(scale, scale, mid.x, mid.y);
                    }
                }
                break;
        }

        view.setImageMatrix(matrix); // display the transformation on screen

        return true; // indicate event was handled
    }

    /*
     * --------------------------------------------------------------------------
     * Method: spacing Parameters: MotionEvent Returns: float Description:
     * checks the spacing between the two fingers on touch
     * ----------------------------------------------------
     */

    private float spacing(MotionEvent event)
    {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }

    /*
     * --------------------------------------------------------------------------
     * Method: midPoint Parameters: PointF object, MotionEvent Returns: void
     * Description: calculates the midpoint between the two fingers
     * ------------------------------------------------------------
     */

    private void midPoint(PointF point, MotionEvent event)
    {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
    }

    /** Show an event in the LogCat view, for debugging */
    private void dumpEvent(MotionEvent event)
    {
        String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE","POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
        StringBuilder sb = new StringBuilder();
        int action = event.getAction();
        int actionCode = action & MotionEvent.ACTION_MASK;
        sb.append("event ACTION_").append(names[actionCode]);

        if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP)
        {
            sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
            sb.append(")");
        }

        sb.append("[");
        for (int i = 0; i < event.getPointerCount(); i++)
        {
            sb.append("#").append(i);
            sb.append("(pid ").append(event.getPointerId(i));
            sb.append(")=").append((int) event.getX(i));
            sb.append(",").append((int) event.getY(i));
            if (i + 1 < event.getPointerCount())
                sb.append(";");
        }

        sb.append("]");
        Log.d("Touch Events ---------", sb.toString());
    }

activity_fullscreen_image.xml也如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black" >

    <ProgressBar
        android:id="@+id/pbLoader"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >
    </ProgressBar>
    <com.google.android.gms.ads.AdView

        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="false"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"
    >
    </com.google.android.gms.ads.AdView>


    <!-- Scroll view for fullscreen preview -->

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="horizontal" >




        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1">




            <ImageView
                android:id="@+id/imgFullscreen"
                android:layout_width="384dp"
                android:layout_height="288dp"

                android:layout_gravity="center_vertical" />
    <!--  android:scaleType="fitXY" /> -->
</LinearLayout>
</HorizontalScrollView>

    <!-- Set as Set wallpaper button -->

    <!-- Set as Share button -->

    <!-- Download wallpaper button -->

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/frameLayout"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="28dp">

        <LinearLayout
            android:id="@+id/btShare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:background="@drawable/btn_rounded_corner"
            android:orientation="horizontal"
            android:layout_alignWithParentIfMissing="false"


            android:baselineAligned="false"
            android:layout_alignTop="@+id/llSetWallpaper"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left|bottom">

            <ImageView
                android:layout_width="22dp"
                android:layout_height="22dp"
                android:src="@drawable/common_signin_btn_icon_light" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:text="@string/Share"
                android:textColor="@color/white"
                android:textSize="18dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/llSetWallpaper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:background="@drawable/btn_rounded_corner"
            android:orientation="horizontal"
            android:layout_alignTop="@+id/llDownloadWallpaper"
            android:layout_alignRight="@+id/pbLoader"
            android:layout_alignParentBottom="false"
            android:layout_centerVertical="true"
            android:layout_gravity="center_horizontal|bottom">

            <ImageView
                android:layout_width="21dp"
                android:layout_height="21dp"
                android:src="@drawable/ico_apply" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:text="@string/set_wallpaper"
                android:textColor="@color/white"
                android:textSize="18dp" />
        </LinearLayout>

    </FrameLayout>

    <LinearLayout
        android:id="@+id/llDownloadWallpaper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_rounded_corner"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_gravity="right|bottom"
        android:layout_alignBottom="@+id/frameLayout"
        android:layout_alignParentRight="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:text="@string/download_wallpaper"
            android:textColor="@color/white"
            android:textSize="18dp" />
    </LinearLayout>

</RelativeLayout>

任何建议?

1 个答案:

答案 0 :(得分:0)

我已经工作了大约两天,最后得到了这个解决方案。在ImageView上设置Bitmap后调用此方法

public void usingSimpleImage(ImageView imageView) {
    ImageAttacher mAttacher = new ImageAttacher(imageView);
    ImageAttacher.MAX_ZOOM = 2.0f; // Double the current Size
    ImageAttacher.MIN_ZOOM = 0.5f; // Half the current Size
    MatrixChangeListener mMaListener = new MatrixChangeListener();
    mAttacher.setOnMatrixChangeListener(mMaListener);
    PhotoTapListener mPhotoTap = new PhotoTapListener();
    mAttacher.setOnPhotoTapListener(mPhotoTap);
}

private class PhotoTapListener implements OnPhotoTapListener {
    @Override
    public void onPhotoTap(View view, float x, float y) {
    }
}

private class MatrixChangeListener implements OnMatrixChangedListener {
    @Override
    public void onMatrixChanged(RectF rect) {
    }
}

愿这对你有帮助,这对我有用。