如何在android中交换View上的位图图像?

发布于 2024-12-24 19:02:41 字数 2120 浏览 2 评论 0原文

我正在使用小型应用程序在 Android 屏幕上显示气泡图像。我已经显示了资源目录中的所有气泡图像。我在视图类中实现了如下代码。

onDraw方法:

    @Override
protected void onDraw(Canvas canvas) 
{
    super.dispatchDraw(canvas);


    drawImages(canvas);

}

我已经实现了drawImages()方法,如下所示:

BitmapFactory.Options opts = new BitmapFactory.Options();   
private void drawImages(Canvas canvas) 
{

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

}

通过使用上述方法,我从items[i][j]中绘制了图像。 我已经实现了initialize()重写方法,如下所示:

 @Override
protected void initialize() 
{

    for(int i=0;i<MAX_ROWS;i++)
        for(int j=0;j<MAX_COLS;j++)
        {
            items[i][j] = ImagesStore.ImageList.get(list_Indx);
            list_Indx++;
            if(list_Indx == ImagesStore.ImageList.size())
                list_Indx = 0;

    }
    opts.inSampleSize = 4;
    width  = getWidth();
    height = getHeight();

}

通过使用上面的代码,我已经在模拟器屏幕上显示了所有气泡图像。

在这里我想交换用户同时选择两个气泡的气泡。

我已经实现了 onTouchEvent 方法来检查屏幕上的位图图像项目位置,如下所示:

               int x = 0;
               int y = 0;
               int tx = 0, ty = 0;   
           @Override
public boolean onTouchEvent(MotionEvent event) 
{

     tx = (int)event.getX();
     ty = (int)event.getY();
    int position;

        x=tx;
        y=ty;

        row = (y) / bmp.getHeight();
        col = x / bmp.getWidth();

        position=row*MAX_COLS+(col+1);

        Log.v("row", "=====>"+row);

        Log.v("col", "=====>"+col);

        Log.v("position", "=====>"+position);

    count++;

    if(count%4==0)
    {

    Log.v("count", "=====>"+count);


    //I would like to implement swap code here



    }   
    return true;
}

从上面的代码中我如何在 onTouchEvent() 上交换(相互更改图像)位图图像实现

请任何人帮助我...... …………

I am working with small application for display bubbles images on android screen.I have displayed all bubbles images from resource directory.I have implemented code as follows in view class.

onDraw method:

    @Override
protected void onDraw(Canvas canvas) 
{
    super.dispatchDraw(canvas);


    drawImages(canvas);

}

I have implemented drawImages() method as follows:

BitmapFactory.Options opts = new BitmapFactory.Options();   
private void drawImages(Canvas canvas) 
{

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

}

By using the above method i have drawn images from items[i][j].
I have implemented the initialize() override method as follows:

 @Override
protected void initialize() 
{

    for(int i=0;i<MAX_ROWS;i++)
        for(int j=0;j<MAX_COLS;j++)
        {
            items[i][j] = ImagesStore.ImageList.get(list_Indx);
            list_Indx++;
            if(list_Indx == ImagesStore.ImageList.size())
                list_Indx = 0;

    }
    opts.inSampleSize = 4;
    width  = getWidth();
    height = getHeight();

}

By using above code i have displayed all the bubble images on my emulator screen.

Here i would like to swap the bubbles which selects two bubbles by the user simultaneously.

I have implemented onTouchEvent method for check the bitmap image item position on screen as follows:

               int x = 0;
               int y = 0;
               int tx = 0, ty = 0;   
           @Override
public boolean onTouchEvent(MotionEvent event) 
{

     tx = (int)event.getX();
     ty = (int)event.getY();
    int position;

        x=tx;
        y=ty;

        row = (y) / bmp.getHeight();
        col = x / bmp.getWidth();

        position=row*MAX_COLS+(col+1);

        Log.v("row", "=====>"+row);

        Log.v("col", "=====>"+col);

        Log.v("position", "=====>"+position);

    count++;

    if(count%4==0)
    {

    Log.v("count", "=====>"+count);


    //I would like to implement swap code here



    }   
    return true;
}

From the above code How can i swap(inter change the images) bitmap images implementation at onTouchEvent()

Please any body help me..............

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随波逐流 2024-12-31 19:02:41

请将您想要的功能定义为..

“在这里我想交换用户同时选择两个气泡的气泡。”

不是一个完整的句子。

Please define the functionality that you desire as..

"Here i would like to swap the bubbles which selects two bubbles by the user simultaneously."

is not a complete sentence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文