请参阅最后的编辑
----------------------------------------------
您必须实现 GestureDetector.OnGestureListener 接口,并使用 onDown、onScroll 方法来检测手势,然后使用 view 的 onDraw(Canvas c) 方法,在修改其位置后重绘模拟的位图(并对其进行转换如果需要,可以使用矩阵)
您也可以在此答案中使用 onTouchEvent :
OnUp event on GestureDetector
---------------------------------------------------------------------------------------
编辑
这是一个尝试,不使用位图,只是 drawCircle 方法:
它不是太复杂,我写得很快,所以肯定有改进,
但它工作得很好:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class AnalogueView extends View {
float x,y;
double r,t;
int cx, cy,w,h;
final int RADIUS = 20;
Paint black = new Paint();
Paint grey = new Paint();
Paint white = new Paint();
private int toDo;
public AnalogueView(Context context, AttributeSet attrs) {
super(context, attrs);
black.setColor(Color.BLACK);
grey.setColor(Color.GRAY);
white.setColor(Color.WHITE);
black.setFlags(Paint.ANTI_ALIAS_FLAG);
white.setFlags(Paint.ANTI_ALIAS_FLAG);
grey.setFlags(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.w = w;
this.h = h;
cx = w/2;
cy = h/2;
x = cx;y=cy;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawMyStuff(canvas);
switch (toDo) {
case 1:
center();
break;
default:
break;
}
}
private void drawMyStuff(final Canvas canvas) {
canvas.drawCircle(cx, cy, w/2, black);
canvas.drawCircle(cx, cy, w/2-5, grey);
canvas.drawCircle(cx, cy, w/2-10, black);
canvas.drawCircle(x, y, RADIUS+2, white);
canvas.drawCircle(x, y, RADIUS, grey);
}
// n2p : normal to polar coordinates conversion
// p2n : polar to normal coordinates conversion
// R : distance to polar center
// T : polar angle
double n2pR(double x, double y){
return distance(x,y,cx,cy);
}
double n2pT(double x, double y){
return Math.atan2((y-cy),(x-cx));
}
double p2nX(double r, double t){
return r*Math.cos(t) + cx;
}
double p2nY(double r, double t){
return r*Math.sin(t) + cy;
}
double n2pR(){
return distance(x,y,cx,cy);
}
double n2pT(){
return Math.atan2((y-cy),(x-cx));
}
double p2nX(){
return r*Math.cos(t) + cx;
}
double p2nY(){
return r*Math.sin(t) + cy;
}
double distance(double x1 , double y1, double x2, double y2 ){
return Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN :
updatePosition(event);
break;
case MotionEvent.ACTION_MOVE :
updatePosition(event);
break;
case MotionEvent.ACTION_UP :
toDo = 1;
center();
break;
default :break;
}
return true;
}
private void center(){
if(r>15){
r-=15;
}else{
toDo=0;
r=0;
}
x = (float) p2nX();
y= (float) p2nY();
invalidate();
}
void updatePosition(MotionEvent e){
r= Math.min(w/2-RADIUS, n2pR(e.getX(),e.getY()));
t = n2pT(e.getX(),e.getY());
x = (float) p2nX();
y =(float) p2nY();
invalidate();
}
}
以下是如何将其添加到布局中:
<yourpackage.AnalogueView
android:id="@+id/analogueView1"
android:layout_width="80px"
android:layout_height="80px"
/>
为了检测不同的模拟事件,您可以使用所需的方法定义添加侦听器,并使用 AnalogView 中的接口,然后当您想要为 AnalogView 实现行为时,只需通过调用 setter 将实现添加到接口对于侦听器(就像按钮的 setOnClickListener 一样)。这样,您将拥有一个完全封装的 AnalogView,您可以在任何其他应用程序中重复使用它。
-------------------------------------------------------------------------------
最后编辑:使用侦听器处理事件:
我嵌入了一个带有 AnalogueView 的监听器,现在您可以像使用 OnClickListener 按钮一样使用它,
使用 setOnMoveListener 方法提供两种方法的实现:
onMaxMoveInDirection(double polar_angle_of_direction) and
onHalfMoveInDirection(double polar_angle_of_direction)
这是 AnalogueView 的新代码(没有大的修改,只是接口定义、moveListener 字段并触发 updatePosition 中的操作):
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class AnalogueView extends View {
OnMoveListener moveListener;
float x,y;
double r,t;
int cx, cy,w,h;
final int RADIUS = 20;
Paint black = new Paint();
Paint grey = new Paint();
Paint white = new Paint();
private int toDo;
public AnalogueView(Context context, AttributeSet attrs) {
super(context, attrs);
black.setColor(Color.BLACK);
grey.setColor(Color.GRAY);
white.setColor(Color.WHITE);
black.setFlags(Paint.ANTI_ALIAS_FLAG);
white.setFlags(Paint.ANTI_ALIAS_FLAG);
grey.setFlags(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.w = w;
this.h = h;
cx = w/2;
cy = h/2;
x = cx;y=cy;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawMyStuff(canvas);
switch (toDo) {
case 1:
center();
break;
default:
break;
}
}
private void drawMyStuff(final Canvas canvas) {
canvas.drawCircle(cx, cy, w/2, black);
canvas.drawCircle(cx, cy, w/2-5, grey);
canvas.drawCircle(cx, cy, w/2-10, black);
canvas.drawCircle(x, y, RADIUS+2, white);
canvas.drawCircle(x, y, RADIUS, grey);
}
// n2p : normal to polar coordinates conversion
// p2n : polar to normal coordinates conversion
// R : distance to polar center
// T : polar angle
double n2pR(double x, double y){
return distance(x,y,cx,cy);
}
double n2pT(double x, double y){
return Math.atan2((y-cy),(x-cx));
}
double p2nX(double r, double t){
return r*Math.cos(t) + cx;
}
double p2nY(double r, double t){
return r*Math.sin(t) + cy;
}
double n2pR(){
return distance(x,y,cx,cy);
}
double n2pT(){
return Math.atan2((y-cy),(x-cx));
}
double p2nX(){
return r*Math.cos(t) + cx;
}
double p2nY(){
return r*Math.sin(t) + cy;
}
double distance(double x1 , double y1, double x2, double y2 ){
return Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN :
updatePosition(event);
break;
case MotionEvent.ACTION_MOVE :
updatePosition(event);
break;
case MotionEvent.ACTION_UP :
toDo = 1;
center();
break;
default :break;
}
return true;
}
private void center(){
if(r>15){
r-=15;
}else{
toDo=0;
r=0;
}
x = (float) p2nX();
y= (float) p2nY();
invalidate();
}
void updatePosition(MotionEvent e){
r= Math.min(w/2-RADIUS, n2pR(e.getX(),e.getY()));
t = n2pT(e.getX(),e.getY());
x = (float) p2nX();
y =(float) p2nY();
if(moveListener != null)
if(r == w/2-RADIUS)
moveListener.onMaxMoveInDirection(t);
else if(r >= w/4-RADIUS/2)
moveListener.onHalfMoveInDirection(t);
invalidate();
}
public void setOnMoveListener(OnMoveListener listener){
moveListener =listener;
}
public interface OnMoveListener{
public void onHalfMoveInDirection(double polarAngle);
public void onMaxMoveInDirection(double polarAngle);
}
}
这里是如何提供一个实现:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.analog.AnalogueView.OnMoveListener;
public class MainActivity extends Activity {
AnalogueView analogue;
TextView showMoveEvent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showMoveEvent = (TextView) findViewById(R.id.showMoveEvent);
analogue = (AnalogueView) findViewById(R.id.analogueView1);
analogue.setOnMoveListener(new OnMoveListener() {
@Override
public void onMaxMoveInDirection(double polarAngle) {
showMoveEvent.setText("max move in "+polarAngle+" direction");
}
@Override
public void onHalfMoveInDirection(double polarAngle) {
showMoveEvent.setText("half move in "+polarAngle+" direction");
}
});
}
}
结果:在文本字段中显示事件: