Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @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;
- 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..
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement