Note that there are some explanatory texts on larger screens.

plurals
  1. POClickable area after Scaling with respect to positions of touch event?
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/8180442/how-to-find-absolute-position-of-click-while-zoomed-in">How to find absolute position of click while zoomed in</a> </p> </blockquote> <p>Trying to get the clickable area after scaling custom relative layout. When I scale(mScaleFactor,mScaleFactor) this code works properly since the Zoom happens from the top left corner and the calculation gives the clickable area position at the half of the mScaleFactor which is passed to the viewContains where it checks the position of the imageviews. Now I am facing problem When I try scaling at the different position by passing scale(mScaleFactor,mScaleFactor,click_locn_x,click_locn_y). I am not getting the calculations correct. How to get the clickable area when I scale with click as the midpoint. Rite now I am at initial stages of scaling and trying to learn. I am going step by step. please help me with the clickable area and is there any similar things done by someone ...example will be greatful !!! </p> <p>here is the code</p> <pre><code>public class LayoutZoomingActivity extends Activity implements OnTouchListener { ImageView img1,img3,img4,img5,img6 ; ImageView img2; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); img1 = (ImageView) findViewById(R.id.imageView1); img1.setOnTouchListener(this); /* img2 = (ImageView) findViewById(R.id.imageView2); img2.setOnTouchListener(this); img3 = (ImageView) findViewById(R.id.imageView3); img3.setOnTouchListener(this); img4 = (ImageView) findViewById(R.id.imageView4); img4.setOnTouchListener(this); img5 = (ImageView) findViewById(R.id.imageView5); img5.setOnTouchListener(this); img6 = (ImageView) findViewById(R.id.imageView6); img6.setOnTouchListener(this);*/ } @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub Log.d("Print","b"); if(isViewContains(img1, (int)event.getX(), (int)event.getY())) { img1.setVisibility(View.GONE); } return false; } @Override public boolean onTouchEvent(MotionEvent event) { Log.d("Print","c"+(int)event.getX()+","+(int)event.getY()); if(isViewContains(img1, (int)(event.getX()/scaleLayout.mScaleFactor), (int)(event.getY()/scaleLayout.mScaleFactor))) { img1.setVisibility(View.GONE); img1.invalidate(); } return super.onTouchEvent(event); } private boolean isViewContains(View view, int rx, int ry) { int[] l = new int[2]; // Position of the view onScreen view.getLocationOnScreen(l); Log.d("Print"," Location On Screen: View Name - " + view.getTag() + " x : " + l[0] + " y: " + l[1]); int x = l[0]; int y = l[1]; int w = view.getWidth(); int h = view.getHeight(); Log.d("Print"+ " isViewContains::"+x+"::"+y+"::"+w+"::"+h,rx+"::"+ry); // Checking the clickable box area if (rx &lt; x || rx &gt; x + w || ry &lt; y || ry &gt; y + h) { return false; } return true; } @Override protected void onPause() { finish(); super.onPause(); } @Override protected void onStop() { finish(); super.onStop(); } @Override protected void onDestroy() { finish(); super.onDestroy(); } } </code></pre> <p>and here is the custom layout</p> <pre><code>public class scaleLayout extends RelativeLayout { static float mScaleFactor=1.0f; int click_Locn_x,click_Locn_y; private long lastTouchTime = -1; public scaleLayout(Context context) { super(context); setWillNotDraw(false); } public scaleLayout(Context context, AttributeSet attrs) { super(context, attrs); setWillNotDraw(false); // TODO Auto-generated constructor stub } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return super.onTouchEvent(event); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { click_Locn_x = (int) ev.getX(); click_Locn_y = (int) ev.getY(); long thisTime = System.currentTimeMillis(); if (thisTime - lastTouchTime &lt; 250) { // Double tap if(mScaleFactor==1.5f) mScaleFactor=1.0f; else mScaleFactor=1.5f; invalidate(); lastTouchTime = -1; } else { // Too slow :) /* mScaleFactor=1.0f; invalidate();*/ lastTouchTime = thisTime; } } return super.onInterceptTouchEvent(ev); } @Override protected void dispatchDraw(Canvas canvas) { // TODO Auto-generated method stub canvas.save(Canvas.MATRIX_SAVE_FLAG); canvas.scale(mScaleFactor, mScaleFactor,click_Locn_x,click_Locn_y); super.dispatchDraw(canvas); canvas.restore(); } @Override public ViewParent invalidateChildInParent(int[] location, Rect dirty) { // TODO Auto-generated method stub return super.invalidateChildInParent(location, dirty); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload