ScaleGestureDetector Not Working Not showing Anything

Viewed 2017

FullOffer.java

package com.synergywebdesigners.veebee;

import android.content.Intent;
import android.graphics.Matrix;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

public class FullOffer extends AppCompatActivity {
    ImageView image;
    TextView start,end,title,messages;
    Float scale = 1f;
    ScaleGestureDetector SDG;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_offer);
        start = (TextView) findViewById(R.id.start);
        end = (TextView) findViewById(R.id.end);
        title = (TextView) findViewById(R.id.title);
        messages = (TextView) findViewById(R.id.message);
        image = (ImageView) findViewById(R.id.imageDownloaded);

        Intent i = getIntent();
        setTitle(i.getStringExtra("title"));//title of Application
        start.setText(i.getStringExtra("start"));
        end.setText(i.getStringExtra("end"));
        title.setText(i.getStringExtra("title"));
        messages.setText(Html.fromHtml(i.getStringExtra("message")),TextView.BufferType.SPANNABLE);
        Picasso.with(FullOffer.this).load(Config.UPLOAD_LOCATION+i.getStringExtra("image")).fit().into(image);
        SDG = new ScaleGestureDetector(this,new ScaleListner());
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        SDG.onTouchEvent(event);
       /* return true;*/
        return super.onTouchEvent(event);
    }
    private class ScaleListner extends ScaleGestureDetector.SimpleOnScaleGestureListener{
       float onScaleBegin = 0;
        float onScaleEnd = 0;
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
           /* scale = scale * detector.getScaleFactor();
            scale = Math.max(0.1f,Math.min(scale,5f));
            matrix.setScale(scale,scale);
            image.setImageMatrix(matrix);*/
           scale *= detector.getScaleFactor();
            image.setScaleX(scale);
            image.setScaleY(scale);
            return true;
        }
        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            Toast.makeText(getApplicationContext(), "Your Application Pinch" ,Toast.LENGTH_SHORT).show();
            onScaleBegin = scale;
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
           onScaleEnd = scale;
            if(onScaleEnd > onScaleBegin){
                Toast.makeText(getApplicationContext(),"Scaled Up By"+String.valueOf(onScaleEnd/onScaleBegin),Toast.LENGTH_SHORT).show();
            }else if(onScaleEnd<onScaleBegin){
                Toast.makeText(getApplicationContext(),"Scaled Down By"+String.valueOf(onScaleEnd/onScaleBegin),Toast.LENGTH_SHORT).show();
            }
        }
    }


}

activity_full_offer.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:scrollbars="none"
    android:background="#ffffff"
    tools:context="com.synergywebdesigners.veebee.FullOffer">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView android:id="@+id/imageDownloaded"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:src="@drawable/abc_ic_menu_copy_mtrl_am_alpha"/>
        <!-- Title Label -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:textAllCaps="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000000"
            android:textStyle="bold"
            android:id="@+id/title"
            android:gravity="center"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/message"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#eff0f1"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:text="Start Date :"/>
                <TextView
                    android:id="@+id/start"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:text="20-07-2017"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:text="Valid Till :"/>
                <TextView
                    android:id="@+id/end"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:text="22-07-2017"/>
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>

I have a Image View That i have Image Picasso Library to Showing Image Dynamically . I am Use ScaleGestureDetector But Not Working no any error Message Come Please Help Me . No Any Error Come But Nothing Happen This Code Please Help Me How can I Fix This Issue. Please Help Me

2 Answers

It looks like I may have had the same issue.

The ScaleGestureDetector only works if it gets more events than just ACTION_DOWN passed into it.

If you have return super.onTouchEvent(event) in your onTouchEvent

The super maybe returning false, which means you'll not receive any more Touch events for a given gesture (such as ACTION_MOVE or ACTION_UP).

If your onTouchEvent returns true you'll receive all the events required for the ScaleGestureDetector to work

Example:

override fun onTouchEvent(event: MotionEvent?): Boolean {
    mScaleDetector.onTouchEvent(event)
    return true
}

private val mScaleDetector = ScaleGestureDetector(context, object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
    override fun onScale(detector: ScaleGestureDetector): Boolean {
        Log.d("TAG", "Pinch detected")
        return true
    }
})

Word of warning - just be cautious that your not breaking anything by setting this to true, if your not calling super the parent will no longer be called.

Related