ScaleGestureDetector不工作没有显示任何内容

时间:2017-07-26 08:42:41

标签: java android xml

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>

我有一个图像视图,我有Image Picasso库动态显示图像。我使用ScaleGestureDetector但不工作没有 任何错误消息来请帮助我。 没有任何错误,但没有发生此代码请帮助我如何解决此问题。请帮助我

2 个答案:

答案 0 :(得分:0)

  

的onTouchEvent(..)

不是每次调用,只有在视图不处理onTouch事件时才会调用它: 您应该使用dispatchTouchEvent(MotionEvent event)

 @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        return SDG.onTouchEvent(event);
    }

否则您可以实施View.OnTouchListener

注册将触摸事件发送到此视图时要调用的回调。

image.setOnTouchListener(this);

和覆盖

   @Override onTouch(View v, MotionEvent event)
    public boolean onTouch(View v, MotionEvent event) {
        return SDG.onTouchEvent(event);
    }

参考以下链接以获得更清晰的触摸事件图片

OnTouchEvent not working on child views

答案 1 :(得分:0)

看来我可能遇到过同样的问题。

ScaleGestureDetector仅在它获得的事件多于传递到其中的ACTION_DOWN时才起作用。

如果您的return super.onTouchEvent(event)中有onTouchEvent

超级可能会返回false,这意味着您将不再收到给定手势(例如ACTION_MOVEACTION_UP)的Touch事件。

如果您的onTouchEvent返回true,您将收到ScaleGestureDetector工作所需的所有事件

示例:

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
    }
})

警告语-请小心,不要通过将其设置为true来破坏任何内容,如果您不调用super,则不会再调用父级。

相关问题