我画了一条从A点(始终为零)开始并在B点结束的行 canvas.drawLine(xStart,yStart,xEnd,yEnd);
但是B点是可拖动的,所以我想在拖动时将画布线xEnd和yEnd更新为B位置。问题在于,当我更新线路编码时,只需1行解释就会出现错误:
“致命信号11(SIGSEGV),代码1,故障地址0x10c in tid 5638(xample.launcher)”它说xample.launcher但我的packageName是com.example.launcher,我不知道问题是否存在
这是我的代码。
DrawView(B点)
import android.app.Activity;
import android.view.View;
public class DrawView {
String label;
int color, direction, x, y;
View view;
DrawPath path;
public DrawView(){
}
public DrawView(String label, int color, int direction, int x, int y, View view){
this.label = label;
this.color = color;
this.direction = direction;
this.x = x;
this.y = y;
this.view = view;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public View getView() {
return view;
}
public void setView(View view) {
this.view = view;
}
public DrawPath getPath() {
return path;
}
public void setPath(DrawPath path) {
this.path = path;
}
}
DrawPath(A点到B点之间的线)
package com.example.launcher;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
public class DrawPath extends View{
int color, x, y;
Canvas canvas;
Paint paint;
public DrawPath(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
this.canvas = canvas;
paint = new Paint();
paint.setColor(color);
paint.setStrokeWidth(10);
Log.e("THIS X, THIS Y", this.x + " " + this.y);
canvas.drawLine(0, 0, this.x, this.y, paint);
invalidate();
}
public void setColor(int color){
this.color = color;
}
public int getColor(){
return this.color;
}
public void setXValue(int x){
this.x = x;
if(canvas != null){
canvas.drawLine(0, 0, this.x, this.y, paint);
invalidate();
}
}
public float getXValue(){
return this.x;
}
public void setYValue(int y){
this.y = y;
if(canvas != null){
canvas.drawLine(0, 0, this.x, this.y, paint);
invalidate();
}
}
public float getYValue(){
return this.y;
}
}
最后是我的onTouch活动
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myView.getLayoutParams();
final int X = (int)event.getRawX();
final int Y = (int)event.getRawY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
xDelta = X - (draw.getX());
yDelta = Y - (draw.getY());
break;
case MotionEvent.ACTION_MOVE:
params.setMargins((X - xDelta), (Y - yDelta), 0, 0);
draw.getPath().setXValue((X - xDelta));
draw.getPath().setYValue((Y - yDelta));
myView.setLayoutParams(params);
break;
}
return true;
}