你好,我一直在努力构建一种设计师,可以通过点击(或拖动)将一些形状放入其中。问题是,我试图理解他们谈论的触摸手势,它们涉及视口、滚动和拖动来创建图表,但我想要的是更像地图设计的东西,就像这样:
到目前为止,我已经能够做一些基本的东西,例如当你触摸屏幕时会绘制一个圆形并且可以缩放,但是我仍然不理解整个视口(viewport)、内容(content)和OverScroller的组合。您能给我一些建议吗?有人做过这个吗?有没有简单易懂的教程或示例可以解释这个问题。你好,我一直在努力构建一种设计师,可以通过点击(或拖动)将一些形状放入其中。问题是,我试图理解他们谈论的触摸手势,它们涉及视口、滚动和拖动来创建图表,但我想要的是更像地图设计的东西,就像这样:
到目前为止,我已经能够做一些基本的东西,例如当你触摸屏幕时会绘制一个圆形并且可以缩放,但是我仍然不理解整个视口(viewport)、内容(content)和OverScroller的组合。您能给我一些建议吗?有人做过这个吗?有没有简单易懂的教程或示例可以解释这个问题。public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
// TODO Auto-generated method stub
int targetWidth = 50;
int targetHeight = 50;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth),
((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}
还要看一下 Path
类的这些方法
path.addArc(oval, startAngle, sweepAngle);
path.addOval(oval, dir);
path.arcTo(oval, startAngle, sweepAngle);
View
来在Canvas
中绘制对象。您可以在这里阅读更多相关信息。以下是一个简单的示例:public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.your_custom_view);
}
}
public class MyView extends SurfaceView{
public MyView (Context context, AttributeSet attrs){
super(context, attrs, 0);
}
@Override
protected void onDraw(Canvas canvas) {
// Draw here
}
@Override
public boolean onTouch(MotionEvent event){
// Touch events here
return true;
}
}
在你的布局XML
中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.your.package.MyView
android:id="@+id/joystickView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<!-- Right side panel with buttons -->
<Button ... />
<Button ... />
...
</RelativeLayout>
现在您在Activity
中拥有自定义的View
,可以使用Canvas
绘制任何内容,并在onTouch()
事件中获取用户触摸坐标。在SO或这里和这里阅读有关onTouch()
的信息。
您还可以阅读这里关于手势的内容,基本上MotionEvent.ACTION_DOWN
是您第一次触摸屏幕时,您可以获取绘制圆形等初始点的坐标。然后MotionEvent.ACTION_MOVE
是当您拖动手指时,您可以获取坐标,以便您可以拖动之前绘制的任何内容。在拖动时,请使用motionEvent.getRawX()
和motionEvent.getRawY()
而不是motionEvent.getX()
和motionEvent.getY()
以获得平滑的拖动效果。
现在根据用户触摸屏幕的位置,您可以在该坐标中绘制任何您想要的内容。要创建像您的图像中的右侧面板,您可以在Activity
布局中添加按钮,该布局还包含您的自定义View
,因此您的Activity
左侧是自定义View
,您可以在其中绘制所有内容,而左侧则是按钮,用于选择要绘制的内容。