如何在自定义视图中实现ScaleGestureDetector

时间:2015-07-26 11:37:04

标签: android zoom android-custom-view ontouchevent gesturedetector

我是一个完全的初学者,无法适应一些教程代码。此活动(gameScreen)应该包含一个名为MapView的自定义视图,该视图扩展了ImageView并应允许缩放。就目前而言,图像不会按预期填满屏幕(屏幕设置为横向,并在图像的左侧和右侧留下空白区域)。视图/活动似乎也不允许缩放功能工作。任何帮助赞赏!谢谢!

这是gameScreen.java活动:

package com.games.michael.treasureseeker;

import android.app.Activity;
import android.os.Bundle;

import java.util.Random;

public class GameScreen extends Activity {

    private MapView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState)    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_screen);
    }
}

这是game_screen.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"

    android:id="@+id/gameBackground">

    <TextView
        android:id="@+id/directions"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@color/white"
        android:background="@color/black"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        />

    <com.games.michael.treasureseeker.MapView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:src="@drawable/pirate_map_2"
        android:layout_below="@+id/directions"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

这是自定义视图类MapView.java:

package com.games.michael.treasureseeker;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;

public class MapView extends ImageView {
    private ScaleGestureDetector SGD;
    private float scale = 1.f;
    private Matrix matrix = new Matrix();

    public MapView(Context context) {
        super(context);
        SGD = new ScaleGestureDetector(getContext(), new ScaleListener());
    }

    public MapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        SGD = new ScaleGestureDetector(getContext(), new ScaleListener());
    }

    public MapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        SGD = new ScaleGestureDetector(getContext(), new ScaleListener());
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        SGD.onTouchEvent(ev);
        return super.onTouchEvent(ev);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        canvas.scale(scale, scale);

        MapView.this.setImageResource(R.drawable.pirate_map_2);
        canvas.restore();
    }

    private class ScaleListener extends
               ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            scale *= detector.getScaleFactor();
            scale = Math.max(0.1f, Math.min(scale, 5.0f));
            //matrix.setScale(scale, scale);
            //this.setImageMatrix(matrix);
            invalidate();
            return true;
        }
    }
}

这是attrs_map_view.xml:

<resources>
    <declare-styleable name="MapView">
        <attr name="exampleString" format="string" />
        <attr name="exampleDimension" format="dimension" />
        <attr name="exampleColor" format="color" />
        <attr name="exampleDrawable" format="color|reference" />
    </declare-styleable>
</resources>

编辑:

我在onScale方法中放了一个toast,当我运行代码并使用onScale手势时,代码被激活并显示了toast。出于某种原因,ImageView只是不会缩放:...

似乎问题在于该项目与在线许多教程之间的区别。不同之处在于我想缩放文件图像。我无法在网上找到这样的例子,或者理解为什么即使经过几个小时的尝试,这也无法解决。任何进一步的信息或建议都是最受欢迎的!

编辑:凹凸!我很绝望,无法解决这个问题!如果有人能提供帮助那就太棒了!

1 个答案:

答案 0 :(得分:1)

您必须在 onTouchEvent()中指定特定操作。为MotionEvents ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_POINTER_DOWN,ACTION_POINTER_UP 编写一个开关案例。这将允许缩放或拖动。请从google Responding to Touch Events查看此文档。

希望这会有所帮助:)