0
- 定製
View
有圈,以及在 MainActivity
我顯示自定義View
,它的工作原理。我可以看到這個圈子。
我已經實現在View
的OnGestureListener
,但它不被識別。所有的手勢方法都不會被調用。
我在做什麼錯?
代碼:
MainActivity
:
package com.example.testpath;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rlayout = (RelativeLayout) findViewById(getResourceId("main","id"));
WorldMap worldmap = new WorldMap(this);
rlayout.addView(worldmap);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public int getResourceId(String pVariableName, String pResourcename)
{
try {
return getResources().getIdentifier(pVariableName, pResourcename, getPackageName());
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}
定製View
:
package com.example.testpath;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
public class WorldMap extends View implements OnGestureListener{
// private GestureDetector gestureScanner;
static float translatex = -2200;
static float translatey = -1500;
public WorldMap(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
canvas.translate(translatex,translatey);
paint.setColor(Color.RED);
canvas.drawCircle(100, 100, 50, paint);
paint.setColor(Color.BLUE);
canvas.drawCircle(2290, 1590, 50, paint);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Log.v("DEBUG TEST", "onDown ");
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.v("DEBUG TEST", "onShowPress ");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
Log.v("DEBUG TEST", "onSingleTapUp ");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
float vx = e1.getRawX();
float vy = e1.getRawY();
Log.v("DEBUG TEST", "onScroll : " + vx + " " + vy);
translatex=translatex+vx;
translatey=translatey+vy;
invalidate();
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Log.v("DEBUG TEST", "onLongPress ");
// TODO Auto-generated method stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// TODO Auto-generated method stub
Log.v("DEBUG TEST", "onFling ");
return false;
}
}