0
我對android很陌生,我試圖在onDragListener
上實現演示,並且拖動工作正常,但拖動結束時imageView
隱藏。我嘗試了很多選擇,但無法使其可見。而另外一部分是我創建的按鈕,點擊imageView
...
以下是我的代碼...ImageView在丟棄後不可見
public class MainActivity extends Activity {
Button b1,b2;
FrameLayout f;
ImageView imageview;
private View selected_item = null;
private boolean dragging = false;
private static final int START_DRAGGING = 0;
private static final int STOP_DRAGGING = 1;
private int status;
private LinearLayout m_llTop;
private float oldXvalue;
private float oldYvalue;
RelativeLayout layout;
int x;
int y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
captureImageInitialization();
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
f=(FrameLayout)findViewById(R.id.framelayout);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
layout=new RelativeLayout(getBaseContext());
//layout.setOrientation(LinearLayout.VERTICAL);
imageview=new ImageView(getBaseContext());
final TextView tv=new TextView(getBaseContext());
/*TextView tv1=new TextView(getBaseContext());
TextView tv2=new TextView(getBaseContext());
TextView tv3=new TextView(getBaseContext());
TextView tv4=new TextView(getBaseContext());*/
RelativeLayout.LayoutParams ivparams = new RelativeLayout.LayoutParams(
100,100);
RelativeLayout.LayoutParams tvparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ivparams.setMargins(0,0,10,0);
imageview.setId(1);
registerForContextMenu(imageview);
imageview.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
}else {
return false;
}
}
});
imageview.setBackgroundResource(R.drawable.ic_launcher);
imageview.setLayoutParams(ivparams);
imageview.getRootView().setOnDragListener(new OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
View view = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DROP:
float X = event.getX();
float Y = event.getY();
view.setX(X-(imageview.getWidth()/2));
view.setY(Y-(imageview.getHeight()/2));
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_EXITED:
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
if(dropEventNotHandled(event)){
view.setVisibility(View.INVISIBLE);
}
break;
default:
break;
}
return true;
}
});
registerForContextMenu(tv);
layout.addView(imageview);
layout.addView(tv);
f.addView(layout);
}
});
}
private boolean dropEventNotHandled(DragEvent dragEvent) {
return !dragEvent.getResult();
}
}
你爲什麼在'onTouch()'裏面使用'view.setVisibility(View.INVISIBLE)'? – SMR
用view.setVisibility(View.VISIBLE)寫入動作的條件; – San
你需要添加新的佈局到你的圖像相同的佈局 – Wannabe