Code example for Matrix

Methods: getValues, postScale, postTranslate, set, setScale, setValues

0
    	ImageView view = (ImageView) v;
    	if (started == false)
    		{initImage(view);
    		started = true;
    		} 
    	mootrix.getValues(matrixArray);
 
 
    	 
    	switch(event.getAction() & MotionEvent.ACTION_MASK)
    	{ 
    	case MotionEvent.ACTION_DOWN:
    		savedMootrix.set(mootrix);
    		stoort.set(event.getX(), event.getY());
    		moved = false;
    		MODE = DRAG;
    		Log.d(TAG, "MODE=DRAG");
    		break; 
    	case MotionEvent.ACTION_POINTER_UP:
    		MODE = DRAG;
    		Log.d(TAG, "MODE=DRAG");
    	case MotionEvent.ACTION_UP:
    		if (tapped == true)
    		{ 
    	    	if (moved == false)
    			tapZoom(view, stoort);
    			tapped = false;
    		} 
    		else tapped = true;
			bounds = dr.getBounds();
			if ((matrixArray[2] + dr.getIntrinsicWidth() * (matrixArray[0])) < (view.getWidth() - 80))
				{ 
					matrixArray[2] = -(dr.getIntrinsicWidth() * (matrixArray[0])) + (view.getWidth() - 80);
					mootrix.setValues(matrixArray);
				} 
			if ((matrixArray[5] + dr.getIntrinsicHeight() * (matrixArray[4])) < (view.getHeight() - 80))
				{ 
					matrixArray[5] = -(dr.getIntrinsicHeight() * (matrixArray[4])) + (view.getHeight() - 80);
					mootrix.setValues(matrixArray);
				} 
			if (matrixArray[2] > 80)
				{ 
					matrixArray[2] = 80;
					mootrix.setValues(matrixArray);
				} 
			if (matrixArray[5] > 80)
				{ 
					matrixArray[5] = 80;
					mootrix.setValues(matrixArray);
				} 
    		MODE = NONE;
//    		Log.d(TAG, "Right: " + view.getRight() + "Bottom: " + view.getBottom()); 
    		Log.d(TAG, "MODE=NONE");
    		break; 
    	case MotionEvent.ACTION_POINTER_DOWN:
    		oldDist = spacing(event);
    		Log.d(TAG, "oldDist="+oldDist);
    		if(oldDist >= 5f)
    		{ 
    			savedMootrix.set(mootrix);
    			midPoint(mid, event);
    			stoort.set(mid);
    			MODE = ZOOM;
    			Log.d(TAG, "MODE=ZOOM");
    		} 
    		break; 
    		 
    	case MotionEvent.ACTION_MOVE:
    		moved = true;
    		if(MODE == DRAG)
    		{ 
    			mootrix.set(savedMootrix);
    			 
    			{ 
    				 
    				mootrix.postTranslate(event.getX() - stoort.x, event.getY() - stoort.y);
    			} 
    		} 
    		else if (MODE == ZOOM)
    		{ 
    			float newDist = spacing(event);
    			Log.d(TAG, "newDist=" + newDist);
    			if(newDist >= 5.0f)
    			{ 
    				mootrix.set(savedMootrix);
    				scale = (newDist/oldDist);
    				if (((scale * matrixArray[0]) >= 3) && (newDist > oldDist))
    				{scale = 3f/matrixArray[0];
    				MODE = NONE;
    				} 
    				else if (((scale * matrixArray[0]) <= 0.5) && (newDist < oldDist))
    				{scale = 0.5f/matrixArray[0];
    				MODE = NONE;
    				} 
    				{Log.d(TAG, "scale" + scale);
    				mootrix.postScale(scale, scale, mid.x, mid.y);
    				break; 
    			} 
    		} 
    		break; 
    	} 
    	} 
    	Log.d(TAG, "imgMatrix:" + matrixArray[0] + ", " + matrixArray[1] + ", " + matrixArray[2] + ", " + matrixArray[3] + ", " + matrixArray[4] + ", " + matrixArray[5] + ", " + matrixArray[6] + ", " + matrixArray[7] + ", " + matrixArray[8]);
    	view.setImageMatrix(mootrix);
    	return true; 
    } 
    	 
    public void tapZoom(ImageView v, PointF p)
    { 
    	float scale = 1.5f;
    	mootrix.getValues(matrixArray);
    	if ((matrixArray[0] * scale) > 3)
    		scale = 3f/matrixArray[0];
      	matrixArray[0] = matrixArray[4] *= scale;
    	matrixArray[2] = p.x - ((p.x - matrixArray[2])*scale);
    	matrixArray[5] = p.y - ((p.y - matrixArray[5])*scale);
    	mootrix.setValues(matrixArray);
    	v.setImageMatrix(mootrix);
    } 
    public 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);
    	 
    } 
     
    public void midPoint (PointF point, MotionEvent event)
    { 
    	float midX = event.getX(0) + event.getX(1);
    	float midY = event.getY(0) + event.getY(1);
    	point.set(midX/2, midY/2);
    } 
     
    //draws map when activity started 
	public void drawMap(int mapIndex)
	{ 
		ImageView iv = (ImageView) findViewById(R.id.main_image_view);
		TextView tv = (TextView) findViewById(R.id.screen_label);
		iv.setBackgroundColor(Color.WHITE);
		iv.setImageResource(mapsDrawable[currentMapIndex]);
		tv.setText(titles[currentMapIndex]);
	} 
	 
	//initiates matrix scaling 
	public void initImage (View view)
	{ 
		ImageView iv = (ImageView) view;
		dr = iv.getDrawable();
        iv.setScaleType(ImageView.ScaleType.MATRIX);
    	int raw_width = iv.getWidth();
    	float view_width = (float) raw_width;
    	float img_width = (float) dr.getIntrinsicWidth();
    	float down_scale = (view_width/img_width);
    	mootrix.setScale(down_scale, down_scale);
	} 
	 
	public void mapUp(View view)
	{ 
		if ((currentMapIndex == 4) ||(currentMapIndex ==  7) ||(currentMapIndex ==  9) ||(currentMapIndex == 10)||(currentMapIndex == 0)||(currentMapIndex == 1)||(currentMapIndex == 15)||(currentMapIndex == 17))