多意图的起点由onTouch事件意图、起点、事件、onTouch

2023-09-12 22:27:04 作者:爱到深处衣自脱

嘿,大家好我得到启动的意图,如果用户点击特定location.at第一次触球,他打开一个菜单,并在第二,他打开了活性。问题是,很多副本的目的相同的启动

Hey guys i am getting to start an intent if a user clicks on specific location.at first touch he opens a menu and on second he opens the activity.The problem is that many copy's of same intent are started

 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.Paint.Align;
 import android.os.Bundle;
import android.text.TextPaint;
import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.OnTouchListener;
 import android.widget.Toast;

  public class gfx extends Activity implements OnTouchListener{
Bitmap a,b;
gfx1 drw;
String a1;
boolean flag=false,flag1=false,flag2=false;
Canvas c1;
float x=0,y=0,z=0,bitx=0,bity=0;
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    drw = new gfx1(this);
    drw.setOnTouchListener(this);
    setContentView(drw);
}

public class gfx1 extends View implements Runnable {

    public gfx1(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        a = BitmapFactory.decodeResource(getResources(),
                R.drawable.greenball);

    }
    void callin(String a1)
    {
        Intent inte = new Intent(a1);
        startActivity(inte);
    }

    @Override
    protected void onDraw(Canvas c1) {
        // TODO Auto-generated method stub
        super.onDraw(c1);
        c1.drawColor(Color.YELLOW);
        b=Bitmap.createScaledBitmap(a,c1.getWidth()/3, c1.getHeight()/3, true);
        bitx=(c1.getWidth()/2)-(a.getWidth()/2);
        bity=(c1.getHeight()/2)-(a.getHeight()/2);
        c1.drawBitmap(a,  bitx, bity, null);

        Paint textPaint = new Paint();
        textPaint.setARGB(50, 254, 10, 50);
        textPaint.setTextAlign(Align.CENTER);
        textPaint.setTextSize(30);
        if(flag)
        {
            c1.drawText("clicked",300, 300,textPaint);
            c1.drawBitmap(b,(c1.getWidth()/2)-(b.getWidth()/2),(c1.getHeight()/2)+(a.getHeight()/2), null);
            c1.drawBitmap(b,(c1.getWidth()/2)-(b.getWidth()/2),(c1.getHeight()/2)-(a.getHeight()/2)-(b.getHeight()), null);
        }
        float bitbx1=(c1.getWidth()/2)-(b.getWidth()/2);
        float bitbx2=(c1.getWidth()/2)+(b.getWidth()/2);
        float bitby1=(c1.getHeight()/2)-(b.getHeight())-(a.getHeight()/2);
        float bitby2=((c1.getHeight()/2)-(a.getHeight()/2));
        if(flag2)
        {
            c1.drawText("Opening...please wait", 600, 600, textPaint);
            flag1=true;
            if(flag1)
            {
                a1="com.example.claci.MAINACTIVITY";
            callin(a1);
            }
            flag1=false;
        }

        if((x>bitx&&x<bitx+(a.getWidth()))&& (y>bity&&y<bity+(a.getHeight())))
        {
            flag=true;

        }
        if((x>bitbx1&&x<bitbx2)&&(y>bitby1&&y<bitby2))
        {
    if(flag)
    {
                flag2=true;
    }
        }
        invalidate();

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub


    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
        x=event.getX();
        y=event.getY();
        break;
}
    return true;

} }

推荐答案

一个更好的结构是将有你的菜单创作,并随后创建活动从onTouch方法调用。正如psink提到,这不是正确使用OnDraw中,并且它不必要的链接拖很不相干的事情。

A much better structure would be to have your menu creation, and subsequent activity creation called from onTouch method. As psink mentioned, that is not the proper use of onDraw, and it unnecessarily links tow very unrelated things.

我也想用两个班variables--一个是一个标志,如果菜单中还存在的,另一种是引用新创建活动。如果活动已存在,则不必创建一个又一个。当存在活动完成,不过,它需要一个结果返回到这个活动,所以你可以清除参考,并准备建立在需要的时候一个新的。

I would also use two class variables-- one that is a flag for if the menu exists yet, and another that is a reference to the newly created activity. If an activity already exists, you don't create another one. When the existent activity completes, though, it needs to return a result to this activity so you can clear that reference and be ready to create a new one when needed.