如何构建传递位图的自定义DragShadow?在这里我的代码:
ImageView shadowView = new ImageView(getActivity());
shadowView.setLayoutParams(new LayoutParams(100,100));
shadowView.setImageResource(R.drawable.usuario);
shadowView.setX(me.getX());
shadowView.setY(me.getY());
shadowView.setVisibility(View.VISIBLE);
View.DragShadowBuilder myShadow = new MyDragShadowBuilder(shadowView);
...
public class MyDragShadowBuilder extends DragShadowBuilder {
Bitmap bitmap;
public MyDragShadowBuilder(View v) {
super(v);
v.setDrawingCacheEnabled(true);
bitmap = v.getDrawingCache();
}
@Override
public void onDrawShadow(Canvas canvas) {
super.onDrawShadow(canvas);
canvas = new Canvas(bitmap.copy(Bitmap.Config.ARGB_8888, true)); //Here I have nullPointerException
}
@Override
public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
shadowSize.set(100,100);
shadowTouchPoint.x = (int)(shadowSize.x / 2);
shadowTouchPoint.y = (int)(shadowSize.y / 2);
}
}
这给我带来了错误:
06-18 13:37:18.650: E/View(15993): Unable to initiate drag
06-18 13:37:18.650: E/View(15993): java.lang.NullPointerException
06-18 13:37:18.650: E/View(15993): at com.signlab.genieapp.main.UsuariosListView$MyDragShadowBuilder.onDrawShadow(UsuariosListView.java:512)
06-18 13:37:18.650: E/View(15993): at android.view.View.startDrag(View.java:17058)
06-18 13:37:18.650: E/View(15993): at com.signlab.genieapp.main.UsuariosListView$UsuarioTouchListener.onTouch(UsuariosListView.java:421)
06-18 13:37:18.650: E/View(15993): at android.view.View.dispatchTouchEvent(View.java:7555)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2285)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2013)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2291)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1985)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2291)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1985)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2291)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1985)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2291)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1985)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2291)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1985)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2291)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1985)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2291)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1985)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2291)
06-18 13:37:18.650: E/View(15993): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1985)
06-18 13:37:18.650: E/View(15993): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2269)
06-18 13:37:18.650: E/View(15993): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1564)
06-18 13:37:18.650: E/View(15993): at android.app.Activity.dispatchTouchEvent(Activity.java:2468)
06-18 13:37:18.650: E/View(15993): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2217)
06-18 13:37:18.650: E/View(15993): at android.view.View.dispatchPointerEvent(View.java:7754)
06-18 13:37:18.650: E/View(15993): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3810)
06-18 13:37:18.650: E/View(15993): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3694)
06-18 13:37:18.650: E/View(15993): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4936)
06-18 13:37:18.650: E/View(15993): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4915)
06-18 13:37:18.650: E/View(15993): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5013)
06-18 13:37:18.650: E/View(15993): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
06-18 13:37:18.650: E/View(15993): at android.os.MessageQueue.nativePollOnce(Native Method)
06-18 13:37:18.650: E/View(15993): at android.os.MessageQueue.next(MessageQueue.java:125)
06-18 13:37:18.650: E/View(15993): at android.os.Looper.loop(Looper.java:138)
06-18 13:37:18.650: E/View(15993): at android.app.ActivityThread.main(ActivityThread.java:5279)
06-18 13:37:18.650: E/View(15993): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 13:37:18.650: E/View(15993): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 13:37:18.650: E/View(15993): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-18 13:37:18.650: E/View(15993): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-18 13:37:18.650: E/View(15993): at dalvik.system.NativeStart.main(Native Method)