انشاء ديالوج يعمل بالسحب في سكيتشوير



 {tocify} $title={محتويات المقال}


الكود الاول


DraggableDialog dialog = new DraggableDialog(MainActivity.this);

final ImageView img = new ImageView(MainActivity.this);

img.setLayoutParams( new LinearLayout.LayoutParams((int)SketchwareUtil.getDip(getApplicationContext(),50),(int)SketchwareUtil.getDip(getApplicationContext(),50)));

img.setImageResource(R.drawable.hevo_team);

int temp = (int) SketchwareUtil.getDip(getApplicationContext(),5);
img.setPadding(temp,temp,temp,temp);

dialog.setView(img);

android.graphics.drawable.GradientDrawable SketchUi = new android.graphics.drawable.GradientDrawable();
SketchUi.setColor(0xFFFFFFFF);
SketchUi.setCornerRadius(SketchwareUtil.getDip(getApplicationContext(),360));
img.setElevation(SketchwareUtil.getDip(getApplicationContext(),2));
img.setBackground(SketchUi);

dialog.addDragListener(img);

dialog.setStickToEdge(true);{codeBox}

الكود الثاني

img.setOnClickListener(new OnClickListener() { public void onClick(View view) {{codeBox}

الكود الثالث 

} });
dialog.show();{codeBox}

الكود الرابع


}
public interface DragListener {
public void onDrag(View v,DraggableDialog dialog);
public void onDragStart(View v,DraggableDialog dialog);
public void onDragEnd(View v,DraggableDialog dialog);
}
public class DraggableDialog extends LinearLayout {
private View content;
private ViewGroup rootView;
private boolean stickToEdge;
public DraggableDialog(Context context) {
super(context);
this.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
this.setLayoutParams(params);
rootView =(ViewGroup)getWindow().findViewById(Window.ID_ANDROID_CONTENT);
}
public void addDragListener(final DragListener listener,final View view) {
view.setOnTouchListener(new View.OnTouchListener(){
        float oldX ,oldY;
        boolean moved = false;
  @Override
  public boolean onTouch(View v, MotionEvent event){
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
      moved= false;
      if(listener != null)listener.onDragStart(view,DraggableDialog.this);
                        oldX = event.getRawX();
                        oldY = event.getRawY();
               break;
               case MotionEvent.ACTION_MOVE:
               if(listener != null) listener.onDrag(view,DraggableDialog.this);
                        content.setX((int)(content.getX () -(oldX - event.getRawX())));
                        content.setY((int)(content.getY()- (oldY - event.getRawY())));
                        oldX = event.getRawX();
                        oldY = event.getRawY();
                        moved = true;
                        break;
      case MotionEvent.ACTION_UP:
      if(!moved)view.callOnClick();
               case MotionEvent.ACTION_CANCEL:
               if(moved){
               int x = (int)content.getX();
               int y = (int)content.getY();
      if(stickToEdge){
      x = content.getX()+content.getMeasuredWidth()/2 > (DraggableDialog.this.getMeasuredWidth()/2)?DraggableDialog.this.getMeasuredWidth() - content.getWidth():0;
      }else{
      if(x <0){
      x = 0;
      }else{
      int maxDistance =DraggableDialog.this.getMeasuredWidth()- content.getMeasuredWidth();
      if(maxDistance<x) x= maxDistance;
      }
      }
      if(y <0){
      y = 0;
      }else{
      int maxDistance =DraggableDialog.this.getMeasuredHeight()- content.getMeasuredHeight();
      if(maxDistance<y) y = maxDistance;
      }
      content.animate().y(y).x(x).setListener(
      new AnimatorListenerAdapter() {
      @Override public void onAnimationEnd(Animator animation) {
         super.onAnimationEnd(animation);
         if(listener != null)listener.onDragEnd(view,DraggableDialog.this);
      } });
      }
      break;
      default:break;
    } return true;
  }
});
}
public void addDragListener(final View view) {
addDragListener(null,view);
}
public void setView(View view) {
this.invalidate();
content = view;
this.removeAllViews();
this.addView(content);
}
public void setStickToEdge(boolean val){
stickToEdge = val;
if(stickToEdge){
this.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
}else{
this.setGravity(Gravity.CENTER);
}
}
public void show() {
rootView.addView(this);
}
public void dismiss() {
try {
rootView.removeView(this);
} catch(NullPointerException e){}
}{codeBox}



إرسال تعليق

اكتب تعليق

أحدث أقدم