Skip to content

Instantly share code, notes, and snippets.

@aniXification
Created June 30, 2013 10:49
Show Gist options
  • Save aniXification/5894714 to your computer and use it in GitHub Desktop.
Save aniXification/5894714 to your computer and use it in GitHub Desktop.
package com.marcingil.dragshadow;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
public class ImageDragShadowBuilder extends View.DragShadowBuilder {
private Drawable shadow;
private ImageDragShadowBuilder() {
super();
}
public static View.DragShadowBuilder fromResource(Context context, int drawableId) {
ImageDragShadowBuilder builder = new ImageDragShadowBuilder();
builder.shadow = context.getResources().getDrawable(drawableId);
if (builder.shadow == null) {
throw new NullPointerException("Drawable from id is null");
}
builder.shadow.setBounds(0, 0, builder.shadow.getMinimumWidth(), builder.shadow.getMinimumHeight());
return builder;
}
public static View.DragShadowBuilder fromBitmap(Context context, Bitmap bmp) {
if (bmp == null) {
throw new IllegalArgumentException("Bitmap cannot be null");
}
ImageDragShadowBuilder builder = new ImageDragShadowBuilder();
builder.shadow = new BitmapDrawable(context.getResources(), bmp);
builder.shadow.setBounds(0, 0, builder.shadow.getMinimumWidth(), builder.shadow.getMinimumHeight());
return builder;
}
@Override
public void onDrawShadow(Canvas canvas) {
shadow.draw(canvas);
}
@Override
public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
shadowSize.x = shadow.getMinimumWidth();
shadowSize.y = shadow.getMinimumHeight();
shadowTouchPoint.x = (int)(shadowSize.x / 2);
shadowTouchPoint.y = (int)(shadowSize.y / 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment