2012-03-25 98 views
0

在DrawingTheBall类制作一些动画的东西。在SUrfaceViewExample类的TouchEvent将detecetd .. 我的问题是我不能够MainActivity和SurtfaceViewExample .. 没有任何问题与DrawingTheBall链接。 主类:。...无法SurfaceViewExample 链接到MainActivity

package maddy.first; 
import android.app.Activity; 
import android.os.Bundle; 
public class Madhu1Activity extends Activity { 

/** Called when the activity is first created. */ 
    Drwwingtheball v; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
      v = new Drawingtheball(this); 
      setContentView(v); 
     } 
} 

CLASS SurfaceViewExample:

package maddy.first; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class SurfaceViewExample extends Activity implements OnTouchListener{ 

    OurView v; 
    Bitmap ball; 
    float x,y; 
     @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
      v=new OurView(this); 
      v.setOnTouchListener(this); 
    ball=BitmapFactory.decodeResource(getResources(),R.drawable.tennis_ball); 
      x = y = 0; 
      setContentView(v); 
     } 
    @Override 
    protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    v.resume(); 
    } 
public class OurView extends SurfaceView implements Runnable{ 
    Thread t; 

    SurfaceHolder holder; 

    boolean isItOk=false; 

    public OurView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
      holder=getHolder(); 
     } 


public void run() { 

    // TODO Auto-generated method stub 

    while(isItOk ==true) 
    { 
    //drawing 
    if(holder.getSurface().isValid()) { 

     continue; 

    } 

    Canvas c=holder.lockCanvas(); 
    c.drawARGB(255,150,150,10);  
    c.drawBitmap(ball, x-(ball.getWidth()), y-(ball.getHeight()), null); 

    holder.unlockCanvasAndPost(c);  

    } 
} 
public void pause() 
{ 
    isItOk=false; 
    while(true) { 
     try { 
      t.join(); 
     }catch(InterruptedException e) { 

      e.printStackTrace(); 

     } 
     break; 

    } 
} 

public void resume() 
{ 
    isItOk=true; 
    t=new Thread(this); 
    t.start(); 
} 

    } 
    public boolean onTouch(View v, MotionEvent event) { 
    // TODO Auto-generated method stub 
    return false; 
    } 


} 

类DrawingTheBall:

package maddy.first; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.view.View; 
public class DrawingTheBall extends View { 

    Bitmap bball; 
    int x,y; 
public DrawingTheBall(Context context) { 
     super(context); 

    bball=BitmapFactory.decodeResource(getResources() ,R.drawable.tennis_ball); 
     x = 0; 
     y = 0; 
     } 
protected void onDraw(Canvas canvas) 
    { 
super.onDraw(canvas); 
    Rect ourRect=new Rect(); 
    ourRect.contains(0, 0,canvas.getWidth(),canvas.getHeight()/2); 
    Paint blue=new Paint(); 
    blue.setColor(Color.RED); 
    blue.setStyle(Paint.Style.FILL); 
    canvas.drawRect(ourRect, blue); 
    if(x < canvas.getWidth()) 
    x+=10; 
    else 
     x=0; 
    if(y<canvas.getHeight()) 
    y+=10; 
    else 
     y=0; 
    Paint p=new Paint(); 
    canvas.drawBitmap(bball,x,y,p); 
    invalidate(); 
    } 


} 
} 

回答

0

你的问题不清楚。你想要链接什么? 你想从另一个人开始活动? 或者你想传递一个参数到另一个活动?

如果你想从主要业务运行SurfaceViewExample活动 - 这是代码 -

startActivity(new Intent(this, SurfaceViewExample.class) 
finish(); 

而且AndroidManifest.xml文件应该是 -

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name="Madhu1Activity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name="SurfaceViewExample"></activity> 
</application> 

如果你想传递参数从一个活动到另一个 - 这是代码 -

startActivity(new Intent(this, SurfaceViewExample.class) 
    .putExtra("key", [value])); // if you want to pass class the class should be Serializable, otherwise you can pass value like a hash map. 

//in other activity convert it to class- 
<classname> obj = (<classname>)getIntent().getSerializableExtra("key"); 

如果这还不够,那么告诉我究竟需要什么。

享受..

+0

我粘贴:startActivity(新意图(这一点,SurfaceViewExample.class) 完成();在主要业务,但它并没有worked.I要运行特别SurfaceViewExample class.please帮我@Suvam罗伊 – user1290992 2012-03-25 09:52:31

0

我认为它会引发一些例外,否则你的布局有一定的误差,所以它是无法设置内容视图。 这是保持SurfaceViewExample类作为主要活动遵循它的最简单的方法 - 转到应用程序的AndroidManifest.xml文件,然后 -

Application tab -> select launch activity(that is your main activity) -> 
Name*(right side) -> browse(take for a while to loading activity list) - > 
select SurfaceViewExample -> remove previous SurfaceViewExample activity if already added. 

在SurfaceViewExample的setContentView(layout.main)设置一个默认的布局; //如果存在 并运行你的应用程序来检查它是否能够显示你的布局。 如果它可以成功运行,那么请查看您的DrawingTheBall代码。 玩得开心......

+0

非常感谢Suvam :) – user1290992 2012-03-26 15:34:25