/** Draw the view into a bitmap. */
  private static Bitmap getViewBitmap(View v) {
    v.clearFocus();
    v.setPressed(false);

    boolean willNotCache = v.willNotCacheDrawing();
    v.setWillNotCacheDrawing(false);

    // Reset the drawing cache background color to fully transparent
    // for the duration of this operation
    int color = v.getDrawingCacheBackgroundColor();
    v.setDrawingCacheBackgroundColor(0);

    if (color != 0) {
      v.destroyDrawingCache();
    }
    v.buildDrawingCache();
    Bitmap cacheBitmap = v.getDrawingCache();
    if (cacheBitmap == null) {
      Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
      return null;
    }

    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

    // Restore the view
    v.destroyDrawingCache();
    v.setWillNotCacheDrawing(willNotCache);
    v.setDrawingCacheBackgroundColor(color);

    return bitmap;
  }
Example #2
0
  /** Draw the view into a bitmap. */
  private Bitmap getViewBitmap(View v) {
    v.clearFocus();
    v.setPressed(false);

    boolean willNotCache = v.willNotCacheDrawing();
    v.setWillNotCacheDrawing(false);

    // Reset the drawing cache background color to fully transparent
    // for the duration of this operation
    int color = v.getDrawingCacheBackgroundColor();
    v.setDrawingCacheBackgroundColor(0);

    if (color != 0) {
      v.destroyDrawingCache();
    }
    v.buildDrawingCache();
    //		float xScale = v.getScaleX();
    //		float yScale = v.getScaleY();
    Bitmap cacheBitmap = v.getDrawingCache();

    if (cacheBitmap == null) {
      Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
      return null;
    }

    // if(xScale != 1.0f || xScale != 1.0f ){
    // cacheBitmap = getResizedBitmap(cacheBitmap, (int)(cacheBitmap.getHeight()*yScale),
    // (int)(cacheBitmap.getWidth()*xScale));
    // }

    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

    // Restore the view
    v.destroyDrawingCache();
    v.setWillNotCacheDrawing(willNotCache);
    v.setDrawingCacheBackgroundColor(color);

    return bitmap;
  }
  public static Bitmap getViewBitmap(View v) {
    v.clearFocus();
    v.setPressed(false);

    // 能画缓存就返回false
    boolean willNotCache = v.willNotCacheDrawing();
    v.setWillNotCacheDrawing(false);
    int color = v.getDrawingCacheBackgroundColor();
    v.setDrawingCacheBackgroundColor(0);
    if (color != 0) {
      v.destroyDrawingCache();
    }
    v.buildDrawingCache();
    Bitmap cacheBitmap = v.getDrawingCache();
    if (cacheBitmap == null) {
      return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
    // Restore the view
    v.destroyDrawingCache();
    v.setWillNotCacheDrawing(willNotCache);
    v.setDrawingCacheBackgroundColor(color);
    return bitmap;
  }
  public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
    if (PROFILE_DRAWING_DURING_DRAG) {
      android.os.Debug.startMethodTracing("Launcher");
    }
    mTagPopup = v.getTag(R.id.TAG_PREVIEW);

    // Hide soft keyboard, if visible
    if (mInputMethodManager == null) {
      mInputMethodManager =
          (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    }
    mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);

    // Faruq: ArrayList For Each
    for (DragListener l : mListener) {
      l.onDragStart(v, source, dragInfo, dragAction);
    }
    Rect r = mDragRect;
    r.set(v.getScrollX(), v.getScrollY(), 0, 0);
    mOriginalX = mLastMotionX;
    mOriginalY = mLastMotionY;

    offsetDescendantRectToMyCoords(v, r);
    mTouchOffsetX = mLastMotionX - r.left;
    mTouchOffsetY = mLastMotionY - r.top;

    v.clearFocus();
    v.setPressed(false);

    boolean willNotCache = v.willNotCacheDrawing();
    v.setWillNotCacheDrawing(false);

    // Reset the drawing cache background color to fully transparent
    // for the duration of this operation
    int color = v.getDrawingCacheBackgroundColor();
    v.setDrawingCacheBackgroundColor(0);

    if (color != 0) {
      v.destroyDrawingCache();
    }
    v.buildDrawingCache();
    Bitmap viewBitmap = v.getDrawingCache();
    if (viewBitmap != null) {
      mDrawModeBitmap = true;
      int width = viewBitmap.getWidth();
      int height = viewBitmap.getHeight();

      Matrix scale = new Matrix();
      float scaleFactor = v.getWidth();
      scaleFactor = (scaleFactor + DRAG_SCALE) / scaleFactor;
      scale.setScale(scaleFactor, scaleFactor);

      mAnimationTo = 1.0f;
      mAnimationFrom = 1.0f / scaleFactor;
      mAnimationDuration = ANIMATION_SCALE_UP_DURATION;
      mAnimationState = ANIMATION_STATE_STARTING;
      mAnimationType = ANIMATION_TYPE_SCALE;
      try {
        mDragBitmap = Bitmap.createBitmap(viewBitmap, 0, 0, width, height, scale, true);
        final Bitmap dragBitmap = mDragBitmap;
        mBitmapOffsetX = (dragBitmap.getWidth() - width) / 2;
        mBitmapOffsetY = (dragBitmap.getHeight() - height) / 2;
      } catch (OutOfMemoryError e) {
        mDrawModeBitmap = false;
        width = v.getWidth();
        height = v.getHeight();
        scaleFactor = v.getWidth();
        scaleFactor = (scaleFactor + DRAG_SCALE) / scaleFactor;
        mDrawWidth = (int) (v.getWidth() * scaleFactor);
        mDrawHeight = (int) (v.getHeight() * scaleFactor);
        mAnimationTo = 1.0f;
        mAnimationFrom = 1.0f / scaleFactor;
        mAnimationDuration = ANIMATION_SCALE_UP_DURATION;
        mAnimationState = ANIMATION_STATE_STARTING;
        mAnimationType = ANIMATION_TYPE_SCALE;
        mBitmapOffsetX = (mDrawWidth - width) / 2;
        mBitmapOffsetY = (mDrawHeight - height) / 2;
      }
      v.destroyDrawingCache();
      v.setWillNotCacheDrawing(willNotCache);
      v.setDrawingCacheBackgroundColor(color);
    } else {
      mDrawModeBitmap = false;
      int width = v.getWidth();
      int height = v.getHeight();
      float scaleFactor = v.getWidth();
      scaleFactor = (scaleFactor + DRAG_SCALE) / scaleFactor;
      mDrawWidth = (int) (v.getWidth() * scaleFactor);
      mDrawHeight = (int) (v.getHeight() * scaleFactor);
      mAnimationTo = 1.0f;
      mAnimationFrom = 1.0f / scaleFactor;
      mAnimationDuration = ANIMATION_SCALE_UP_DURATION;
      mAnimationState = ANIMATION_STATE_STARTING;
      mAnimationType = ANIMATION_TYPE_SCALE;
      mBitmapOffsetX = (mDrawWidth - width) / 2;
      mBitmapOffsetY = (mDrawHeight - height) / 2;
    }
    if (dragAction == DRAG_ACTION_MOVE) {
      v.setVisibility(GONE);
    }

    mDragPaint = null;
    mDragging = true;
    mShouldDrop = true;
    mOriginator = v;
    mDragSource = source;
    mDragInfo = dragInfo;

    performHapticFeedback(
        HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);

    mEnteredRegion = false;

    invalidate();
  }