android:在Canvas上放置bmp

时间:2014-04-07 13:31:49

标签: android canvas

我试图在第一次触摸屏幕时在Canvas上绘制BMP,但问题是BMP在我触摸屏幕的任何地方都会不断跳跃。我想要的是让BMP保持固定在第一次接触的位置,我不知道该怎么做。

@Override 
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBarn, mTouchX, mTouchY, mPaint);
invalidate();
}

public boolean onTouchEvent(MotionEvent event) {
int eventAction = event.getAction();   

switch (eventAction) {
    case MotionEvent.ACTION_DOWN: 
        mTouchX = (int)event.getX();
        mTouchY = (int)event.getY();                    
        break;

    case MotionEvent.ACTION_MOVE: 
        break;

    case MotionEvent.ACTION_UP:
        break;
} 
invalidate();
return true;      

}

1 个答案:

答案 0 :(得分:0)

  package com.example.graphics1;


 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.view.Menu;
 import android.view.MotionEvent;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;
 import android.view.View;
 import android.view.View.OnTouchListener;

 public class MainActivity extends Activity implements OnTouchListener{
MyBringBack ourView;
float x,y;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ourView = new MyBringBack(this);
    ourView.setOnTouchListener(this);
    x=0;
    y=0;
    setContentView(ourView);
}
    public class MyBringBack  extends SurfaceView implements Runnable
    {
        SurfaceHolder ourholder;
        Thread ourThread = null;
        boolean isrunning=true; 
        public MyBringBack(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            ourholder = getHolder();
            ourThread = new Thread(this);
            ourThread.start();
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while(isrunning)
            {
                if(!ourholder.getSurface().isValid())
                    continue;
                Canvas canvas = ourholder.lockCanvas();
                canvas.drawRGB(255, 255, 255);
                if(x!=0 && y!=0)
                {
                    Bitmap bitmap =  
        BitmapFactory.decodeResource(getResources(), R.drawable.images);
                    canvas.drawBitmap(bitmap, x-
        (bitmap.getWidth()/2), y-(bitmap.getHeight()/2), null);
                }
                ourholder.unlockCanvasAndPost(canvas);
            }
        }

    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
    // TODO Auto-generated method stub
    x=arg1.getX();
    y=arg1.getY();      
    return true;
}

    }

Use this code this will help you..