嗨,我是 android 新手,我使用 android 2.2 api level-8.so 我可以使用两个图像,一个用于拖动目的,另一个用于 Drop 目的。在这里我显示一些我可以在这里做的代码。代码是哪个显示设备高度宽度和球是我拖动的图像视图。
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
Log.e("Screen Width-->", "" + windowwidth);
Log.e("Screen Height-->", "" + windowheight);
ball = (ImageView) findViewById(R.id.secondImage);
tempAnimationDrawable = (AnimationDrawable) ball.getDrawable();
ball.setOnTouchListener(this);
在此应用 seton touch Listener。on touchListener 如下。
public boolean onTouch(View v, MotionEvent event)
{
layoutParams = (RelativeLayout.LayoutParams) ball.getLayoutParams();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int imgWidth = ball.getWidth();
Log.e("Image Width -->", "" + imgWidth);
int imgHeight = ball.getHeight();
Log.e("Image Height -->", "" + imgHeight);
int x_cord = (int) event.getRawX();
System.err.println("Display X code->" + x_cord);
int y_cord = (int) event.getRawY();
System.err.println("Display y code->" + y_cord);
int getX=(int) event.getX();
System.err.println("Display Here X Value -->"+getX);
int getY=(int) event.getY();
System.err.println("Display Here Y Value -->"+getY);
if (x_cord < windowwidth && getX < imgWidth )
{
x_cord = windowwidth;
Log.e("If Part X Cord-->", "" + x_cord);
}
if (y_cord > windowheight)
{
y_cord = windowheight;
Log.e("If Part Y Cord-->", "" + y_cord);
}
layoutParams.setMargins(imgWidth + incLeft, imgHeight + incTop, imgWidth + incRight, imgHeight + incBottom);
// layoutParams.setMargins(left, top, right, bottom)
ball.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
现在我该怎么办。