Math 缩放和滚动后区域的偏移量
缩放和滚动后getHitRect的偏移量是多少。可以计算缩放后命中区域的偏移量-(event.getX()/ZoomLayout.mScaleFactor+ZoomLayout.mclipboond.left),但我还没有计算出缩放和滚动后命中区域的偏移量Math 缩放和滚动后区域的偏移量,math,android-layout,Math,Android Layout,缩放和滚动后getHitRect的偏移量是多少。可以计算缩放后命中区域的偏移量-(event.getX()/ZoomLayout.mScaleFactor+ZoomLayout.mclipboond.left),但我还没有计算出缩放和滚动后命中区域的偏移量 public class ZoomLayout extends RelativeLayout implements OnDoubleTapListener, OnGestureListener{ //ScalingFactor i.e.
public class ZoomLayout extends RelativeLayout implements OnDoubleTapListener, OnGestureListener{
//ScalingFactor i.e. Amount of Zoom
static float mScaleFactor = 1.0f;
// Maximum and Minimum Zoom
private static float MIN_ZOOM = 1.0f;
private static float MAX_ZOOM = 2.0f;
//Different Operation to be used
private final int NONE_OPERATION=0;
private final int DRAG_OPERATION=1;
private final int ZOOM_OPERATION=2;
private float mWidth= 1280;
private float mHeight=800;
// Mode to select the operation
private int mode;
//Track X and Y coordinate of the finger when it first touches the screen
private float mInitialX = 0f;
private float mInitialY = 0f;
// Track the Bound of the Image after zoom to calculate the offset
static Rect mClipBound;
// mDetector to detect the scaleGesture for the pinch Zoom
private ScaleGestureDetector mDetector;
// mDoubleTapDetector to detect the double tap
private GestureDetector mDoubleTapDetector;
//Pivot point for Scaling
static float gx=0,gy=0;
boolean mdrag=false,mZoom=false;
public ZoomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
mClipBound = new Rect();
// Intialize ScaleGestureDetector
mDetector = new ScaleGestureDetector(getContext(), new ZoomListener());
mDoubleTapDetector = new GestureDetector(context,this);
mDoubleTapDetector.setOnDoubleTapListener(this);
}
public ZoomLayout(Context context) {
super(context);
setWillNotDraw(false);
mClipBound = new Rect();
// Intialize ScaleGestureDetector
mDetector = new ScaleGestureDetector(getContext(), new ZoomListener());
mDoubleTapDetector = new GestureDetector(context,this);
mDoubleTapDetector.setOnDoubleTapListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Handles all type of motion-events possible
switch(event.getAction() ) {
case MotionEvent.ACTION_DOWN:
// Event occurs when the first finger is pressed on the Screen
Log.d("ZoomPrint", "Event: Action_Down " );
mInitialX = event.getX();
mInitialY = event.getY();
break;
case MotionEvent.ACTION_POINTER_DOWN:
//Event occurs when the second finger is pressed down
Log.d("ZoomPrint", "Event: Action_Pointer_Down " );
// If second finger is pressed on the screen with the first set the Mode to Zoom operation
mode=ZOOM_OPERATION;
break;
case MotionEvent.ACTION_POINTER_UP:
Log.d("ZoomPrint", "Event: Action_Pointer_UP " );
mdrag=true;
case MotionEvent.ACTION_UP:
//Event occurs when all the finger are taken of the screen
Log.d("ZoomPrint", "Event: Action_UP " );
//If all the fingers are taken up there will be no operation
mode = NONE_OPERATION;
mdrag=false;
break;
}
// give the event to the mDetector to get the scaling Factor
mDetector.onTouchEvent(event);
// give the event to the mDoubleTapDetector for the doubleTap
mDoubleTapDetector.onTouchEvent(event);
if(!mdrag)
invalidate();
return true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
onTouchEvent(ev);
return super.onInterceptTouchEvent(ev);
// return true;
}
@Override
public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
return super.invalidateChildInParent(location, dirty);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
int count = getChildCount();
for(int i=0;i<count;i++){
View child = getChildAt(i);
if(child.getVisibility()!=GONE){
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)child.getLayoutParams();
child.layout(
(int)(params.leftMargin ),
(int)(params.topMargin ),
(int)((params.leftMargin + child.getMeasuredWidth()) ),
(int)((params.topMargin + child.getMeasuredHeight()))
);
}
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
//Save the canvas to set the scaling factor returned from detector
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.scale(mScaleFactor, mScaleFactor,gx,gy);
super.dispatchDraw(canvas);
mClipBound = canvas.getClipBounds();
canvas.restore();
}
private class ZoomListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
// getting the scaleFactor from the detector
mScaleFactor *= detector.getScaleFactor(); // gives the scaling factor from the previous scaling to the current
// Log.d("ZoomPrint", "detector scaling Factor" + mScaleFactor);
gx = detector.getFocusX();
gy = detector.getFocusY();
// Limit the scale factor in the MIN and MAX bound
mScaleFactor= Math.max(Math.min(mScaleFactor, MAX_ZOOM),MIN_ZOOM);
// Log.d("ZoomPrint", "Bounded scaling Factor" + mScaleFactor);
/*//Force canvas to redraw itself only if the one event is to happen (say Zooming only ) else do not invalidate here for multi operations
As what we de for scrolling or panning will not reflect here. So we will add this in onDraw method
invalidate();*/
// Here we are only zooming so invalidate has to be done
// invalidate();
// requestLayout();
// we have handle the onScale
return true;
}
}
@Override
public boolean onDoubleTap(MotionEvent e) {
// Make the mScaleFactor to its normal value
if(mScaleFactor>1.0f)
{
mScaleFactor=1.0f;
}
// Force the canvas to redraw itself again as the changes has been occured.
invalidate();
requestLayout();
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// Log.d("ZoomPrint", "OnDoubleTapEvent");
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// Log.d("ZoomPrint", "OnSingleTap");
return false;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
int distX= (int) distanceX, distY =(int) distanceY;
//Log.d("Print"," X " + this.mClipBound.left +" Y " + this.mClipBound.right + " b "+ this.mClipBound.bottom + " g" + this.mClipBound.top) ;
Log.d("Print", "Scroll X " + distanceX + " Y " + distanceY);
if(this.mClipBound.left<=0)
this.scrollTo(-280, 0);
else if(this.mClipBound.top<=0)
this.scrollTo(0, -250);
else if (this.mClipBound.right>=1047)
this.scrollTo(280, 0);
else if (this.mClipBound.bottom>=800)
this.scrollTo(0, 250);
else
this.scrollBy((int)distanceX,(int)distanceY);
return true;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
公共类ZoomLayout扩展了RelativeLayout实现OnDoubleTapListener、OnEstureListener{
//缩放因子,即缩放量
静态浮动mScaleFactor=1.0f;
//最大和最小缩放
私有静态浮动最小缩放=1.0f;
私有静态浮动最大缩放=2.0f;
//要使用的不同操作
私有最终int NONE_运算=0;
私有最终整数拖动操作=1;
私有最终整数缩放操作=2;
专用浮点数mWidth=1280;
私人浮点数mhweight=800;
//模式来选择操作
私有int模式;
//第一次触摸屏幕时跟踪手指的X和Y坐标
专用浮点数mInitialX=0f;
私有浮动最小=0f;
//缩放后跟踪图像的边界以计算偏移
静态Rect-mclipsbound;
//mDetector用于检测收缩缩放的缩放强度
私有scalegestruedetector mDetector;
//mDoubleTapDetector用于检测双抽头
私人手势检测器mDoubleTapDetector;
//缩放的轴心点
静态浮动gx=0,gy=0;
布尔值mdrag=false,mZoom=false;
公共ZoomLayout(上下文、属性集属性){
超级(上下文,attrs);
setWillNotDraw(假);
mclipboond=new Rect();
//初始化scalegestruedetector
mDetector=new ScaleGestureDetector(getContext(),new ZoomListener());
mDoubleTapDetector=新的GestureDetector(上下文,this);
mDoubleTapDetector.setOnDoubleTapListener(此);
}
公共缩放布局(上下文){
超级(上下文);
setWillNotDraw(假);
mclipboond=new Rect();
//初始化scalegestruedetector
mDetector=new ScaleGestureDetector(getContext(),new ZoomListener());
mDoubleTapDetector=新的GestureDetector(上下文,this);
mDoubleTapDetector.setOnDoubleTapListener(此);
}
@凌驾
公共布尔onTouchEvent(运动事件){
//处理所有类型的运动事件
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
//在屏幕上按下第一个手指时发生事件
Log.d(“ZoomPrint”,“事件:Action_Down”);
mInitialX=event.getX();
mInitialY=event.getY();
打破
case MotionEvent.ACTION\u指针\u向下:
//事件在按下食指时发生
Log.d(“ZoomPrint”,“事件:操作指针向下”);
//如果第一次按下屏幕上的第二个手指,则将模式设置为缩放操作
模式=缩放操作;
打破
case MotionEvent.ACTION\u指针\u向上:
Log.d(“ZoomPrint”,“事件:操作指针向上”);
mdrag=true;
case MotionEvent.ACTION\u UP:
//事件发生在将所有手指从屏幕上取下时
Log.d(“ZoomPrint”,“事件:Action_UP”);
//如果把所有的手指都拿起来,就不会动手术了
模式=无操作;
mdrag=false;
打破
}
//将事件提供给mDetector以获取比例因子
mDetector.onTouchEvent(事件);
//将事件提供给doubleTap的mDoubleTapDetector
mDoubleTapDetector.onTouchEvent(事件);
如果(!mdrag)
使无效();
返回true;
}
@凌驾
公共布尔值onInterceptTouchEvent(MotionEvent ev){
火山口(ev);
返回超级onInterceptTouchEvent(ev);
//返回true;
}
@凌驾
public ViewParent invalidateChildInParent(int[]位置,Rect dirty){
返回super.invalidateChildInParent(位置,脏);
}
@凌驾
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b)
{
int count=getChildCount();
对于(int i=0;i1.0f)
{
mScaleFactor=1.0f;
}
//在发生更改时,强制画布重新绘制自身。
使无效();
requestLayout();
返回false;
}
@凌驾
公共布尔OnDoubleTapeEvent(运动事件e){
//Log.d(“ZoomPrint”、“OnDoubleTapEvent”);
返回false;
}
@凌驾
公共布尔值OnSingleTapConfiged(运动事件e){
//Log.d(“ZoomPrint”、“OnSingleTap”);
返回false;
}
@凌驾
公共布尔onDown(运动事件e){
//TODO自动生成的方法存根
返回false;
}
@凌驾
公共布尔onFling(运动事件e1、运动事件e2、,
浮动速度x,浮动速度y){
返回false;
}
@凌驾
公开无效在线新闻(运动事件e){
}
@凌驾
公共布尔onScroll(运动事件e1、运动事件e2、,
浮动距离X,浮动距离Y){
int distX=(int)distanceX,distY=(int)distanceY;
//Log.d(“打印”,“X”+this.mclipboond.left+“Y”+this.mclipboond.right+“b”+this.mclipboond.bottom+“g”+this.mclipboond.top);
日志d(“打印”、“滚动X”+距离X+“Y”+距离Y”);
if(this.mclipboond.left=800)
这个。滚动到(0250);
其他的
this.scrollBy((int)distanceX,(int)distanceY);
返回true;
}
@凌驾
在ShowPress上公开作废(运动事件e){
}
@凌驾
公共布尔onSingleTapUp(运动事件e){
返回false;
}
您需要
canvas.translate(getScrollX(), getScrollY());