onTouchListener不起作用

时间:2016-11-02 11:33:03

标签: android ontouchlistener

代码:

public class ImageActivity extends FragmentActivity {

    ImageView mClickedImage;
    PhotoViewAttacher mPhotoViewAttacher;
    TextView mRotate_imageActivity;
    private int BASEINT_ImageActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        Intent intent = getIntent();
        final String string = (String) intent.getSerializableExtra("Clicked Image");
        mClickedImage = (ImageView) findViewById(R.id.iv_clicked_image);
        mRotate_imageActivity = (TextView) findViewById(R.id.tv_rotate_imageActivity);

        mPhotoViewAttacher = new PhotoViewAttacher(mClickedImage);

        Glide.with(getApplicationContext())
                .load(string)
                .fitCenter()
                .into(mClickedImage);

        mRotate_imageActivity.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                    Glide.with(getApplicationContext())
                            .load(string)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .transform(new RotateHelper(getApplicationContext(), 90f))
                            .into(mClickedImage);

                return true;
            }
        });
    }
}

XML CODE:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.keepair.www.pinair.ImageActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rl_rotate_text">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_rotate_imageActivity"
            android:text="@string/rotate"/>
    </RelativeLayout>

    <ImageView
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/iv_clicked_image"/>
</RelativeLayout>

正如您在上面所看到的,我的xml代码表示textview在imageview上。它们是重叠的。

但我认为,由于photoviewattacher,onTouchListener是为TextView编写的,mRotate_imageActivity不起作用。

问题:如何让onTouchListener正常工作?

1 个答案:

答案 0 :(得分:1)

更改update

的图片后,您需要在PhotoViewAttacher中致电ImageView
// If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call
mAttacher.update();

在您的情况下,您可以将一个监听器附加到Glide,以了解图像何时加载并更新PhotoViewAttacher

Glide.with(getActivity())
     .load(args.getString(IMAGE_TO_SHOW))
     .listener(new RequestListener<String, GlideDrawable>() {
         @Override
         public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
             return false;
         }

         @Override
         public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
             mAttacher.update();
             return false;
         }
     })
     .into(imageView)