Android:从onClick在画布上启动意图

时间:2015-03-19 03:22:08

标签: java android canvas 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);

感谢您的帮助:)

干杯。

2 个答案:

答案 0 :(得分:0)

使用此:

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

你不能只使用this,因为你在内部类中,因此this属于内部类上下文。

答案 1 :(得分:0)

注意DocsIntent构造函数的说法:

  

public Intent(Context packageContext,Class cls)

     

为特定组件创建意图。 ...

     

参数

     

packageContext 实现此功能的应用程序包的上下文   类。 cls要用于的组件类   意图。

您的班级MyBringBack不是Context,因此您无法使用此代码:

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

您必须将MyBringBack的构造函数中收到的上下文存储为字段,例如“myContext”,然后在new Intent()中使用它,如下所示:

Intent i = new Intent(myContext, WhichGameActivity.class);
相关问题