Android Open Source - BrokenDisplay Overlay Editor Fragment






From Project

Back to project page BrokenDisplay.

License

The source code is released under:

Apache License

If you think the Android project BrokenDisplay listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.foolish.brokendisplay;
//from w w w.j  a  va  2 s.  co  m
import android.app.Fragment;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnClickListener;
import android.view.View.OnDragListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class OverlayEditorFragment extends Fragment {

  private FrameLayout mRootView = null;

  private LinearLayout mComponentBox = null;
  private boolean mComponentBoxVisible = true;
  private int mComponentBoxWidth = 0;
  private static final int COMPONENT_BOX_OFFSET_X = 20;

  private Button mBtnPreview = null;

  private Intent mServiceIntent;
  private boolean mServiceStarted = false;

  private TextView mTvTest = null;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    mRootView = (FrameLayout) inflater.inflate(
        R.layout.fragment_overlay_editor, container, false);

    mComponentBox = (LinearLayout) mRootView
        .findViewById(R.id.fragment_overlay_editor_componentbox);
    mBtnPreview = (Button) mRootView
        .findViewById(R.id.fragment_overlay_editor_btn_componentbox_preview);
    mTvTest = (TextView) mRootView.findViewById(R.id.tvTest);

    mServiceIntent = new Intent(getActivity(), BrokenGlassService.class);
    registerListeners();

    return mRootView;
  }

  private void registerListeners() {
    mComponentBox.getViewTreeObserver().addOnGlobalLayoutListener(
        new OnGlobalLayoutListener() {
          @Override
          public void onGlobalLayout() {
            mComponentBox.getViewTreeObserver()
                .removeOnGlobalLayoutListener(this);
            LayoutParams params = (LayoutParams) mComponentBox
                .getLayoutParams();
            params.width = mComponentBox.getWidth()
                - (mComponentBox.getWidth() / 4);
            mComponentBoxWidth = params.width;
            mComponentBox.setLayoutParams(params);
            mComponentBox
                .setOnTouchListener(new OnSwipeTouchListener(
                    getActivity()) {
                  @Override
                  public void onSwipeTop() {
                  }

                  @Override
                  public void onSwipeRight() {
                    if (!mComponentBoxVisible) {
                      performAnim();
                    }
                  }

                  @Override
                  public void onSwipeLeft() {
                    if (mComponentBoxVisible) {
                      performAnim();
                    }
                  }

                  @Override
                  public void onSwipeBottom() {
                  }
                });
          }
        });

    mBtnPreview.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (!mServiceStarted) {
          getActivity().startService(mServiceIntent);
        } else {
          getActivity().stopService(mServiceIntent);
        }
        mServiceStarted = !mServiceStarted;
      }
    });

    mTvTest.setOnLongClickListener(new OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
        v.startDrag(new ClipData(mTvTest.getText(),
            new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN },
            new ClipData.Item(mTvTest.getText())),
            new DragShadowBuilder(v), v, 0);

        performAnim();

        return false;
      }
    });

    mRootView.setOnDragListener(new OnDragListener() {
      @Override
      public boolean onDrag(View v, DragEvent event) {
        final int action = event.getAction();

        switch (action) {
        case DragEvent.ACTION_DRAG_STARTED:
          if (event.getClipDescription().hasMimeType(
              ClipDescription.MIMETYPE_TEXT_PLAIN)) {
            v.setBackgroundColor(Color.argb(128, 0, 0, 255));
            v.invalidate();
            return true;
          }
          return false;
        case DragEvent.ACTION_DRAG_ENTERED:
          v.setBackgroundColor(Color.argb(128, 0, 255, 0));
          v.invalidate();
          return true;
        case DragEvent.ACTION_DRAG_LOCATION:
          return true;
        case DragEvent.ACTION_DRAG_EXITED:
          v.setBackgroundColor(Color.argb(128, 0, 0, 255));
          v.invalidate();
          return true;
        case DragEvent.ACTION_DROP:
          View draggedView = (View) event.getLocalState();
          mComponentBox.removeView(draggedView);
          LayoutParams params = new LayoutParams(draggedView
              .getLayoutParams());
          params.leftMargin = (int) event.getX();
          params.topMargin = (int) event.getY();
          mRootView.setLayoutParams(params);
          mRootView.addView((View) event.getLocalState());
          v.setBackground(null);
          v.invalidate();
          return true;
        case DragEvent.ACTION_DRAG_ENDED:
          v.setBackground(null);
          v.invalidate();
          return true;
        default:
          break;
        }

        return false;

      }
    });
  }

  @Override
  public void onDestroy() {
    super.onDestroy();

    getActivity().stopService(mServiceIntent);
  }

  private void performAnim() {
    final float to = mComponentBoxVisible ? -mComponentBoxWidth
        + COMPONENT_BOX_OFFSET_X : 0;
    mComponentBox.animate().setDuration(500).x(to).start();
    mComponentBoxVisible = !mComponentBoxVisible;
  }

}




Java Source Code List

com.foolish.brokendisplay.BrokenDisplayActivity.java
com.foolish.brokendisplay.BrokenGlassService.java
com.foolish.brokendisplay.ItemDetailActivity.java
com.foolish.brokendisplay.ItemDetailFragment.java
com.foolish.brokendisplay.ItemListActivity.java
com.foolish.brokendisplay.ItemListFragment.java
com.foolish.brokendisplay.MyImageView.java
com.foolish.brokendisplay.OnSwipeTouchListener.java
com.foolish.brokendisplay.OverlayEditorActivity.java
com.foolish.brokendisplay.OverlayEditorFragment.java
com.foolish.brokendisplay.OverviewFragment.java
com.foolish.brokendisplay.dummy.DummyContent.java
com.foolish.brokendisplay.picturedialog.BitmapWorkerTask.java
com.foolish.brokendisplay.picturedialog.IOnSelectPictureDialog.java
com.foolish.brokendisplay.picturedialog.Picture_GridAdapter.java
com.foolish.brokendisplay.picturedialog.SelectPictureDialog.java