在android中开发一个像简单形状(方形和圆形)设计师的视图

时间:2013-12-25 23:19:15

标签: android drawing android-custom-view

您好我一直在苦苦挣扎这一段时间,试图建立一种设计师,我可以通过点击(或拖动它们)将一些形状放入其中,问题是我试图理解{ {3}}他们谈论视口,滚动和拖动来创建图表,然而我的目标是更像地图设计,如下所示:

enter image description here

到目前为止,我已经能够做到基本的,就像你触摸它时会绘制一个圆圈并且能够缩放,但我仍然不了解整个视口,内容和OverScroller组合。你能指出一些意见吗?有没有人这样做过?任何可以用简单的方式解释的教程或例子。

3 个答案:

答案 0 :(得分:0)

您可以使用此功能将方形位图更改为圆形位图

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);

答案 1 :(得分:0)

如果我正确理解了您的问题,您只需要自定义View即可在Canvas中绘制对象。您可以阅读更多相关信息here。一个简单的例子:

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>

现在您的View中有一个自定义Activity,您可以使用Canvas绘制所需内容,并在onTouch()事件中获取用户触摸坐标。阅读SO中的onTouch()herehere

您还可以阅读有关手势的here,基本上MotionEvent.ACTION_DOWN是您第一次触摸屏幕时,您可以获取绘制圆圈的初始点的坐标。然后MotionEvent.ACTION_MOVE就是当您拖动手指时,您可以获得坐标,这样您就可以拖动之前绘制的任何内容。拖动时使用motionEvent.getRawX()motionEvent.getRawY()代替motionEvent.getX()motionEvent.getY()进行平滑拖动。

现在,根据用户触摸屏幕的位置,您可以在该坐标中绘制任何想要的内容。要在图片中创建右侧面板,您可以在Activity布局中添加按钮,该按钮也会保留您的自定义View,因此Activity的左侧是自定义View在那里绘制所有内容,左侧是用于选择绘制内容的按钮。

答案 2 :(得分:0)

这是一种抽象。看到这样:
你的世界就是你正在绘制的地图 只有部分可见,视口内的部分是矩形 滚动时,您将视图端口移动到地图的其他部分,之前可能已经无法识别。
如果你想象用一盏小灯看世界地图,你只会看到被照亮的部分。
如果您想象将坐标系放在世界地图上,这意味着您只能看到(0,0) - (10,10)中的矩形,例如,如果您的镜头尺寸为10。 /> 移动镜头就像移动视口一样 所以你必须从你的真实地图中获得一个分辨率(可能比你的显示器尺寸更大),以显示的尺寸和滚动时可见的地图部分。
如果存储要绘制的图片/图像/圆的坐标,则可以轻松计算哪些对象可见以及显示的位置。
假设您的显示器尺寸为800x400,您的地图尺寸在内部为8000x4000,您的矩形为100X100,左下角为0,0,另一个为-200,0。 当视口位于0,0时,您可以看到第一个矩形 滚动到左侧时,您不会移动地图,而是移动视口 当视口移动到50,0时,您可以看到矩形的一半。 这种计算可以通过使用矩阵对矢量进行各种数学变换来完成,并且可以变得相当复杂但是众所周知 如果你看一下OpenGL ES教程,你可以看到更多关于视口概念的信息(它们类似于相机 - 对象)。
我希望,这可能会有所帮助。