私はアニメーションとジェスチャーを使用してアプリを作成しようとしています。これまでのところ、私はジェスチャーを取得しており、ジェスチャーに応じてイメージビューを作成して移動させることができます。クリックが発生した座標に基づいてイベントを実行できますか?
私がしたいことは、クリックが発生した場所に基づいて特定の画像を表示することです。たとえば、私のレイアウト全体は単なるフィールドの絵です。写真の上部1/4は山です。誰かがその部分をクリックすると、フライング・ドッグ・ピクチャが発生し、フィールドでクリックが発生すると、実行中のドッグ・ピクチャが発生します。座標などの組み込みに関するアドバイスはありますか?
public class FieldActivity extends AppCompatActivity {
public static final String DEBUG_TAG = FieldActivity.class.getSimpleName();
public TextView mGestureText;
public ImageView mCorgiFly;
final Handler handler = new Handler();
public ImageView mGoldenRun;
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_field);
mGestureText = (TextView) findViewById(R.id.text_view);
// Create an object of our Custom Gesture Detector Class
CustomGestureDetector customGestureDetector = new CustomGestureDetector();
// Create a GestureDetector
mGestureDetector = new GestureDetector(this, customGestureDetector);
// Attach listeners that'll be called for double-tap and related gestures
mGestureDetector.setOnDoubleTapListener(customGestureDetector);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.slideshowopenermenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.menu_item_memories:
Intent i = new Intent(this, GalleryActivity.class);
startActivity(i);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
class CustomGestureDetector implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
mGestureText.setText("onSingleTapConfirmed");
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
mCorgiFly = (ImageView)findViewById(R.id.superman);
mCorgiFly.setImageResource(R.drawable.flyingcorgi);
// handler.postDelayed(new Runnable() {
// @Override
// public void run() {
// mCorgiFly.setImageDrawable(null);
//}
//}, 500);
TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
0.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(1000); // animation duration
// animation.setRepeatCount(5); // animation repeat count
//animation.setRepeatMode(2); // repeat animation (left to right, right to left)
//animation.setFillAfter(true);
mCorgiFly.startAnimation(animation); // start animation
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
mGestureText.setText("onDoubleTapEvent");
return true;
}
@Override
public boolean onDown(MotionEvent e) {
mGestureText.setText("onDown");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
mGestureText.setText("onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
mGestureText.setText("onSingleTapUp");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
mGestureText.setText("onScroll");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
mGestureText.setText("onLongPress");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
mGestureText.setText("onFling " + e1.getX() + " - " + e2.getX());
if (e1.getX() < e2.getX()) {
Log.d(DEBUG_TAG, "Left to Right swipe performed");
}
if (e1.getX() > e2.getX()) {
Log.d(DEBUG_TAG, "Right to Left swipe performed");
}
if (e1.getY() < e2.getY()) {
Log.d(DEBUG_TAG, "Up to Down swipe performed");
}
if (e1.getY() > e2.getY()) {
Log.d(DEBUG_TAG, "Down to Up swipe performed");
}
return true;
}
}
}
はImageViewのにタッチリスナーを追加し、あなたがして反応することができ、 (通常のロング・ダブルなど)、x/y座標を得ることができます。 –
@ChristianStengel最初のステップは、どのようなx/y座標を知ることです。私は飛行をしなければなりません。私はそれをどのように把握していますか? – Kiran