Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid, missing touch events on ImageView
    text
    copied!<p>I have understood this is a problem of my code. But I can't find out which is the problem. I downloaded a "sample project" to use the touchListener, it works, but if I try to do the same on my project, suddently I can get only "press down button" events.</p> <p>Activity:</p> <pre><code> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.navigator_layout); b = this.getIntent().getStringArrayExtra("building"); cv = (MapView) this.findViewById(R.id.map); MapMovement mm = new MapMovement(cv); compass = new Compass(this, cv, debug); } </code></pre> <p>map.xml:</p> <pre><code> &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/black" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" &gt; &lt;it.inav.graphics.MapView android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>I omitted some parts of MapView, where I get the images and so on, ask if you think is important:</p> <pre><code> public class MapView extends ImageView { ... public MapView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MapView(Context context, AttributeSet attrs) { super(context, attrs); } public MapView(Context context) { super(context); } public void init(List&lt;Floor&gt; floors) { this.floors = floors; setFocusable(true); } private void setInitialZoom() { if (screen_center == null) return; // recupero la dimensione dell'immagine int image_width = bmp.getWidth(); int image_heigth = bmp.getHeight(); int longest_i = image_width; if (longest_i &lt; image_heigth) longest_i = image_heigth; float longest_s = screen_center.x * 2; if (longest_s &lt; screen_center.y * 2) longest_s = screen_center.y * 2; float zoom = (float)longest_s / longest_i; this.min_zoom = zoom; this.zoom = zoom; setImageCenter(null); } private void setZoom(float zoom) { if (zoom &lt; min_zoom) this.zoom = min_zoom; else if (zoom &gt; 1) this.zoom = 1; else this.zoom = zoom; this.invalidate(); } private void setImageCenter(PointF posizione) { if (posizione != null) image_center = new PointF(posizione.x, posizione.y); else image_center = new PointF(bmp.getWidth(), bmp.getHeight()); } private void prepareImage(Canvas canvas) { canvas.rotate((float)(bearing - selected_floor.bearing), screen_center.x, screen_center.y); // scalo l'immagine image = new Matrix(); image.setScale(zoom, zoom); Paint drawPaint = new Paint(); drawPaint.setAntiAlias(true); drawPaint.setFilterBitmap(true); float centerScaledWidth = image_center.x * zoom / 2; float centerScaledHeigth = image_center.y * zoom / 2; image.postTranslate(screen_center.x - centerScaledWidth, screen_center.y - centerScaledHeigth); canvas.drawBitmap(bmp, image, drawPaint); canvas.save(); canvas.restore(); } private void drawMarkers(Canvas canvas) { Paint drawPaint = new Paint(); drawPaint.setAntiAlias(true); drawPaint.setColor(Color.WHITE); canvas.drawCircle(screen_center.x, screen_center.y, MARKER_DIAMETER + 1, drawPaint); drawPaint.setColor(Color.RED); canvas.drawCircle(screen_center.x, screen_center.y, MARKER_DIAMETER, drawPaint); canvas.save(); canvas.restore(); } private void setScreenCenter() { if (screen_center != null) return; // recupero la dimensione dello schermo int screen_height = getMeasuredHeight(); int screen_width = getMeasuredWidth(); if ((screen_height == 0) || (screen_width == 0)) return; screen_center = new PointF(screen_width / 2, screen_height / 2); setInitialZoom(); } @Override public void draw(Canvas canvas) { setScreenCenter(); if (selected_floor != null) { prepareImage(canvas); drawMarkers(canvas); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int measuredWidth = measure(widthMeasureSpec); int measuredHeight = measure(heightMeasureSpec); setMeasuredDimension(measuredWidth, measuredHeight); } private int measure(int measureSpec) { int result = 0; // Decode the measurement specifications. int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.UNSPECIFIED) { // Return a default size of 200 if no bounds are specified. result = 200; } else { // As you want to fill the available space // always return the full available bounds. result = specSize; } return result; } public void setBearing(float _bearing) { bearing = _bearing; } } </code></pre> <p>And the touch listener, I've put it in another class. MapMovement:</p> <pre><code> public class MapMovement implements OnTouchListener { ... // We can be in one of these 3 states private static final int NONE = 0; private static final int DRAG = 1; private static final int ZOOM = 2; private int mode = NONE; public MapMovement(MapView mapView) { this.mapView = mapView; mapView.setOnTouchListener(this); } public void setImageCenter(PointF p) { this.image_center = p; } 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 &amp; 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 &gt;&gt; MotionEvent.ACTION_POINTER_ID_SHIFT); sb.append(")" ); } sb.append("[" ); for (int i = 0; i &lt; 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 &lt; event.getPointerCount()) sb.append(";" ); } sb.append("]" ); Log.d("event", sb.toString()); } @Override public boolean onTouch(View v, MotionEvent event) { // Dump touch event to log dumpEvent(event); return false; } } </code></pre> <p>And this is the output log when I try to do a "DRAG" on my MapView:</p> <pre><code> 12-24 09:41:58.412: D/event(31342): event ACTION_DOWN[#0(pid 0)=85,248] 12-24 09:42:01.752: D/event(31342): event ACTION_DOWN[#0(pid 0)=106,126] 12-24 09:42:02.466: D/event(31342): event ACTION_DOWN[#0(pid 0)=96,286] </code></pre> <p>The log code is the same of the test project I downloaded, and this is all the output I can get. No ACTION_UP, no ACTION_MOVEMENT, nothing.</p> <p>I don't know which is the problem or where it is. All that I want is to move the image in the activity using the touch events, but it seems that I can get only one touch event, ACTION_DOWN.</p> <p>I've posted all the parts of code where I think it could be the error. If I would have thought that the problem was certainly in one class or layout, I will have posted it and not this bunch of code for which I've lost 15 minutes to edit for you.</p> <p>Really thank you for the help. If you don't know which is the error is not a problem. But you can't come here and tell me to be "more specific", because if I could be "more specific" I will not be here to ask to you because I will have solved the problem by myself.</p>
 

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