applibgroup / pan_and_zoom Goto Github PK
View Code? Open in Web Editor NEWLibrary to pan and zoom images (InProgress)
Library to pan and zoom images (InProgress)
Issue:
Drag functionality is not working.
Problem:
if condition failed and startFilingAnimation() is not called. Because value is not getting proper.
Implementation in Android:
In Android, to detect drag event we use GestureDetector.SimpleOnGestureListener() inner class and override onFling() method as follow:
GestureDetector gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Perform dragging using parameters.
return super.onFling(e1, e2, velocityX, velocityY);
}
});
Here, In onFling method we have 4 parameters (i.e. MotionEvent e1, e2, velocityX and velocityY) and on the basics of these parameters we calculate centerXEnd and centerYEnd and pass in startFilingAnimation() method. Complete implementation show in below code
mFlingDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (mTranslateEnabled && mReadySent && mViewTranslate != null && e1 != null && e2 != null
&& (Math.abs(e1.getX() - e2.getX()) > 50 || Math.abs(e1.getY() - e2.getY()) > 50)
&& (Math.abs(velocityX) > 500 || Math.abs(velocityY) > 500) && !mIsZooming) {
PointF vTranslateEnd =
new PointF(mViewTranslate.x + (velocityX * 0.25f), mViewTranslate.y + (velocityY * 0.25f));
float sCenterXEnd = ((getWidth() / 2.0F) - vTranslateEnd.x) / mScale;
float sCenterYEnd = ((getHeight() / 2.0F) - vTranslateEnd.y) / mScale;
startFilingAnimation(sCenterXEnd, sCenterYEnd);
if (BuildConfig.DEBUG) Log.d(TAG, "onFling: 正在滑行");
return true;
}
return false;
}
});
But In HMOS, to detect drag event we have Component.DraggedListener callbak and then we have to override onDragDown, onDragStart, onDragUpdate, onDragEnd and onDragCancel methods. And every methods have two parameters (i.e. Component and DragInfo).
So currently to achieve same functionality I did as follow:
@Override
public void onDragStart(Component component, DragInfo dragInfo) {
LogUtil.info(TAG, "onDragStart");
dragInfoStart = dragInfo;
}
@Override
public void onDragEnd(Component component, DragInfo dragInfo) {
LogUtil.info(TAG, "onDragEnd");
mIsZooming = false;
mIsPanning = false;
mMaxTouchCount = 0;
dragInfoEnd = dragInfo;
permormDrag();
}
private void permormDrag() {
Point e1 = new Point(dragInfoStart.startPoint);
Point e2 = new Point(dragInfoEnd.updatePoint);
LogUtil.info(TAG,
"Perform drag e1: " + e1.toString() +
" e2: " + e2.toString() +
" condition 1st: " + (Math.abs(e1.getPointX() - e2.getPointX()) > 50 || Math.abs(e1.getPointY() - e2.getPointY()) > 50) +
" condition 2nd: " + (Math.abs(mVelocityDetector.getHorizontalVelocity()) > 500 || Math.abs(mVelocityDetector.getVerticalVelocity()) > 500) +
" mViewTranslate: " + mViewTranslate.toString()
);
if (mTranslateEnabled && mReadySent && mViewTranslate != null && e1 != null && e2 != null
&& (Math.abs(e1.getPointX() - e2.getPointX()) > 50 || Math.abs(e1.getPointY() - e2.getPointY()) > 50)
&& (Math.abs(mVelocityDetector.getHorizontalVelocity()) > 500 || Math.abs(mVelocityDetector.getVerticalVelocity()) > 500) && !mIsZooming){
LogUtil.info(TAG, "onDragEnd If condition true");
PointF vTranslateEnd =
new PointF(mViewTranslate.x + ((float) mVelocityDetector.getHorizontalVelocity() * 0.25f), mViewTranslate.y + ((float) mVelocityDetector.getVerticalVelocity() * 0.25f));
float sCenterXEnd = ((getWidth() / 2.0F) - vTranslateEnd.x) / mScale;
float sCenterYEnd = ((getHeight() / 2.0F) - vTranslateEnd.y) / mScale;
startFilingAnimation(sCenterXEnd, sCenterYEnd);
}
}
But, above workaround is not working.
App crash because Attempt to update UI in non-UI thread.
Whenever try to call resetScaleAndCenter() method of HDImageView component class from MainAbilitySlice class as follow
ImageSource imageSource = ImageSourceBuilder.newBuilder()
.setUri(IMAGE_URL)
.setImageSourceLoadListener((uri, options) -> {
float scaleW = mImageView.getWidth() / options.mWidth;
float scaleH = mImageView.getHeight() / options.mHeight;
mImageView.setMinScale(Math.min(1.0f , Math.min(scaleW , scaleH)));
mImageView.resetScaleAndCenter();
})
.build();
App has crashed. with following error logs
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: Process: xyz.zpayh.hdimage, PID: 22083
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: java.lang.IllegalStateException: Attempt to update UI in non-UI thread.
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: at ohos.agp.components.Component.nativeInvalidate(Native Method)
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: at ohos.agp.components.Component.invalidate(Component.java:1060)
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: at xyz.zpayh.hdimageview.HDImageView.resetScaleAndCenter(HDImageView.java:1730)
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: at xyz.zpayh.hdimage.slice.MainAbilitySlice.lambda$onStart$0(MainAbilitySlice.java:35)
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: at xyz.zpayh.hdimage.slice.-$$Lambda$MainAbilitySlice$PqsU8NZc8aGkmmm02gZheMtcNfo.loadSuccess(Unknown Source:2)
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: at xyz.zpayh.hdimageview.HDImageView.onTilesInitialized(HDImageView.java:1417)
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: at xyz.zpayh.hdimageview.HDImageView$OriginalHandler.processEvent(HDImageView.java:374)
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: at ohos.eventhandler.EventHandler.distributeEvent(EventHandler.java:890)
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: at ohos.eventhandler.EventRunner$EventInnerRunner.startToRun(EventRunner.java:142)
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: at ohos.eventhandler.EventRunner$EventInnerRunner.run(EventRunner.java:98)
09-03 14:42:06.625 22083-24394/? E AndroidRuntime: at java.lang.Thread.run(Thread.java:929)
In Ability class whenever try to get custom component from findComponentById() method, It return null.
HDImageView mImageView = (HDImageView) findComponentById(ResourceTable.Id_hd_image_view);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.