在我的问题中,我正在移动图像按钮onTouch事件以及拖动停止在该位置重绘图像按钮的位置。实际上这对于简单的按钮非常有效,但不适用于图像按钮。
public class NActivity extends Activity implements OnTouchListener {
private final static int START_DRAGGING = 0;
private final static int STOP_DRAGGING = 1;
private ImageButton btn;
private FrameLayout layout;
private int status;
private LayoutParams params;
private ImageView image;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.LinearLayout01);
// layout.setOnTouchListener(this);
btn = (ImageButton) findViewById(R.id.imageButton1);
btn.setDrawingCacheEnabled(true);
btn.setOnTouchListener(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
image = new ImageView(this);
image.setImageBitmap(btn.getDrawingCache());
layout.addView(image, params);
}
if (me.getAction() == MotionEvent.ACTION_UP) {
status = STOP_DRAGGING;
Log.i("Drag", "Stopped Dragging");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (status == START_DRAGGING) {
System.out.println("Dragging");
image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
image.invalidate();
}
}
return false;
}
}
答案 0 :(得分:0)
在我的示例中,我使用了布局并在其中放置了 ImageButton ,因此图像按钮将继承其内部布局的属性 XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageButton
android:src="@drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing" />
</LinearLayout>
</FrameLayout>
的java:
public class SomeClass extends Activity implements OnTouchListener {
private LinearLayout ll1;
private FrameLayout layout;
private float xAxis = 0;
private float yAxis = 0;
private LinearLayout newLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.LinearLayout01);
ll1 = (LinearLayout) findViewById(R.id.ll);
newLinearLayout = ll1;
ll1.setOnTouchListener(this);
}
public void redraw() {
layout.removeAllViews();
newLinearLayout.setPadding((int) xAxis, (int) yAxis, 0, 0);
layout.addView(newLinearLayout);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
xAxis = event.getX();
yAxis = event.getY();
redraw();
case MotionEvent.ACTION_UP:
System.out.println("intermediate finished");
}
return true;
}
}
它非常简单易用,最终 LinearLayout 中的哪个组件无关紧要,因为这个组件无关紧要