2011-11-11 7 views

Répondre

6

Voici mon code complet pour le constructeur de l'ombre de glisser sur mesure (gist for custom drag shadow).

Cependant, comme d'autres ont déclaré qu'il n'y a pas possibilité de modifier l'ombre de glisser en utilisant la fonctionnalité native introduite dans l'API-11.

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.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); 
    } 
} 
+0

Marcin, Que faire si mon ombre personnalisée n'est pas un bitmap, mais une mise en page? J'ai une question sans réponse (petite prime fournie) concernant un tel scénario: http://stackoverflow.com/questions/22274384/how-to-create-a-custom- glisser-ombre –

Questions connexes