0
public class Sample2 extends Activity  {

private SampleView sView;
private static int displayWidth = 100;    //movement area
private static int displayHeight = 100;
float angle = 0;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    sView = new SampleView(this);
    setContentView(sView);


     }//oncreate

private class SampleView extends View {
    Context con;
    private Rect displayRect = null; //rect we display to
    private int scrollRectX = 0; //current left location of scroll rect
    private int scrollRectY = 0; //current top location of scroll rect
    private float scrollByX = 0; //scroll by amounts
    private float scrollByY = 0;
    private float startX = 0;   //track x from one ACTION_MOVE to the next
    private float startY = 0;   //track y from one ACTION_MOVE to the next
    private int state = 0;


    Bitmap bitmap2;


    public SampleView(Context context) {
        super(context);
        displayRect = new Rect(0, 0, displayWidth, displayHeight);
        }//constructor

  public boolean onTouchEvent(MotionEvent event) {
    float x;
    float y;

       switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN: 
                    //Initial down event location.
                        startX = event.getRawX();
                        startY = event.getRawY()-50;
                       // Log.e("TOUCHED",startY+" "+(scrollRectY+displayHeight));
                        if (((startX>scrollRectX)&(startX<(scrollRectX+displayWidth)))&
                           ((startY>scrollRectY)&(startY<(scrollRectY+displayHeight)))) state = 1;
                        //Log.e("TOUCHED","State "+state);
                        break;
                case MotionEvent.ACTION_MOVE:
                        x = event.getRawX();
                        y = event.getRawY()-50;                            
                        scrollByX = x - startX;
                        scrollByY = y - startY;
                        startX = x; 
                        startY = y;
                        if (state != 0) invalidate(); //move it
                        break;
                case MotionEvent.ACTION_UP:
                        x = event.getRawX();
                        y = event.getRawY()-50;
                        scrollByX = x - startX;
                        scrollByY = y - startY;
                        startX = x; 
                        startY = y;
                        state = 0;
                        invalidate();
                       break;
        }//switch
        return true;
    }//ontouch

protected void onDraw(Canvas canvas) {
   scrollRectX = scrollRectX+(int)scrollByX;
   scrollRectY = scrollRectY+(int)scrollByY;
   displayRect.set(scrollRectX,scrollRectY,scrollRectX+displayWidth,
                   scrollRectY+displayHeight);
   Paint paint = new Paint();
   Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
   bitmap2= BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher).copy(Config.RGB_565, true);
   canvas.drawBitmap(bitmap2, null, displayRect, paint);

//TODO: Fill In Methods Etc.
}
}
}

我已经使用了这段代码....现在我的问题是如何在android中为2个位图设置单独的toast消息?....如果我在背景矩形中触摸它会显示toast消息,如果我触摸图像它会显示另一个toast通过在 ontouchevents 上使用 if 条件发送消息....请 any1 可以这样说....

4

1 回答 1

0

你可以这样做,

演示.java

public class Demo extends Activity  implements OnClickListener{
 @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo_layout);
    final LinearLayout lin_layout = (LinearLayout) findViewById(R.id.lin_layout);
    final ImageView img_view = (EditText) findViewById(R.id.img_view);

     // call your touch event 
    l_l.setOnClickListener(this);
    imageView1.setOnClickListener(this);

 @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.l_l: // if you touch on layout
      // do your task
      Toast.makeText(getApplicationContext(), "You Touch on: Linear_Layout",
          Toast.LENGTH_LONG).show();
      break;

    case R.id.imageView1:  // if you touch on Image
      // do your task
      Toast.makeText(getApplicationContext(), "You Touch on : Image ", Toast.LENGTH_LONG)
          .show();
      break;
    default:
      break;
    }

  }

}
于 2012-11-05T09:31:19.707 回答