1

我正在尝试根据我在屏幕上的轨迹进行绘制。在这个过程中,我的 onTouch 和 OnDraw 没有被执行。你能帮我更正这段代码吗?

xml

<LinearLayout  
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:layout_weight="100"
android:orientation="vertical"
android:background="#ffff0000" >

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="60"
    android:gravity="center"  >   

        <com.example.test2.PathDraw
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="140dp" />

   </LinearLayout> 

   <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"        
    android:weightSum="40"
    android:gravity="center"  >   
           <ImageView 
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"    
           android:background="@drawable/lion" />
    </LinearLayout>

主要活动

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    PathDraw sineWaveSurfaceView = (PathDraw) findViewById(R.id.surfaceView);
}

}

路径绘制

public class PathDraw extends SurfaceView implements SurfaceHolder.Callback 
{
private Context context; 
//  List<Point> points;
  ArrayList<Point> points = new ArrayList<Point>();

public PathDraw(Context context)    {
    super(context);
    // TODO Auto-generated constructor stub  
}

public PathDraw(Context context, AttributeSet attrs)    {
    super(context, attrs);
    // TODO Auto-generated constructor stub 
    this.context = context;
    getHolder().addCallback(this); 
}

public PathDraw(Context context, AttributeSet attrs, int defStyle)  {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub 
}

protected void OnDraw(Canvas canvas, int value) {  
    Paint paint = new Paint();
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(2);
    paint.setColor(Color.WHITE);

    Path path = new Path();
    boolean first = true;
    for(Point point : points){
        if(first){
            first = false;
            path.moveTo(point.x, point.y);
        }
        else{
            path.lineTo(point.x, point.y);
        }
    } 
    canvas.drawPath(path, paint);  
}

public boolean onTouch(View view, MotionEvent event) {
    // if(event.getAction() != MotionEvent.ACTION_DOWN)
    // return super.onTouchEvent(event);
    Point point = new Point();
    point.x = event.getX();
    point.y = event.getY();
    points.add(point);
    invalidate();
    Log.d("PathDraw", "point: " + point);
    return true;
}


@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)    {
    // TODO Auto-generated method stub 
}

@Override
public void surfaceCreated(SurfaceHolder arg0)  {
    // TODO Auto-generated method stub  
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0)    {
    // TODO Auto-generated method stub

}

}
4

1 回答 1

0

不调用 OnTouch

我今天早些时候也遇到了完全相同的问题。

类 PathDraw 应该实现 OnTouchListener,然后将侦听器设置为自身,执行以下操作:

public class PathDraw extends SurfaceView implements OnTouchListener, SurfaceHolder.Callback{
    ...    
    public PathDraw(Context context){
        super(context);
        setOnTouchListener(PathDraw.this);  
    }

不调用 OnDraw

这篇文章可能会有所帮助:

Android 自定义布局 - onDraw() 永远不会被调用

于 2013-11-05T08:14:02.127 回答