gpt4 book ai didi

android - 拖放 ImageView 不起作用

转载 作者:行者123 更新时间:2023-11-29 00:13:23 26 4
gpt4 key购买 nike

我的 RelativeLayout 有一张图片。我只是想让它在整个布局中都可以拖动。问题是每次我拖放时,它都会回到原来的位置。这是我的

drag_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainView"
android:background="@drawable/bg_animation"
tools:context="com.example.activities.AnimationActivity">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher"
android:id="@+id/appLogo"
android:layout_alignBottom="@+id/ba_cell_animation"
android:layout_centerHorizontal="true"
android:layout_marginBottom="47dp" />
</RelativeLayout>

AnimationActivity.java

public class AnimationActivity extends ActionBarActivity implements View.OnTouchListener, View.OnDragListener {

static ImageView mLogo;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation);
setTitle("Animation");
Typeface mAnimPromptTF = Typeface.createFromAsset(getAssets(), "fonts/Jelloween - Machinato ExtraLight.ttf");
Typeface mAnimBonusTF = Typeface.createFromAsset(getAssets(), "fonts/Jelloween - Machinato SemiBold Italic.ttf");
mAnimPrompt = (TextView) findViewById(R.id.anim_prompt);
mAnimBonus = (TextView) findViewById(R.id.anim_bonus);
mAnimPrompt.setTypeface(mAnimPromptTF);
mAnimBonus.setTypeface(mAnimBonusTF);

mLogo = (ImageView) findViewById(R.id.appLogo);


mLogo.setOnTouchListener(this);
findViewById(R.id.mainView).setOnDragListener(this);

}

@Override
public boolean onTouch(View v, MotionEvent e) {
// TODO Auto-generated method stub
if (e.getAction() == MotionEvent.ACTION_DOWN) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}

@Override
public boolean onDrag(View v, DragEvent e) {
// TODO Auto-generated method stub

switch (e.getAction()) {
case DragEvent.ACTION_DROP:
View view = (View) e.getLocalState();
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
RelativeLayout to = (RelativeLayout) v;
to.addView(view);
view.setVisibility(View.VISIBLE);

break;

//the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
break;

//the user has moved the drag shadow outside the bounding box of the View
case DragEvent.ACTION_DRAG_EXITED:
break;

// the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
break;

default:


break;
}

return true;
}

@Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}

我已经尝试了以下所有链接,但仍然不知道哪里出错了:

http://www.vogella.com/tutorials/AndroidDragAndDrop/article.html

http://codingjunkie.net/android-drag-and-drop-part1

http://tech-papers.org/android-drag-and-drop

我真的尝试了很多。

最佳答案

我终于找到了解决方案,请引用以下代码:就像这段代码一样简单。

@Override
public boolean onTouch(View v, MotionEvent e) {

switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:

View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
return true;
}

return false;

}

@Override
public boolean onDrag(View mainView, DragEvent e) {
View view = (View) e.getLocalState();
switch (e.getAction()) {
case DragEvent.ACTION_DROP:
view.setX(e.getX() - (view.getWidth() / 2));
view.setY(e.getY() - (view.getHeight() / 2));
view.invalidate();
mainView.invalidate();
return true;
case DragEvent.ACTION_DRAG_STARTED:
return true;

case DragEvent.ACTION_DRAG_EXITED:
break;

case DragEvent.ACTION_DRAG_ENDED:
mainView.invalidate();
return true;

default:


break;
}

return true;
}

关于android - 拖放 ImageView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28533762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com