Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. super.onDraw(canvas);
  4. canvas.drawBitmap(mBarn, mTouchX, mTouchY, mPaint);
  5. invalidate();
  6. }
  7.  
  8. public boolean onTouchEvent(MotionEvent event) {
  9. int eventAction = event.getAction();
  10.  
  11. switch (eventAction) {
  12. case MotionEvent.ACTION_DOWN:
  13. mTouchX = (int)event.getX();
  14. mTouchY = (int)event.getY();
  15. break;
  16.  
  17. case MotionEvent.ACTION_MOVE:
  18. break;
  19.  
  20. case MotionEvent.ACTION_UP:
  21. break;
  22. }
  23. invalidate();
  24. return true;
  25.  
  26. package com.example.graphics1;
  27.  
  28.  
  29. import android.os.Bundle;
  30. import android.app.Activity;
  31. import android.content.Context;
  32. import android.graphics.Bitmap;
  33. import android.graphics.BitmapFactory;
  34. import android.graphics.Canvas;
  35. import android.view.Menu;
  36. import android.view.MotionEvent;
  37. import android.view.SurfaceHolder;
  38. import android.view.SurfaceView;
  39. import android.view.View;
  40. import android.view.View.OnTouchListener;
  41.  
  42. public class MainActivity extends Activity implements OnTouchListener{
  43. MyBringBack ourView;
  44. float x,y;
  45.  
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. ourView = new MyBringBack(this);
  50. ourView.setOnTouchListener(this);
  51. x=0;
  52. y=0;
  53. setContentView(ourView);
  54. }
  55. public class MyBringBack extends SurfaceView implements Runnable
  56. {
  57. SurfaceHolder ourholder;
  58. Thread ourThread = null;
  59. boolean isrunning=true;
  60. public MyBringBack(Context context) {
  61. super(context);
  62. // TODO Auto-generated constructor stub
  63. ourholder = getHolder();
  64. ourThread = new Thread(this);
  65. ourThread.start();
  66. }
  67.  
  68. @Override
  69. public void run() {
  70. // TODO Auto-generated method stub
  71. while(isrunning)
  72. {
  73. if(!ourholder.getSurface().isValid())
  74. continue;
  75. Canvas canvas = ourholder.lockCanvas();
  76. canvas.drawRGB(255, 255, 255);
  77. if(x!=0 && y!=0)
  78. {
  79. Bitmap bitmap =
  80. BitmapFactory.decodeResource(getResources(), R.drawable.images);
  81. canvas.drawBitmap(bitmap, x-
  82. (bitmap.getWidth()/2), y-(bitmap.getHeight()/2), null);
  83. }
  84. ourholder.unlockCanvasAndPost(canvas);
  85. }
  86. }
  87.  
  88. }
  89.  
  90. @Override
  91. public boolean onCreateOptionsMenu(Menu menu) {
  92. // Inflate the menu; this adds items to the action bar if it is present.
  93. getMenuInflater().inflate(R.menu.activity_main, menu);
  94. return true;
  95. }
  96.  
  97. @Override
  98. public boolean onTouch(View arg0, MotionEvent arg1) {
  99. // TODO Auto-generated method stub
  100. x=arg1.getX();
  101. y=arg1.getY();
  102. return true;
  103. }
  104.  
  105. }
  106.  
  107. Use this code this will help you..
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement