我想根据用户的触摸在网格视图中的两个单元格之间画一条线?但我需要它从第一个细胞的中心到最后一个细胞的中心。那么我怎样才能得到一个细胞的中心点(x,y)?
这是我的代码:
GridView lettersGrid;
GridAdapter ga;
StringBuilder word = new StringBuilder();
List<Integer> positions = new ArrayList<Integer>();
List<String> words;
Methods mthd;
int[] cellsColors;
int randomColor = 0;
Paint paint;
Canvas canvas;
Bitmap bitmap;
float downx = 0, downy = 0, upx = 0, upy = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lettersGrid = (GridView) findViewById(R.id.letters_grid);
ga = new GridAdapter(this);
mthd = new Methods();
cellsColors = new int[ga.getCount()];
paint = new Paint();
paint.setStrokeWidth(15);
Display currentDisplay = getWindowManager().getDefaultDisplay();
bitmap = Bitmap.createBitmap(currentDisplay.getWidth(), currentDisplay.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
lettersGrid.setAdapter(ga);
}//end onCreate
@Override
public boolean onTouch(View v, MotionEvent event) {
if(v.getId() == R.id.letters_grid){
int action = event.getActionMasked();
switch(action){
case MotionEvent.ACTION_DOWN:
downx = event.getX()+3;
downy = event.getY();
break;
case MotionEvent.ACTION_UP:
upx = event.getX();
upy = event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
break;
}
}
return false;
}
答案 0 :(得分:0)
它可以通过多种方式完成,没有一个女巫特别直截了当。
我可能最终实施的是在每个OnTouchListener
孩子上设置GridView
。
这样你就可以在ACTION_MOVE
事件发生的那一刻,毫不费力地知道触摸所在的细胞。
on ACTION DOWN
int centerX = (int) ( v.getX() + v.getMesuredWidth()/2d );
int centerY = (int) ( v.getY() + v.getMesuredHeight()/2d );
在ACTION_MOVE和ACTION_ENTER
上我很确定这可行,这些只是实施它的指导方针,但我想你可以做到。不太难。