2015-03-19 2 views
0

Недавно я начал использовать холст поверх макета, потому что у меня были анимации в фоновом режиме. Однако я хочу начать новую работу после того, как пользователь нажмет кнопки. Вот код до сих пор ...Android: начальное намерение от onClick на холсте

public class GFX extends Activity implements View.OnTouchListener { 

MyBringBack ourSurfaceView; 
Paint title, play, options; 
float x, y, sX, sY, fX, fY, dX, dY, aniX, aniY, scaledX, scaledY, changingRedX, changingYellowX, changingPurpleX, changingGreenX, whereIsRedXY, whereIsYellowXY, whereIsGreenXY, whereIsPurpleXY; 
int whereIsRedY, redXThing, whereIsYellowY, whereIsGreenY, whereIsPurpleY, yellowXThing, greenXThing, purpleXThing, firstRun; 
Bitmap green, yellow, red, purple, plus, redFixed, yellowFixed, greenFixed, purpleFixed, titleTest, playTest, playFixed; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ourSurfaceView = new MyBringBack(this); 
    ourSurfaceView.setOnTouchListener(this); 
    x = 0; 
    y = 0; 
    sX = 0; 
    sY = 0; 
    fX = 0; 
    fY = 0; 
    Context ctx; 
    firstRun = 0; 

    dX = dY = aniX = aniY = scaledX = scaledY = redXThing = 0; 
    ctx = this; 
    green = BitmapFactory.decodeResource(getResources(), R.drawable.greenhairedpotatoblack); 
    yellow = BitmapFactory.decodeResource(getResources(), R.drawable.yellowhairedpotatoblack); 
    red = BitmapFactory.decodeResource(getResources(), R.drawable.redhairedpotatoblack); 
    purple = BitmapFactory.decodeResource(getResources(), R.drawable.purplehairedpotatoblack); 
    plus = BitmapFactory.decodeResource(getResources(), R.drawable.plus); 
    titleTest = BitmapFactory.decodeResource(getResources(), R.drawable.title); 
    playTest = BitmapFactory.decodeResource(getResources(), R.drawable.play); 


