Comment déplacer une image sur la mise en page XML lorsque l'utilisateur sur l'événement tactile?
-
10-12-2019 - |
Question
J'ai mis en place une application avec image.Dans mon application, j'ai utilisé sur l'image lorsque jamais l'utilisateur touche à l'image, j'aimerais déplacer l'image avec son doigt Touch.J'ai mis en œuvre ma demande comme suit:
((ImageView)findViewById(R.id.imageView1)).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
//I would like to Move image along with user finger touch code
break;
default:
break;
}
return false;
}
});
du code ci-dessus, je ne suis pas capable de déplacer l'image avec le doigt de l'utilisateur.
La solution
Il y a un échantillon pour atteindre ce voir:
http://blahti.wordpress.com/2011/ 01/17 / Déplacement-Vues-Part-2 /
Autres conseils
juste une suggestion, cela fonctionne pour moi:
Rendez-vous FALSE à TRUE de la méthode AOUTU ()
J'utilise ce code pour atteindre ce
package mani.droid.touchdrag;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends Activity {
Bitmap img;
float x;
float y;
boolean isStarted = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
img = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
setContentView(new MyScreen(this));
}
public class MyScreen extends View {
Context context;
public MyScreen(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
float xdiff = Math.abs( x - event.getX());
float ydiff = Math.abs( y - event.getY());
if( xdiff < 23 || ydiff < 23 ){
isStarted = true;
}
break;
case MotionEvent.ACTION_MOVE:
if(isStarted)
{
x = event.getX() - img.getWidth()/2;
y = event.getY() - img.getHeight()/2;
Log.v("X:" + x, "Y: " + y);
this.invalidate();
}
break;
case MotionEvent.ACTION_UP:
isStarted = false;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(img, x, y, null);
}
}
}
Et sa fonctionne parfaitement ...
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow