Reputation: 55
I'm kind of fresh in adnroid, so asking for an advice.
I'm trying to make 2 imageViews, which could be zoomable. For this reason I've implemented https://github.com/chrisbanes/PhotoView library, which makes imageView fully zoomable, double clicks zoom and etc.
The key idea I want to make, is to copy gestures from one image, and apply those gestures to another(For example, if the first image is beeing zoomed, I want the other one to be zoomed exactly the same). The logical way to do that is writing an setOnTouchListener for photoView(imageView from library) and in onTouch method call other image view and dispachTouchEvent for it. But writing setOnTouchListener requeres to override onTouch method, which then makes the funcionaluty of photoView don't work.
So my question is, can I even override libraries mathods ? Or if I can,maybe that's the drawback, that i loose all it's functionality ? If I can't do that, do you guys have any other advices how to copy gestures from one imageView to another? Or maybe it would work with other zoomable image libraries ? The main point of using external library was not to reinvent the whole zoomable imageView.
Thanks in advance!
PhotoView image = findViewById(R.id.photo);
PhotoView image2 = findViewById(R.id.photo2);
image.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
image2.dispatchTouchEvent(motionEvent);
return false;
}
});
Upvotes: 1
Views: 2381
Reputation: 8758
There is another option to achieve what you want due to how Java classloader works. But before that you need to check license (if any) for a library you want to change. If you have a source code of a class then you can create a class in your project with exactly same full class name as a class you want to change. Then write your code in that class in your project. And when you start your application Java classloader will load class from your project with your code instead of a class from library. This approach will work even if class from library is final or method you want to override is final.
Upvotes: 3
Reputation: 3234
You may not override the methods directly in an external library.
However, if the class you want to override methods for is not final
and the methods you want to override is not final
, then you can create a subclass of the class.
For example, if PhotoView
isn't a class marked with final
, I can create something like:
public class CustomPhotoView extends PhotoView {
@Override
public void setOnTouchListener(OnTouchListener l) {
super.setOnTouchListener(l);
//Do stuff
}
}
Essentially, it's the same as when you create custom Views of your own by subclassing ImageView
other other Views.
But you're also allowed to create a branch or a clone of PhotoView
and make your changes directly to that PhotoView
. Afterwards, you use your own implementation of PhotoView
instead of the one from Chris Banes.
Upvotes: 4