如何在现有活动中绘制线条?

时间:2014-05-17 14:04:20

标签: java android graphics draw lines

事实上,我们想要编写一个Bridge Constructor Game代码,用户必须能够绘制这些代码来构建桥梁。

已经回答:我一直在尝试创建一个类,这样我就可以在现有布局上绘制线条,但是当我们尝试启动活动时应用程序崩溃了#34;游戏" (按主菜单中的按钮)。 LogCat告诉我们:"致命异常:主要"并且我们有一个NullPointerException。

[编辑]:我们现在搜索如何取回我们作为单个对象创建的所有行(我们假设画布创建了位图),但我们真的不喜欢#39;知道如何获取这些位图...

以下是活动的代码" CustomView" (用于绘制线条):

    package com.g70.buildmybridge;

import java.util.LinkedList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;

public class CustomView extends View {

    private Paint   mPaint;
    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;

    private LinkedList<Path> paths = new LinkedList<Path>();


        public CustomView (Context context) {
               super(context);
               setFocusable(true);
               setFocusableInTouchMode(true);

               mPath = new Path();
               mCanvas = new Canvas();
               mBitmapPaint = new Paint();
               mPaint = new Paint();
               mPaint.setColor(Color.BLACK);
               mPaint.setStyle(Paint.Style.STROKE);
               mPaint.setStrokeJoin(Paint.Join.BEVEL);
               mPaint.setStrokeCap(Paint.Cap.ROUND);
               mPaint.setStrokeWidth(6);
               paths.add(mPath);
           }

         @Override
           protected void onDraw(Canvas canvas) {
             for (Path p : paths){
                    canvas.drawPath(p, mPaint);
                }

        }

        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

     // à la touche posée
        private void touch_start(float x, float y) {

                x=Math.round(x/100)*100;
                y=Math.round(y/100)*100;
                mPath.reset();
                // deplacement du path et positionnement en x,y
                mPath.moveTo(x, y);
                mX = x;
                mY = y;

        }

        // a la touche levée
        private void touch_up(float x, float y) {
            float dX=(x-mX);
            float dY=(y-mY);
            if(dY>=0&dY<=50){
                y=mY;
            }
            if(dY>=50){
                y=mY+100;
            }
            if(dX>=0&dX<=50){
                x=mX;
            }
            if(dX>=50){
                x=mX+100;
            }
            if(dX<=0&dX>=-50){
                x=mX;
            }
            if(dY<=0&dY>=-50){
                y=mY;
            }
            if(dX<=-50){
                x=mX-100;
            }
            if(dY<=-50){
                y=mY-100;
            }
            mPath.lineTo(x, y);
            // dessin du path sur l'objet paint
            mCanvas.drawPath(mPath, mPaint);
            // reset du path pour ne pas avoir le chemin précédent
            mPath = new Path();
            paths.add(mPath);
            mPath.reset();
         }


        // lancement des methodes de touch de l'écran tactile du smartphone
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // prend l'axe des X
            float x = event.getX();
            // prend l'axe des Y
            float y = event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up(x,y);
                    invalidate();
                    break;
            }
            return true;
        }

        /**public void ChangeColor(String s){
            if(s=="Metal"){
                mPaint.setColor(Color.GRAY);
            }
            if(s=="Wood"){
                mPaint.setColor(Color.YELLOW);
            }
        }**/
}

这是游戏的代码:

package com.g70.buildmybridge;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.view.View;
import android.view.View.OnClickListener;

public class Game extends Activity {

    protected GridLayout DrawLayout;
    CustomView customview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game1);
        DrawLayout = (GridLayout) findViewById(R.id.game1);
        customview = new CustomView(this);
        DrawLayout.addView(customview);
        customview.requestFocus();


        final Button exit = (Button) findViewById(R.id.button4);
        exit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        Intent intent = new Intent(Game.this, MainActivity.class);
        startActivity(intent);
        }
      });

        /**final Button undo = (Button) findViewById(R.id.button5);
        undo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            CustomView.paths.pollLast();
        }
      });**/


    }  

}

提前谢谢!

1 个答案:

答案 0 :(得分:1)

这是Game / onCreate中的错误:

MainLayout = (LinearLayout) findViewById(R.layout.draw);

在这里你必须得到一个ID,而不是布局...
因此,在您的/ res / layout / game中,必须是一个ID =&#34; @ + id / draw&#34;

的LinearLayout

解决方案:将上述说明更改为:

MainLayout = (LinearLayout) findViewById(R.id.draw);

注意:它 findViewById ,而不是 findViewByLayout

<强> [编辑]

您永远不会在CustomView类中初始化路径对象。在构造函数中,在超级行之后执行此操作:

mPath = new Path();

[编辑2]

此外,还需要初始化这些对象:

private Paint mPaint;
private Bitmap  mBitmap;
private Canvas  mCanvas;
private Paint   mBitmapPaint;

然后开始使用新的绘图对象绘制,但是然后尝试使用mBitmapPaint和mPaint。

[编辑3]

我找到了一篇你可能会觉得非常有趣的帖子:Android How to draw a smooth line following your finger

[编辑4]

Finally it works, but we want now to get the Bitmap we created as objects (each line one by one !) to make them move ... We don't find anything on how to get them ! I will edit our code to see what we have done !

这应该是另一个问题... 1 Q =&gt; 1 A.
编辑问题并将其更改为另一个问题毫无意义(并且也增加了混淆) 它也可能使以前的答案无效......