Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 1.59 KB  |  hits: 31  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to swap the bitmap images on View in android?
  2. @Override
  3. protected void onDraw(Canvas canvas)
  4. {
  5.     super.dispatchDraw(canvas);
  6.  
  7.  
  8.     drawImages(canvas);
  9.  
  10. }
  11.        
  12. BitmapFactory.Options opts = new BitmapFactory.Options();  
  13. private void drawImages(Canvas canvas)
  14. {
  15.  
  16.         for(int i = 0; i<MAX_ROWS; i++){
  17.             for(int j=0; j<MAX_COLS; j++)
  18.             {
  19.                 bmp = BitmapFactory.decodeResource(mContext.getResources(), items[i][j],opts);
  20.                     canvas.drawBitmap(bmp,j*bmp.getWidth()+j*2,i*bmp.getHeight()+i*2,mBitmapPaint);
  21.             }
  22.         }
  23.  
  24. }
  25.        
  26. @Override
  27. protected void initialize()
  28. {
  29.  
  30.     for(int i=0;i<MAX_ROWS;i++)
  31.         for(int j=0;j<MAX_COLS;j++)
  32.         {
  33.             items[i][j] = ImagesStore.ImageList.get(list_Indx);
  34.             list_Indx++;
  35.             if(list_Indx == ImagesStore.ImageList.size())
  36.                 list_Indx = 0;
  37.  
  38.     }
  39.     opts.inSampleSize = 4;
  40.     width  = getWidth();
  41.     height = getHeight();
  42.  
  43. }
  44.        
  45. int x = 0;
  46.                int y = 0;
  47.                int tx = 0, ty = 0;  
  48.            @Override
  49. public boolean onTouchEvent(MotionEvent event)
  50. {
  51.  
  52.      tx = (int)event.getX();
  53.      ty = (int)event.getY();
  54.     int position;
  55.  
  56.         x=tx;
  57.         y=ty;
  58.  
  59.         row = (y) / bmp.getHeight();
  60.         col = x / bmp.getWidth();
  61.  
  62.         position=row*MAX_COLS+(col+1);
  63.  
  64.         Log.v("row", "=====>"+row);
  65.  
  66.         Log.v("col", "=====>"+col);
  67.  
  68.         Log.v("position", "=====>"+position);
  69.  
  70.     count++;
  71.  
  72.     if(count%4==0)
  73.     {
  74.  
  75.     Log.v("count", "=====>"+count);
  76.  
  77.  
  78.     //I would like to implement swap code here
  79.  
  80.  
  81.  
  82.     }  
  83.     return true;
  84. }