我有一個全屏的FrameLayout裏面一個小的ImageView,我想設置這樣的,當我接觸到的FrameLayout的ImageView的移動到那個位置。試圖經由setLayoutParams觸摸事件的移動ImageView的,ImageView的不動
我讀過所有其他的做題我可以找到有關移動的ImageView,但我還沒有看到,爲什麼我的解決方案還沒有工作。
在延伸的FrameLayout的自定義視圖:
@Override
public boolean onInterceptTouchEvent(MotionEvent event){
if(event.getActionMasked() == MotionEvent.ACTION_DOWN){
ImageView iv = (ImageView) this.findViewById(R.id.draggedImage);
FrameLayout.LayoutParams params = (LayoutParams) iv.getLayoutParams();
params.leftMargin = (int) event.getX();
params.bottomMargin = (int) event.getY();
iv.setLayoutParams(params);
iv.invalidate();
invalidate();
}
return true;
}
我已經改變了的LayoutParams保證金,以反映新的觸摸位置,和無效的ImageView的和的FrameLayout(不知道其中哪些我應該做的事) ,但imageView不會退出(總是坐在框架的左上角)。
我還證實,當我觸摸屏幕和params爲越來越設定理智的X和Y值這個方法被調用。
我的佈局:
<CustomFrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- some other unrelated views -->
<ImageView
android:id="@+id/draggedImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/pause" />
</CustomFrameLayout>
編輯: 應該提到,我努力的目標API級別10,所以我不能使用任何蜂窩視圖方向善良:(
我認爲改變填充確實有效,儘管這對我來說有點奇怪。要通過改變填充來移動它,我必須使imageview填充整個屏幕,而不是僅僅移動imageview。這可能是一個解決方案,但我有點想移動視圖。 – Tim
我試圖找出折舊絕對佈局的替代方案時,我做了一堆搜索。普遍的共識是使用框架佈局和填充位置。如果有人有更好的方法,我很樂意聽到它。 – cstrutton