首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onTouch与OnDraw in SurfaceView

onTouch与OnDraw in SurfaceView
EN

Stack Overflow用户
提问于 2013-08-08 12:09:36
回答 1查看 2.1K关注 0票数 1

我正试图根据我在屏幕上的痕迹来画。在这个过程中,我的onTouch和OnDraw没有被执行。你能帮我更正一下这段代码吗?

Xml

代码语言:javascript
复制
<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>

MainActivity

代码语言:javascript
复制
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);
}

}

PathDraw

代码语言:javascript
复制
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

}

}
EN

回答 1

Stack Overflow用户

发布于 2013-11-05 08:14:02

OnTouch不称为

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

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

代码语言:javascript
复制
public class PathDraw extends SurfaceView implements OnTouchListener, SurfaceHolder.Callback{
    ...    
    public PathDraw(Context context){
        super(context);
        setOnTouchListener(PathDraw.this);  
    }

OnDraw不称为

这篇文章可能会有帮助:

Android Custom Layout - onDraw() never gets called

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18125672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档