2012-03-25 9 views
0

にSurfaceViewExampleをリンクすることはできません.. 私の問題は、私はMainActivityとSurtfaceViewExampleをリンクすることはできませんよ..ですDrawingTheBallと 無問題。 。 MAIN CLASS:...いくつかのアニメーション作品を作るDrawingTheBallクラスでは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

iペースト:startActivity(新規インテント(this、SurfaceViewExample.class) finish();メインアクティビティでは動作しませんでした。実行したいです。SurfaceViewExampleクラスを実行します。help me @Suvam Roy – user1290992

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

関連する問題