    setContentView(ourSurfaceView); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_gfx, menu); 
    return true; 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    ourSurfaceView.pause(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    ourSurfaceView.resume(); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 

    try { 
     Thread.sleep(1000/60); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 

    x = event.getX(); 
    y = event.getY(); 

    switch (event.getAction()){ 
     case MotionEvent.ACTION_DOWN: 
      //if(y > play.getTextBounds("Play",);) 
      break; 
    } 

    return true; 
} 
public class MyBringBack extends SurfaceView implements Runnable { 

    SurfaceHolder ourHolder; 
    Thread ourThread = null; 
    boolean isRunning = false; 


    public MyBringBack(Context context) { 
     super(context); 
     ourHolder = getHolder(); 
    } 



    public void pause(){ 
     isRunning = false; 
     while(true){ 
      try { 
       ourThread.join(); 
      } catch (InterruptedException e){ 
       e.printStackTrace(); 
      } 
      break; 
     } 
     ourThread = null; 
    } 
    public void resume(){ 
     isRunning = true; 
     ourThread = new Thread(this); 
     ourThread.start(); 
    } 



    @Override 
    public void run() { 
     while(isRunning){ 
      if(!ourHolder.getSurface().isValid()) 
       continue; 

      Canvas canvas = ourHolder.lockCanvas(); 
      canvas.drawColor(Color.BLACK); 


      if(firstRun == 0){ 
       changingRedX = 0 - red.getWidth(); 
       yellowXThing = 1; 
       changingYellowX = canvas.getWidth(); 
       greenXThing = 1; 
       firstRun = 1; 



       title = new Paint(); 
       title.setColor(Color.parseColor("#0889ec")); 
       title.setTextSize(180); 
       title.setTextAlign(Paint.Align.CENTER); 
       title.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); 


       play = new Paint(); 
       play.setTextSize(140); 
       play.setColor(Color.WHITE); 
       play.setTextAlign(Paint.Align.CENTER); 

       options = new Paint(); 
       options.setTextSize(140); 
       options.setColor(Color.WHITE); 
       options.setTextAlign(Paint.Align.CENTER); 

       playFixed = getResizedBitmap(playTest, canvas.getWidth()/5, canvas.getHeight()/3); 
       redFixed = getResizedBitmap(red, canvas.getWidth()/6, (int)canvas.getWidth()/6 * (red.getHeight()/red.getWidth())); 
       yellowFixed = getResizedBitmap(yellow, canvas.getWidth()/6, (int)canvas.getWidth()/6 * (yellow.getHeight()/yellow.getWidth())); 
       greenFixed = getResizedBitmap(green, canvas.getWidth()/6, (int)canvas.getWidth()/6 * (green.getHeight()/green.getWidth())); 
       purpleFixed = getResizedBitmap(purple, canvas.getWidth()/6, (int)canvas.getWidth()/6 * (purple.getHeight()/purple.getWidth())); 
      } 

      if((x>(canvas.getWidth()/2)-(playFixed.getWidth()/2)&&(x<(canvas.getWidth()/2)-(playFixed.getWidth()/2) + playFixed.getWidth())) 
        && ((y > 4*(canvas.getHeight()/9))&& (y<4*(canvas.getHeight()/9)+playFixed.getHeight()))){ 


       **** HERE IS WHERE I WANT TO START THE NEW INTENT **** 


      } 

      if(whereIsRedY == 0){ 
       whereIsRedXY = (int) (Math.random() * canvas.getHeight()) - 20; 
       whereIsRedY = 1; 

      } 
      if(whereIsYellowY == 0){ 
       whereIsYellowXY = (int) (Math.random() * canvas.getHeight()) - 20; 
       whereIsYellowY = 1; 
      } 
      if(whereIsGreenY == 0){ 
       whereIsGreenXY = (int) (Math.random() * canvas.getWidth()) - 20; 
       whereIsGreenY = 1; 
      } 
      if(whereIsPurpleY == 0){ 
       whereIsPurpleXY = (int) (Math.random() * canvas.getWidth()) - 20; 
       whereIsPurpleY = 1; 
      } 

      if((changingRedX > canvas.getWidth() + redFixed.getWidth()) && redXThing == 0){ 
       changingRedX = canvas.getWidth() + 3*(redFixed.getWidth()); 
       whereIsRedY = 0; 
       redXThing = 1; 
      } 
      if((changingRedX < 0 - redFixed.getWidth()) && redXThing == 1){ 
       changingRedX = 0 - 4*(redFixed.getWidth()); 
       whereIsRedY = 0; 
       redXThing = 0; 
      } 
      if(redXThing == 0) { 
       changingRedX += 10; 
      } 
      if(redXThing == 1) { 
       changingRedX -= 10; 
      } 

      if((changingYellowX > canvas.getWidth() + yellowFixed.getWidth()) && yellowXThing == 0){ 
       changingYellowX = canvas.getWidth() + 3*(yellowFixed.getWidth()); 
       whereIsYellowY = 0; 
       yellowXThing = 1; 
      } 
      if((changingYellowX < 0 - yellowFixed.getWidth()) && yellowXThing == 1){ 
       changingYellowX = 0 - 4*(yellowFixed.getWidth()); 
       whereIsYellowY = 0; 
       yellowXThing = 0; 
      } 
      if(yellowXThing == 0) { 
       changingYellowX += 13; 
      } 
      if(yellowXThing == 1) { 
       changingYellowX -= 13; 
      } 

      if((changingGreenX > canvas.getHeight() + greenFixed.getHeight()) && greenXThing == 0){ 
       changingGreenX = canvas.getHeight() + 3*(greenFixed.getHeight()); 
       whereIsGreenY = 0; 
       greenXThing = 1; 
      } 
      if((changingGreenX < 0 - greenFixed.getHeight()) && greenXThing == 1){ 
       changingGreenX = 0 - 4*(greenFixed.getHeight()); 
       whereIsGreenY = 0; 
       greenXThing = 0; 
      } 
      if(greenXThing == 0) { 
       changingGreenX += 8; 
      } 
      if(greenXThing == 1) { 
       changingGreenX -= 8; 
      } 

      if((changingPurpleX > canvas.getHeight() + purpleFixed.getHeight()) && purpleXThing == 0){ 
       changingPurpleX = canvas.getHeight() + 3*(purpleFixed.getHeight()); 
       whereIsPurpleY = 0; 
       purpleXThing = 1; 
      } 
      if((changingPurpleX < 0 - purpleFixed.getHeight()) && purpleXThing == 1){ 
       changingPurpleX = 0 - 4*(purpleFixed.getHeight()); 
       whereIsPurpleY = 0; 
       purpleXThing = 0; 
      } 
      if(purpleXThing == 0) { 
       changingPurpleX += 15; 
      } 
      if(purpleXThing == 1) { 
       changingPurpleX -= 15; 
      } 



      canvas.drawBitmap(redFixed, changingRedX, whereIsRedXY, null); 
      canvas.drawBitmap(yellowFixed, changingYellowX, whereIsYellowXY, null); 
      canvas.drawBitmap(purpleFixed, whereIsPurpleXY, changingPurpleX, null); 
      canvas.drawBitmap(greenFixed, whereIsGreenXY, changingGreenX, null); 
      canvas.drawText("Mustache Me", (canvas.getWidth())/2, (canvas.getHeight()/3), title); 
      //canvas.drawText("Play", canvas.getWidth()/2, canvas.getHeight()/2 + 40, play); 
      canvas.drawBitmap(playFixed, (canvas.getWidth()/2)-(playFixed.getWidth()/2), 4*(canvas.getHeight()/9), null); 
      canvas.drawText("Options", canvas.getWidth()/2, 2*(canvas.getHeight()/3), options); 

      ourHolder.unlockCanvasAndPost(canvas); 

     } 
    } 
} 
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) 
{ 
    int width = bm.getWidth(); 
    int height = bm.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 
    // create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 
    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 
    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 
    return resizedBitmap; 
} 

}

