2012-04-12 67 views

回答

0

你可以計算你的屏幕大小,可以有些怎麼樣這個

Display display = getWindowManager().getDefaultDisplay(); 
      width = display.getWidth(); 
      height = display.getHeight(); 
0

你不想OnTouchListener,你想OnDragListener屏幕內修復形象的舉動。查找不同的DragEvent動作類型,以獲取有關視圖所在位置的信息。從那裏,只要你知道你的尺寸View,你可以禁止它移出屏幕。

1

我沒有類似的東西根據我發現這裏的代碼:DragView Example

它是基於谷歌的啓動代碼。爲了保持屏幕上的視圖,我修改了onTouchEvent方法以保持屏幕上的視圖。

public boolean onTouchEvent(MotionEvent ev) { 
    if (!mDragging) { 
     return false; 
    } 
    final int action = ev.getAction(); 
    final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels); 
    final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels); 
    //**ADDED to keep view entirely on screen 
    final int[] parentpos = {-1,-1}; 
    ((View) mDragSource).getLocationOnScreen(parentpos); 
    int minScreenX = parentpos[0] + (int)mTouchOffsetX; 
    int minScreenY = parentpos[1] + (int)mTouchOffsetY; 
    int maxScreenX = minScreenX + ((View) mDragSource).getWidth() - mDragView.getWidth(); 
    int maxScreenY = minScreenY + ((View) mDragSource).getHeight() - mDragView.getHeight(); 
    //end ADDED 

這裏

case MotionEvent.ACTION_MOVE: 
     // Update the drag view. Don't use the clamped pos here so the dragging looks 
     // like it goes off screen a little, intead of bumping up against the edge. 
     // **CHANGED to keep view entirely on screen 
     mDragView.move(clamp(screenX,minScreenX,maxScreenX), 
       clamp(screenY,minScreenY,maxScreenY)); 
     // end CHANGED 

這裏

case MotionEvent.ACTION_UP: 
     if (mDragging) { 
      // **CHANGED to keep view entirely on screen 
      drop(clamp(screenX,minScreenX,maxScreenX), 
        clamp(screenY,minScreenY,maxScreenY)); 
      //end CHANGED 
     } 
     endDrag(); 
1

如果你想在頁面中顯示的圖像,使用下面的代碼:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="2px" 
     android:src="pathOfImage" /> 

    </LinearLayout> 

,或者你可以使用下面的代碼設置高度和寬度:

Display display = getWindowManager().getDefaultDisplay(); 
    width = display.getWidth(); 
    height = display.getHeight(); 

    ImageView imageView=(ImageView) findViewById(R.id.image); 
    imageView.setLayoutParams(new LayoutParams(width, height));