Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我的手指触摸代码在一个特定的布局中绘制?_Android_Android Layout_Android Canvas - Fatal编程技术网

Android 我的手指触摸代码在一个特定的布局中绘制?

Android 我的手指触摸代码在一个特定的布局中绘制?,android,android-layout,android-canvas,Android,Android Layout,Android Canvas,我想随着手指的移动在布局上画线。有一个带有背景图像的父布局和一个id为“relayForDraw”的相对布局的子布局,该布局有一个图像视图。我想,当我的手指在子布局上移动时,然后在上面画线。我应用了一些代码,但它不起作用 代码在这里 public class MainActivity extends Activity implements OnTouchListener { RelativeLayout forDraw; ImageView imgV; Bitmap bitmap; Canva

我想随着手指的移动在布局上画线。有一个带有背景图像的父布局和一个id为“relayForDraw”的相对布局的子布局,该布局有一个图像视图。我想,当我的手指在子布局上移动时,然后在上面画线。我应用了一些代码,但它不起作用 代码在这里

public class MainActivity extends Activity implements OnTouchListener {

RelativeLayout forDraw;
ImageView imgV;
Bitmap bitmap;
Canvas canvas;
Paint paint;

float downx = 0, downy = 0, upx = 0, upy = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    forDraw = (RelativeLayout) findViewById(R.id.relayForDraw);
    imgV = (ImageView) findViewById(R.id.imageView1);
    float dw = forDraw.getWidth();
    float dh = forDraw.getHeight();
    bitmap = Bitmap.createBitmap((int) dw, (int) dh,
            Bitmap.Config.ARGB_8888);
    canvas = new Canvas();
    paint = new Paint();
    paint.setColor(Color.GREEN);
    imgV.setImageBitmap(bitmap);
    imgV.setOnTouchListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        downx = event.getX();
        downy = event.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        upx = event.getX();
        upy = event.getY();
        canvas.drawLine(downx, downy, upx, upy, paint);
        imgV.invalidate();
        downx = upx;
        downy = upy;
        break;
    case MotionEvent.ACTION_UP:
        upx = event.getX();
        upy = event.getY();
        canvas.drawLine(downx, downy, upx, upy, paint);
        imgV.invalidate();
        break;
    case MotionEvent.ACTION_CANCEL:
        break;
    default:
        break;
    }
    return true;
}
}

我的xml文件在这里

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back1"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/relayForDraw"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="112dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@null" />
</RelativeLayout>

您正在“空”画布上绘图。
替换:

canvas = new Canvas();
与:

canvas = new Canvas(bitmap);

但它仍然抛出异常。我写的其他代码对吗?@user3652702什么异常?请编辑这个问题。
canvas = new Canvas(bitmap);