我希望创建一个类似控制器的模拟器,以便在我的xml布局中移动我的角色(这看起来与PlayStation或Xbox控制器上的类似 - 圆形模拟)。
我的第一个计划是简单地使用图像视图并在图像视图上放置4个透明按钮以允许移动,但是这有点沉闷并且想要真正有创意,但是我很难找到任何那种文件并不确定从哪里开始。
基本上,我所追求的功能是一个看似模拟的图像(见下文),然后用户可以按下并移动动画,这将触发相关的位置移动。理想情况下,我也想拥有这个,这样用户只需点击模拟上的右侧即可自动动画,然后触发正确的动作。
我不确定这是否可行,我要求在我的xml布局中实现,或者如果更容易,可以通过编程方式创建。
答案 0 :(得分:7)
请参阅最后编辑
----------------------------------------------
你必须实现GestureDetector.OnGestureListener接口,并使用onDown,onScroll方法来检测手势,然后使用视图的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"
/>
为了检测不同的模拟事件,您可以使用所需的方法定义添加一个Listener,并使用AnalogView中的接口,然后当您想要为AnalogView实现一个行为时,您只需通过以下方法添加一个实现:为侦听器调用setter(就像Button的setOnClickListener一样)。这样,您将拥有一个完全封装的AnalogView,您可以在任何其他应用程序中重复使用。
-------------------------------------------------------------------------------
最后编辑:使用侦听器处理事件:
我使用AnalogueView嵌入了一个Listener,现在你可以像使用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");
}
});
}
}
结果:在文本字段中显示事件: