pra*_*gai 5 android swap image rows
我已经将应用程序中的资源图像随机显示为行和列.从那些行和列我想在用户单击图像旁边时交换两个图像.以下代码将随机显示行和列中的图像.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
通过使用上面的代码,我已经将图像显示为行和列.
在这里我如何交换行和列旁边的两个图像?
请任何身体帮助我.....
我没有权限添加评论,所以我将其作为答案发布。除了图像之外什么意思?当用户单击一张图像时,是否应该将其与旁边的图像交换?您还可以分享将这些图像装箱以查看或任何适配器视图的代码吗?
编辑 :
在绝对布局还存在的时候,我也遇到过类似的情况。我所做的如下:
班级:
public class PlayScreen extends Activity implements OnTouchListener
private Panel mainPanel; // Panel for out display
boolean firstClick = false;
Run Code Online (Sandbox Code Playgroud)
创建时:
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);
Run Code Online (Sandbox Code Playgroud)
控制板 :
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);
}
}
Run Code Online (Sandbox Code Playgroud)
绘制图像:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
触摸时:
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;
}
Run Code Online (Sandbox Code Playgroud)
我只是尝试使用我自己的示例向您展示逻辑并将其与您的代码混合。要点是,在ondraw方法中只需调用drawcanvas,而在触摸时只需交换items[][]并调用Panel类的postinvalidate方法。
| 归档时间: |
|
| 查看次数: |
1698 次 |
| 最近记录: |