如何使用矩阵控制IMAGEVIEW的拖拽 [英] HOW TO Control THE DRAG OF THE IMAGEVIEW using matrix
问题描述
我在我的应用中使用矩阵来缩放拖动,我使用自定义ImageView
I'm using Matrix in my app to zoom an drag, i use a custom ImageView
我的问题:
我无法控制拖动限制(缩放最大值和最小值已完成)
我试试解决这个问题,根据变焦寻找矩阵的值,但不是更好的方法,因为它根据设备而变化,我可以控制0.9到1.7之间的变焦并手动
I try solve this, looking for the values of the matrix depending the zoom but is not the better way because it change depending the device and I just can control the zoom between 0.9 and 1.7 and manually
我需要一种更好的方法来控制角落。
我的问题是值[2]和值[5]的矩阵
My problem is with values[2] and values[5] of the matrix
感谢您的帮助,谢谢! :D
I appreciate some help, thanks! :D
现在代码
我现在使用的是2种方法为此:
There are the 2 methods i use now for this:
此方法取决于矩阵的缩放(值[0]和值[4])和
将自定义参数发送到limitCorners(浮点浮动) )
This method chech the zoom of the matrix(values[0]and values[4]) and send custom parameters to limitCorners(float float)
public void checkZoom(){
float[] values = new float[9];
matrix.getValues(values);
//compruebo el zoom
float scaleX = values[Matrix.MSCALE_X];
float scaleY = values[Matrix.MSCALE_Y];
if(scaleX > MAX_ZOOM) {
scaleX = MAX_ZOOM;
} else if(scaleX < MIN_ZOOM) {
scaleX = MIN_ZOOM;
}
if(scaleY > MAX_ZOOM) {
scaleY = MAX_ZOOM;
} else if(scaleY < MIN_ZOOM) {
scaleY = MIN_ZOOM;
}
values[Matrix.MSCALE_X] = scaleX;
values[Matrix.MSCALE_Y] = scaleY;
matrix.setValues(values);
//Second part: depend zoom send values
//a limitaBordes(float, float)
valores = new float[9];
matrix.getValues(valores);
if (valores[0]<=MIN_ZOOM){
valores[0]=MIN_ZOOM;
valores[4]=MIN_ZOOM;
valores[2]=0;
valores[5]=0;
matrix.setValues(valores);
//imageView.setImageMatrix(matrix);
}else if(valores[0]>0.9 && valores[0]<=1){
limitCorners(-65, -40);
}else if(valores[0]>1 && valores[0]<=1.1){
limitCorners(-166, -123);
}else if(valores[0]>1.1 && valores[0]<=1.2){
limitCorners(-246, -201);
}else if(valores[0]>1.2 && valores[0]<=1.3){
limitCorners(-316, -280);
}else if(valores[0]>1.3 && valores[0]<=1.4){
limitCorners(-409, -359);
}else if(valores[0]>1.4 && valores[0]<=1.5){
limitCorners(-484, -435);
}else if(valores[0]>1.5 && valores[0]<=1.6){
limitCorners(-563, -521);
}else {// (valores[0]>1.6f)
limitCorners(-664, -600);
}
}
>
void limitCorners(float valorX, float valorY){
//log("determinando");
//if(x>0)
if(valores[2]>0){
valores[2]=0;
matrix.setValues(valores);
}//if(y>0)
if(valores[5]>0){
valores[5]=0;
matrix.setValues(valores);
}//if(x<valorX)
if(valores[2]< valorX){
valores[2]= valorX;
matrix.setValues(valores);
}//if(y<valorY)
if(valores[5]< valorY){
valores[5]= valorY;
matrix.setValues(valores);
}
imageView.setImageMatrix(matrix);
}
推荐答案
我解决了!
如果有人遇到同样的问题,我把代码放在这里:
I put here the code if somebody have the same problem:
public void checkZoom(){
float[] values = new float[9];
matrix.getValues(values);
float scaleX = values[Matrix.MSCALE_X];
if(scaleX > MAX_ZOOM) {
setZoom(MAX_ZOOM);
}
else if(scaleX < MIN_ZOOM) {
setZoom(MIN_ZOOM);
}
limitCorners();
}
private void limitCorners() {
viewWidth = this.getWidth();
viewHeight = this.getHeight();
float []m = new float[9];
matrix.getValues(m);
float transX = m[Matrix.MTRANS_X];
float transY = m[Matrix.MTRANS_Y];
float fixTransX = getFixTrans(transX, viewWidth, getImageWidth());
float fixTransY = getFixTrans(transY, viewHeight, getImageHeight());
if (fixTransX != 0 || fixTransY != 0) {
matrix.postTranslate(fixTransX, fixTransY);
}
matrix.getValues(m);
if (getImageWidth() < viewWidth) {
m[Matrix.MTRANS_X] = (viewWidth - getImageWidth()) / 2;
}
if (getImageHeight() < viewHeight) {
m[Matrix.MTRANS_Y] = (viewHeight - getImageHeight()) / 2;
}
matrix.setValues(m);
}
private float getFixTrans(float trans, float viewSize, float contentSize) {
float minTrans, maxTrans;
if (contentSize <= viewSize) {
minTrans = 0;
maxTrans = viewSize - contentSize;
} else {
minTrans = viewSize - contentSize;
maxTrans = 0;
}
if (trans < minTrans)
return -trans + minTrans;
if (trans > maxTrans)
return -trans + maxTrans;
return 0;
}
/** Get the width of image (bitmapWidth*Zoom)*/
float getImageWidth(){
float []m = new float[9];
matrix.getValues(m);
return m[Matrix.MSCALE_X]*bitmap.getWidth();
}
/** Get the Height of image (bitmapHeight*Zoom)*/
float getImageHeight(){
float []m = new float[9];
matrix.getValues(m);
return m[Matrix.MSCALE_X]*bitmap.getHeight();
}
这篇关于如何使用矩阵控制IMAGEVIEW的拖拽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!