本文實例講述了Android實現讓圖片在屏幕上任意移動的方法。分享給大家供大家參考,具體如下:
public class DragExampleActivity extends Activity { Bitmap mBitmap; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); setContentView(new DragView(this)); } @Override protected void onDestroy() { super.onDestroy(); if(null != mBitmap) { mBitmap.recycle(); mBitmap = null; } } private class DragView extends View { private int mMotionX = 0; private int mMotionY = 0; private Paint paint; public DragView(Context context) { super(context); paint = new Paint(); } @Override public void draw(Canvas canvas) { super.draw(canvas); canvas.drawBitmap(mBitmap, mMotionX, mMotionY, paint); } @Override public boolean onTouchEvent(MotionEvent ev) { if(ev.getAction() == MotionEvent.ACTION_DOWN) { mMotionX = (int) ev.getX(); mMotionY = (int) ev.getY(); invalidate(); return true; }else { return super.onTouchEvent(ev); } } }}更多關于Android相關內容感興趣的讀者可查看本站專題:《Android拍照與圖片處理技巧總結》、《Android編程之activity操作技巧總結》、《Android視圖View技巧總結》、《Android操作SQLite數據庫技巧總結》、《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android文件操作技巧匯總》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答