Java 按位图的位置

Java 按位图的位置,java,android,bitmap,Java,Android,Bitmap,我正在制作一个应用程序,其中我希望对象向左、向右移动并跳跃(向上)。这是我现在的代码: package com.bjo.er; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.view

我正在制作一个应用程序,其中我希望对象向左、向右移动并跳跃(向上)。这是我现在的代码:

package com.bjo.er;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;


public class Play extends View  {
    int x=0;
    int y=0;
    Bitmap object;

    public Play(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        object = BitmapFactory.decodeResource(getResources(), R.drawable.brid);
   }
    @Override
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.drawColor(Color.GRAY);
        canvas.drawBitmap(object, x, y, null);
        invalidate();
    } 
}

我希望用户可以按屏幕上的三个不同位置移动对象。一些帮助将非常有用。

您可以使用RelativeLayout。使您的播放视图充满屏幕,并在播放视图上方放置3个按钮。然后,您只需为这些按钮注册ClickHandler并更改其中的x和y值

为了在布局中使用自定义播放视图,您需要添加另一个构造函数

public Play(Context context, AttributeSet attrs) {
    super(context, attrs);
}
布局文件可以如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.bjo.er.Play
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Button
        android:id="@+id/button_left"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="left"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />
    <Button
        android:id="@+id/button_right"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="right"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />
</RelativeLayout>

使您的视图实现onTouchListener,例如:
textView.setOnTouchListener(此)
其余工作将在onTouch中进行,例如:

float previousx,previousy;
int counter;

     public boolean onTouch(View v, MotionEvent event) {

     switch (event.getAction()) {


     case MotionEvent.ACTION_UP:
    float x =event.getX();
 float y =event.getY();   
 if(x==previousx && y==previousy)
Toast.makeText(this, "Touch a diffrent position",Toast.LENGTH_SHORT).show();
else{
counter+=1;
if(counter==3) {
//move your object.Put code here
counter=0;
}
}    
previousx=x;
previousy=y;
 break;
     }
     return true;
     }

我已经在另一个类中运行play类了,那么我会将xml文件导入到另一个文件中吗?
float previousx,previousy;
int counter;

     public boolean onTouch(View v, MotionEvent event) {

     switch (event.getAction()) {


     case MotionEvent.ACTION_UP:
    float x =event.getX();
 float y =event.getY();   
 if(x==previousx && y==previousy)
Toast.makeText(this, "Touch a diffrent position",Toast.LENGTH_SHORT).show();
else{
counter+=1;
if(counter==3) {
//move your object.Put code here
counter=0;
}
}    
previousx=x;
previousy=y;
 break;
     }
     return true;
     }