双指缩放的自定义视图

编程入门 行业动态 更新时间:2024-10-11 13:30:19
本文介绍了双指缩放的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经创建了我的自定义视图,我想申请双指缩放为我的自定义视图。那怎么办?

I have created my custom view and I want to apply pinch zoom for my custom view. How to do that?

推荐答案

这篇文章在Android开发者博客介绍这个主题非常好(向下滚动到 GestureDetectors的栏目):

This article on the Android Developers Blog covers this topic very well (scroll down to the section on GestureDetectors):

制作多点触摸感

如果你只是想实现双指缩放,有$ C $只有几行C,你需要:

If you just want to implement pinch-to-zoom, there's only a few lines of code you'll need:

private ScaleGestureDetector mScaleDetector; private float mScaleFactor = 1.f; public MyCustomView(Context mContext){ //... //Your view code //... mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); } @Override public boolean onTouchEvent(MotionEvent ev) { // Let the ScaleGestureDetector inspect all events. mScaleDetector.onTouchEvent(ev); return true; } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.save(); canvas.scale(mScaleFactor, mScaleFactor); //... //Your onDraw() code //... canvas.restore(); } private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { @Override public boolean onScale(ScaleGestureDetector detector) { mScaleFactor *= detector.getScaleFactor(); // Don't let the object get too small or too large. mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f)); invalidate(); return true; } }

的文章涉及处理其它手势而不是使用其实施的休息,可以使用 GestureDetector 就像ScaleGestureDetector用在code以上。

The rest of the article deals with handling other gestures but rather than using their implementation, you can use GestureDetector just like ScaleGestureDetector is used in the code above.

更多推荐

双指缩放的自定义视图

本文发布于:2023-10-30 07:25:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   缩放   视图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!