如何限制在Android中的ImageView图像平移边界边界、图像、Android、ImageView

2023-09-05 05:05:17 作者:迢迢相思意

我有一个多点触控的ImageView的大致基于本教程。一个评论者那里拼凑限制图像拖动到图像的边界的一个半脏的方法,以使图像边缘不能被拖动超出其边缘。这种方法八九不离十工作,但不完全是。它唯一的限制拖累的两个边缘。

有谁知道限制的图像拖不那么混乱和实际功能的方法?

这是Android应用程序开发未充分解决......

一个非常重要的概念,

我在考虑以下思路:

1)setScaleType(scaleType.fitXY)当变焦= 1.0F(即分变焦),并拖动才会启用时,变焦> 1.0F

2)当变焦> 1.0F,setScaleType(scaleType.MATRIX),然后确定图像边界和屏幕尺寸,并且在某些方面是太聪明对我来说,使用if语句只允许拖拉时,图像边缘并不是在屏幕上。我不知道如何来声明,是东西。

反正,完整性,这里是从该链接极限泛code。这似乎是在计算器上最流行的建议,但我认为我们可以做的更好:

  //限制锅
matrix.getValues​​(matrixValues​​);
浮currentY = matrixValues​​ [Matrix.MTRANS_Y]
浮currentX = matrixValues​​ [Matrix.MTRANS_X]
浮currentScale = matrixValues​​ [Matrix.MSCALE_X]
浮currentHeight =身高* currentScale;
浮currentWidth =宽* currentScale;
浮DX = event.getX() -  start.x;
浮DY = event.getY() -  start.y;
浮下一页末= currentX + DX;
浮newY = currentY + DY;

RectF drawingRect =新RectF(下一页末,newY,下一页末+ currentWidth,newY + currentHeight);
浮diffUp = Math.min(viewRect.bottom-drawingRect.bottom,viewRect.top-drawingRect.top);
浮diffDown = Math.max(viewRect.bottom-drawingRect.bottom,viewRect.top-drawingRect.top);
浮diffLeft = Math.min(viewRect.left-drawingRect.left,v​​iewRect.right-drawingRect.right);
浮diffRight = Math.max(viewRect.left-drawingRect.left,v​​iewRect.right-drawingRect.right);
如果(diffUp大于0){
DY + = diffUp;
}
如果(diffDown℃,){
DY + = diffDown;
}
如果(diffLeft大于0){
DX + = diffLeft;
}
如果(diffRight℃,){
DX + = diffRight;
}
matrix.postTranslate(DX,DY);
 

解决方案

 私人无效limitDrag(矩阵m,ImageView的视图){


    浮动[]值=新的浮动[9];
    m.getValues​​(值);
    浮transX =值[Matrix.MTRANS_X]
    浮transY =值[Matrix.MTRANS_Y]
    浮动的scaleX =值[Matrix.MSCALE_X]
    浮动的scaleY =值[Matrix.MSCALE_Y]

    矩形边界= view.getDrawable()的getBounds()。
    INT viewWidth = getResources()getDisplayMetrics()widthPixels。;
    INT viewHeight = getResources()getDisplayMetrics()heightPixels。;



    如果(viewHeight< = 480)
    {

        _y_up = 0;
    }
    如果(viewHeight> 480安培;&安培; viewHeight< 980)
    {

        _y_up = 140;
    }

    INT宽度= bounds.right  -  bounds.left;
    INT高= bounds.bottom  -  bounds.top;
    INT __width =宽度;
    INT __height =高度;
    宽度= viewWidth / 2;
    身高= viewHeight / 2;


    //高度= 200;
    漂浮其minX =( - 宽度); // *的scaleX;
    浮动MINY =(-height); // *的scaleY;


    如果((transX)>(viewWidth)){

        // _ x_left

        transX = viewWidth;
    }否则如果(transX<其minX){


        transX =其minX;
    }


    如果((-transX)>(viewWidth)){
             // _x_right
        transX =  - (viewWidth);
    }否则如果(-transX<其minX){

        transX =  - (其minX + 30);
    }



    如果((transY)>(viewHeight)){
    //  _对
        transY =(viewHeight);


    }否则如果(transY< MINY){

        transY =(MINY + _y_up);
    }

    如果((-transY)>(viewHeight)){
    // _y_down
        transY =  - (viewHeight);

    }否则如果(-transY< MINY){

        transY =  - (MINY + 170);
    }

    值[Matrix.MTRANS_X] = transX;
    值[Matrix.MTRANS_Y] = transY;
    m.setValues​​(值);
}
 
