如何在缩放时放大图像视图中的默认图像尺寸?

时间:2014-06-26 06:03:38

标签: java android imageview zoom

我正在使用水平滚动视图,当我点击水平滚动视图的图像时,我的图像显示在imageview中。此功能正常运行。 在图像视图中,我可以缩放图像。但是在缩放之后,当我点击scrollview的下一个图像按钮时,imageview bydefault显示缩放图像。实际上它正在缩放上一张图像。所以我的问题是如何删除以前的缩放,为下一个图像。我的意思是下一张图片必须带有默认尺寸,尚未缩放。

这是我的代码。

public class ViewButtonActivity extends Activity implements OnClickListener,
    OnTouchListener {
ImageView imgView;
private ProgressDialog pDialog;
public static boolean isTouch = false;
ImageButton imgButton1, imgButton2, imgButton3, imgButton4, imgButton5;
HorizontalScrollView horizontalScrollView;
public ImageLoader imageLoader;
public static String[] imageUrl = {
        "http://0-03/_cover.jpg",
        "http://www.magazine.jpg",
        "http://large_1.jpg",
        "http://4.bp.Apr2011.jpg",
        "http://www.theof.com/git.jpg" };

private int mImageHeight, mImageWidth;
private static final String TAG = "Touch";
private static final float MIN_ZOOM = 1f, MAX_ZOOM = 1f;

Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_button_click_activity);

    imageLoader = new ImageLoader(getApplicationContext());
    imgView = (ImageView) findViewById(R.id.image_view);
    imgButton1 = (ImageButton) findViewById(R.id.imageButton1);
    imgButton2 = (ImageButton) findViewById(R.id.imageButton2);
    imgButton3 = (ImageButton) findViewById(R.id.imageButton3);
    imgButton4 = (ImageButton) findViewById(R.id.imageButton4);
    imgButton5 = (ImageButton) findViewById(R.id.imageButton5);
    horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontal_scroll_view);


      imageLoader.DisplayImage(imageUrl[0], imgButton1);
     imageLoader.DisplayImage(imageUrl[1], imgButton2);
     imageLoader.DisplayImage(imageUrl[2], imgButton3);
     imageLoader.DisplayImage(imageUrl[3], imgButton4);
      imageLoader.DisplayImage(imageUrl[4], imgButton5);

    imgButton1.setOnClickListener(this);
    imgButton2.setOnClickListener(this);
    imgButton3.setOnClickListener(this);
    imgButton4.setOnClickListener(this);
    imgButton5.setOnClickListener(this);

    mImageHeight = imgView.getWidth();
    mImageWidth = imgView.getHeight();

    imgView.setOnTouchListener(this);

      }

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.imageButton1:
            imageLoader.DisplayImage(imageUrl[0], imgView);
        break;

    case R.id.imageButton2:
        imageLoader.DisplayImage(imageUrl[1], imgView);
        break;

    case R.id.imageButton3:
        imageLoader.DisplayImage(imageUrl[2], imgView);
        break;

    case R.id.imageButton4:
        imageLoader.DisplayImage(imageUrl[3], imgView);
        break;

    case R.id.imageButton5:
        imageLoader.DisplayImage(imageUrl[4], imgView);
        // imgView.setImageResource(R.drawable.img5);
        break;
    }
}

public boolean onTouch(View v, MotionEvent event) {
    ImageView imgView = (ImageView) v;
    imgView.setScaleType(ImageView.ScaleType.MATRIX);
    float scale;

    dumpEvent(event);

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: // first finger down only
        savedMatrix.set(matrix);
        start.set(event.getX(), event.getY());
        Log.d(TAG, "mode=DRAG"); // write to LogCat
        mode = DRAG;
        break;

    case MotionEvent.ACTION_UP: // first finger lifted

    case MotionEvent.ACTION_POINTER_UP: // second finger lifted

        mode = NONE;
        Log.d(TAG, "mode=NONE");
        break;

    case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down

        oldDist = spacing(event);
        Log.d(TAG, "oldDist=" + oldDist);
        if (oldDist > 5f) {
            savedMatrix.set(matrix);
            midPoint(mid, event);
            mode = ZOOM;
            Log.d(TAG, "mode=ZOOM");
        }
        break;

    case MotionEvent.ACTION_MOVE:

        if (mode == DRAG) {
            matrix.set(savedMatrix);
            matrix.postTranslate(event.getX() - start.x, event.getY()
                    - start.y); // create the transformation in the matrix
                                // of points
        } else if (mode == ZOOM) {
            // pinch zooming
            float newDist = spacing(event);
            Log.d(TAG, "newDist=" + newDist);
            if (newDist > 5f) {
                matrix.set(savedMatrix);
                scale = newDist / oldDist; // setting the scaling of the
                                            // matrix...if scale > 1 means
                                            // zoom in...if scale < 1 means
                                            // zoom out
                matrix.postScale(scale, scale, mid.x, mid.y);
            }
        }
        break;
    }
    imgView.setImageMatrix(matrix);
    return true;
}

private float spacing(MotionEvent event) 
{
    float x = event.getX(0) - event.getX(1);
    float y = event.getY(0) - event.getY(1);
    return FloatMath.sqrt(x * x + y * y);
}
private void midPoint(PointF point, MotionEvent event) 
{
    float x = event.getX(0) + event.getX(1);
    float y = event.getY(0) + event.getY(1);
    point.set(x / 2, y / 2);
}

private void dumpEvent(MotionEvent event) 
{
    String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE","POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
    StringBuilder sb = new StringBuilder();
    int action = event.getAction();
    int actionCode = action & MotionEvent.ACTION_MASK;
    sb.append("event ACTION_").append(names[actionCode]);

    if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) 
    {
        sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
        sb.append(")");
    }

    sb.append("[");
    for (int i = 0; i < event.getPointerCount(); i++) 
    {
        sb.append("#").append(i);
        sb.append("(pid ").append(event.getPointerId(i));
        sb.append(")=").append((int) event.getX(i));
        sb.append(",").append((int) event.getY(i));
        if (i + 1 < event.getPointerCount())
            sb.append(";");
    }

    sb.append("]");
    Log.d("Touch Events ---------", sb.toString());
}

}

1 个答案:

答案 0 :(得分:0)

大概你必须重置为包含转换当前状态(包括缩放)的Matrix的原始值?我看到你已经跟踪了Matrix,当你在onClick()中前进到下一个图像时,你有什么方法将它重置为旧值?