java - 如何在 DragListener 中获取画布项目以便在其中绘制?
问题描述
如果将项目放到画布上,我正在尝试在画布上绘制一些形状:
public class MainActivity extends AppCompatActivity {
private String selectedItem=null;
private Button circle=null;
private Button rectangle=null;
private Button triangle=null;
private MyTouchListener touch= new MyTouchListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rectangle=(Button)findViewById(R.id.rectangle);
rectangle.setOnTouchListener(touch);
rectangle.setOnDragListener(new MyDragListener());
circle=(Button)findViewById(R.id.circle);
circle.setOnTouchListener(touch);
circle.setOnDragListener(new MyDragListener());
triangle=(Button)findViewById(R.id.triangle);
triangle.setOnTouchListener(touch);
triangle.setOnDragListener(new com.example.dragtest.MainActivity.MyDragListener());
}
private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
// selectedButton
final int id = view.getId();
switch(id){
case R.id.circle:
selectedItem="circle";
break;
case R.id.rectangle:
selectedItem="rectangle";
break;
case R.id.triangle:
selectedItem="triangle";
break;
}
return true;
}
return false;
}
}
private final class MyDragListener implements View.OnDragListener {
public boolean onDrag(View v, DragEvent event) {
final int id = v.getId();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
if(id != R.id.dropzone || !(v instanceof Canvas)){
return true;
}
final float x = v.getX();
final float y = v.getY();
final Canvas c = (Canvas)v;
switch(selectedItem){
case "rectangle":
Paint p = new Paint();
p. setARGB(0, 255, 0, 0);
c.drawRect(x,y,x+5,y+5,p);
break;
case "circle":
Paint p = new Paint();
p. setARGB(0, 255, 0, 128);
c.drawCircle(x,y,100,p);
break;
case "triangle":
Paint p = new Paint();
p. setARGB(0, 255, 144, 128);
Point a = new Point(x,y);
Point b = new Point(x-5, y+5);
Point c = new Point(x+5, y+5);
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.lineTo(a.x, a.y);
path.close();
c.drawPath(path, p);
break;
}
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
}
但在第 86 行:
final Canvas c = (Canvas)v;
我收到以下错误:
error: incompatible types: View cannot be converted to Canvas
据我所知,视图获取事件的来源,如果项目被删除,我想执行一些代码。
那么我如何检测物品是否被丢弃:
- 掉落在哪里
- 在画布内的什么位置被丢弃
实际上我想检查按钮是否放在画布内。你知道我该怎么做吗?
解决方案
推荐阅读
- android - 为什么,如果 messageField 为空,那么代码仍然会被执行?
- java - 如何在 Android 中混合两个音频文件然后保存
- docker - Gitlab runner 不打印 docker run 命令的输出
- python-2.7 - GAE 标准 python 2.7 运行时何时消失?
- python - 如何遍历熊猫中的一列并填充另一列
- r - 插入新行时,反应值重置为 NA rhandsontable 闪亮
- bash - 如何在没有 Brew 的情况下重新创建 Brew 别名?
- ubuntu - 在 VR 和 Ubuntu 服务器上使用 Bullet3 Pyhsics Simulator
- c++ - 防止重载方法上的 PLT 断点
- xcode - 如何更改 xcode 项目的 firebase 数据库?