Я маркирован в моем коде, где я хочу намерение начать, но я получаю сообщение об ошибке: «Не удается разрешить конструктор Intetn (... ..)»

код, я попытался было это:

   Intent i = new Intent(this, WhichGameActivity.class); 

Спасибо за любую помощь :)

Ch EERS.

+0

Вы пытались использовать 'new Intent (thisactivity.this, another.class)' – Gavin

+0

Является ли «Невозможно разрешить конструктор Intetn (.....)» фактическую ошибку, которую вы получаете, или орфографическую ошибку? Можете ли вы опубликовать вывод журнала ... – Karl

ответ

0

Используйте это:

new Intent(GFX.this,WhichGameActivity.class); 

Вы не можете просто использовать this, потому что вы находитесь внутри внутреннего класса, таким образом, this принадлежит к контексту внутреннего класса.

0

Обратите внимание на то, что Docs сказать о Intent конструктор:

public Intent (Context packageContext, Class cls)

Create an intent for a specific component. ...

Parameters

packageContext A Context of the application package implementing this class. cls The component class that is to be used for the intent.

Ваш класс MyBringBack не Context, так что вы не можете использовать этот код:

Intent i = new Intent(this, WhichGameActivity.class); 

Вы должны хранить контекст, получен в конструкторе MyBringBack как поле, например «myContext», а затем использовать его в new Intent() следующим образом:

Intent i = new Intent(myContext, WhichGameActivity.class); 
Смежные вопросы