android中的ImageView,ImageView加载网络图片

调用这个上面你view.setImageMatrix(矩阵);

I have an imageView with multitouch roughly based on this tutorial. One of the commenters there put together a semi-dirty method of limiting the image drag to the boundaries of the image, so that the image edge cannot be dragged beyond its edge. This method sorta works, but not entirely. It only limits drag of two edges.

Does anyone know a less messy and actually functional method for limiting image drag?

This is a highly important concept for android app development that is not adequately addressed....

I was thinking of the following ideas:

1) setScaleType(scaleType.fitXY) when zoom = 1.0F (i.e. min zoom), and drag only enabled when zoom > 1.0f

2) when zoom > 1.0f, setScaleType(scaleType.MATRIX), then you determine image bounds and screen dimensions, and in some way that is too smart for me, using an if statement you only allow drag when the image edge is not on the screen. I don't know how to declare that, is the thing.

anyways, for completeness, here is the limit pan code from that link. This seems to be the most popular suggestion on stackoverflow, but I think we can do better:

// limit pan
matrix.getValues(matrixValues);
float currentY = matrixValues[Matrix.MTRANS_Y];
float currentX = matrixValues[Matrix.MTRANS_X];
float currentScale = matrixValues[Matrix.MSCALE_X];
float currentHeight = height * currentScale;
float currentWidth = width * currentScale;
float dx = event.getX() - start.x;
float dy = event.getY() - start.y;
float newX = currentX+dx;
float newY = currentY+dy; 

RectF drawingRect = new RectF(newX, newY, newX+currentWidth, newY+currentHeight);
float diffUp = Math.min(viewRect.bottom-drawingRect.bottom, viewRect.top-drawingRect.top);
float diffDown = Math.max(viewRect.bottom-drawingRect.bottom, viewRect.top-drawingRect.top);
float diffLeft = Math.min(viewRect.left-drawingRect.left, viewRect.right-drawingRect.right);
float diffRight = Math.max(viewRect.left-drawingRect.left, viewRect.right-drawingRect.right);
if(diffUp > 0 ){
dy +=diffUp; 
}
if(diffDown < 0){
dy +=diffDown;
} 
if( diffLeft> 0){ 
dx += diffLeft;
}
if(diffRight < 0){
dx += diffRight;
}
matrix.postTranslate(dx, dy);

解决方案

private void limitDrag(Matrix m, ImageView view) {


    float[] values = new float[9];
    m.getValues(values);
    float transX = values[Matrix.MTRANS_X];
    float transY = values[Matrix.MTRANS_Y];
    float scaleX = values[Matrix.MSCALE_X];
    float scaleY = values[Matrix.MSCALE_Y];

    Rect bounds = view.getDrawable().getBounds();
    int viewWidth = getResources().getDisplayMetrics().widthPixels;
    int viewHeight = getResources().getDisplayMetrics().heightPixels;



    if(viewHeight<=480)
    {

        _y_up=0;
    }
    if(viewHeight>480&&viewHeight<980)
    {

        _y_up=140;
    }

    int width = bounds.right - bounds.left;
    int height = bounds.bottom - bounds.top;
    int __width=width;
    int __height=height;
    width = viewWidth / 2;
    height = viewHeight / 2;


    //height = 200 ;
    float minX = (-width) ;//* scaleX;
    float minY = (-height) ;//* scaleY;


    if ((transX) > (viewWidth)) {

        //_x_left

        transX = viewWidth;
    } else if (transX < minX) {


        transX = minX;
    }


    if ((-transX) > (viewWidth)) {
             // _x_right
        transX = -(viewWidth);
    } else if (-transX < minX) {

        transX = -(minX+30);
    }



    if ((transY) > (viewHeight)) {
    //  _y_up
        transY =( viewHeight);


    } else if (transY < minY) {

        transY = (minY+_y_up);
    }

    if ((-transY) > (viewHeight)) {
    //  _y_down
        transY = -(viewHeight);

    } else if (-transY < minY) {

        transY = -(minY+170);
    }

    values[Matrix.MTRANS_X] = transX;
    values[Matrix.MTRANS_Y] = transY;
    m.setValues(values);
}

call this above your view.setImageMatrix(matrix) ;