Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

creating opaque shadows in android while dragging

HI,
I have the following piece of code which generates a shadow of an app icon when it is dragged. The issue is although my original image is not translucent android automatically creates a translucent image. How can i produce an opaque DragShadow :

code :
 public boolean onAppListLongItemClick(AdapterView<?> parent, View view, int position) {
                AllAppsAdapter adapter = (AllAppsAdapter) parent.getAdapter();
                adapter.setDraggableItemPosition(position);
                adapter.notifyDataSetChanged();
                LauncherItem launcherItem = adapter.getItem(position);
                Drawable icon = new BitmapDrawable(getResources(), VaderApp.getIconManager().get(launcherItem));
[b]                View.DragShadowBuilder myShadow = new MyDragShadowBuilder(view, icon.getConstantState().newDrawable());[/b]
                DragInfo dragInfo = new DragInfo(DragInfo.DragSource.APP_LIST, view, adapter.getItem(position));
                view.startDrag(null,
                        myShadow,
                        dragInfo,
                        0);
                showContextMenu();
                if(!isSystemApp(launcherItem))
                    findViewById(R.id.uninstall).setVisibility(View.VISIBLE);
                else
                    findViewById(R.id.uninstall).setVisibility(View.GONE);
                findViewById(R.id.remove).setVisibility(View.GONE);
                findViewById(R.id.app_info).setVisibility(View.VISIBLE);
                findViewById(R.id.add_fav).setVisibility(View.VISIBLE);
                return true;
            }
        };


private static class MyDragShadowBuilder extends View.DragShadowBuilder {
        private Drawable _shadow;

        public MyDragShadowBuilder(View v, Drawable shadow) {
            super(v);
            _shadow = shadow;
        }

        @Override
        public void onProvideShadowMetrics(@NonNull Point size, @NonNull Point touch) {
            int width, height;
            width = getView().getResources().getDimensionPixelSize(R.dimen.drag_icon_enlarged);
            height = getView().getResources().getDimensionPixelSize(R.dimen.drag_icon_enlarged);
            int intrinsicHeight = _shadow.getIntrinsicHeight();
            int intrinsicWidth = _shadow.getIntrinsicWidth();
            if (intrinsicHeight > 0 && intrinsicWidth > 0) {
                if (width > height) {
                    height = width * intrinsicHeight / intrinsicWidth;
                } else {
                    width = height * intrinsicWidth / intrinsicHeight;
                }
                _shadow.setBounds(0, 0, width, height);
                size.set(width, height);
                touch.set(width / 2, height / 2);
            } else {
                _shadow.setBounds(0, 0, width, height);
                size.set(width, height);
                touch.set(width / 2, height / 2);
            }
        }

        @Override
        public void onDrawShadow(@NonNull Canvas canvas) {
            _shadow.draw(canvas);
        }
    }

Open in new window


Thanks
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

It cannot be done using the android library. Since 4.4 there is no api for dragShadow. Changing of the alpha of the View used to work, but not any more apparently.
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
shadowBuilder.getView().setAlpha(1);

Open in new window


It looks like the only way of achieving it is drawing  a second image and create your own custom drag shadow. There are some answers for this on stackoverflow but it sounds like a lot of work for not much return.
Avatar of Rohit Bajaj

ASKER

HI,
Thanks for information. Can you give some ideas on how can i go for implementing my own custom drag shadow
ASKER CERTIFIED SOLUTION
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial