How to share images on canvas in Android?

I displayed images from a resource in an application as rows and columns randomly. Of these rows and columns, I would like to swap two images when the user clicks only on the images. The following code will display images in rows and columns as randomly.

   private void rand(int imagesList[][])
{
     Random generator = new Random();
     int temp;
    for (int i = 0; i < MAX_ROWS; i++)
        for(int j = 0; j < MAX_COLS; j++)
        {
          int randRowPos = generator.nextInt(MAX_ROWS);
          int randColPos = generator.nextInt(MAX_COLS); 
          temp = imagesList[i][j];
          imagesList[i][j] = imagesList[randRowPos][randColPos];
          imagesList[randRowPos][randColPos]= temp;
      }
}

Using the code above, I displayed the images as rows and columns.

Here, how can I swap two next to images from rows and columns?

please, any body will help me .....

+5
source share
3 answers

, . ? , ? , ?

:

, . :

:

    public class PlayScreen extends Activity implements OnTouchListener

    private Panel mainPanel; // Panel for out display
    boolean firstClick = false; 

OnCreate:

main = new Panel(this);
// Display the panel (calls the ondraw and updates the display)
setContentView(main,new ViewGroup.LayoutParams(screenwidth,screenheight));
// Listen for touchevents on the panel
main.setOnTouchListener(this);

:

class Panel extends View
    {
        /*
         * Init a Panel to draw on and a paint to paint with
         */
        Paint mBitmapPaint;
        public Panel(Context context)
        {
            super(context);
            mBitmapPaint = new Paint();
        }

        @Override
        protected void onDraw(Canvas canvas)
        {
        drawImages(canvas);
        }
    }

drawImages:

private void drawImages(Canvas canvas) 
{

        for(int i = 0; i<MAX_ROWS; i++){
            for(int j=0; j<MAX_COLS; j++)
            {
        int xpos = j*bmp.getWidth()+j*2;
        int ypos = i*bmp.getHeight()+i*2;
            bmp = BitmapFactory.decodeResource(mContext.getResources(), items[i][j],opts);
                canvas.drawBitmap(bmp,xpos,ypos,mBitmapPaint);
        clickzonex.add(xpos);
        clickzoney.add(ypos);
        clickzonei.add(i);
        clickZonej.add(j);

            }
        }

}

OnTouch:

onTouch(View v, MotionEvent event) :

if (event.getAction() == MotionEvent.ACTION_DOWN)
{
// imply logic 
x = (int) event.getX(); 
y = (int) event.getY(); 

    for(int i = 0; i < clickzonex.size();i++)
    {
    if((x>clickzonex[i]) && (x<(clickzonex[i]+ bmp.getwidth())) && (y>(clickzoney[i])) && (y<(clickzoney[i]+bmp.getHeight())))
    {
    // we have a click in a zone so we get the card-number in that zone
    if(firstClick == false)
    {
    itemAti=clickzonei[i];
    itemAtj = clickzonej[i];
    firstclick = false;
    }
    else
    {   
    FirstItemToSwap = items[clickzonei[i]][clickzonej[i]];
    SecondItemToSwap = items[itemAti][itemAtj];

  // Now do the swaping using any algo you like.

    main.postInvalidate(); 
    firstclick = true;
    }
    break;
    }
    }
return true;
}
else
{
return false;
}

, . , ondraw drawcanvas, [] [] postinvalidate Panel.

+2

- . (invalidate()) .

void swap(int x1, int y1, int x2, int y2) {
    // swap items[x1][y1] and items[x2][y2]
    ........
    invalidate();
}
+1

, , , , .

ViewAnimator ImageView.

<ViewAnimator 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/slideshow_animator"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

, , , ViewAnimator View ( ImageView), . http://developer.android.com/reference/android/widget/ViewAnimator.html

0

All Articles