2011-03-18 32 views
2

我想添加阴影效果做一个图像。我使用一个蒙版来绘制图像(我需要一个特定的形状为我的图像)。你能告诉我如何为我的图像添加阴影效果吗?我尝试了一些像paint.setShadowLayer(10,10,10,Color.RED)但它没有奏效。这里是源代码:android影子效果

@Override 
public void draw(Canvas canvas) { 
    Rect rect = new Rect(0, 0, getWidth() - 1, getHeight() - 1); 
    NinePatchDrawable mask = (NinePatchDrawable) getContext().getResources().getDrawable(maskResId); 
    mask.setBounds(rect); 
    Bitmap content = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888); 
    Canvas contentCanvas = new Canvas(content); 
    super.draw(contentCanvas); 
    Paint paint = new Paint(); 
    paint.setXfermode(new AvoidXfermode(Color.BLACK, 255, AvoidXfermode.Mode.TARGET)); 
    mask.draw(canvas); 
    canvas.drawBitmap(content, null, rect, paint); 
} 
+0

检查[CoverFlow](http://www.inter-fuser.com/2010/01/android-coverflow-widget.html)Widget。 或者[Android 3D Carousel](http://www.codeproject.com/KB/android/androcarousel.aspx) 你肯定会在那里得到一些东西。 – 2011-03-18 13:18:59

+0

看看这个答案http://stackoverflow.com/a/33889791/4356754 – 2015-11-24 09:30:49

回答

3

我的解决方案:

类的ShadowImage

public class ShadowImage extends BitmapDrawable { 

Bitmap bm; 
static float shadowRadius = 4f; 
static PointF shadowDirection = new PointF(2f, 2f); 
int fillColor = 0; 

@Override 
public void draw(Canvas canvas) { 

    Rect rect = new Rect(0, 0, bm.getWidth(), bm.getHeight()); 
    Log.i("TEST", rect.toString()); 
    setBounds(rect); 

    Paint mShadow = new Paint(); 
    mShadow.setAntiAlias(true); 
    mShadow.setShadowLayer(shadowRadius, shadowDirection.x, shadowDirection.y, Color.BLACK); 

    canvas.drawRect(rect, mShadow); 
    if(fillColor != 0) { 
     Paint mFill = new Paint(); 
     mFill.setColor(fillColor); 
     canvas.drawRect(rect, mFill); 
    } 
    canvas.drawBitmap(bm, 0.0f, 0.0f, null); 

} 

public ShadowImage(Resources res, Bitmap bitmap) { 
    super(res, Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth()+shadowRadius*shadowDirection.x), (int) (bitmap.getHeight()+shadowRadius*shadowDirection.y), false)); 
    this.bm = bitmap; 
} 
public ShadowImage(Resources res, Bitmap bitmap, int fillColor) { 
    super(res, Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth()+shadowRadius*shadowDirection.x), (int) (bitmap.getHeight()+shadowRadius*shadowDirection.y), false)); 
    this.bm = bitmap; 
    this.fillColor = fillColor; 
} 

}

活动:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    LinearLayout root = (LinearLayout) findViewById(R.id.root_layout); 

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
    ShadowImage image = new ShadowImage(getResources(), bmp); 
    ShadowImage image2 = new ShadowImage(getResources(), bmp, Color.WHITE); 

    ImageView iv_normal = new ImageView(getApplicationContext()); 
    iv_normal.setPadding(10, 10, 10, 10); 
    iv_normal.setImageBitmap(bmp); 

    ImageView iv_shadow = new ImageView(getApplicationContext()); 
    iv_shadow.setPadding(10, 10, 10, 10); 
    iv_shadow.setImageDrawable(image); 

    ImageView iv_fill = new ImageView(getApplicationContext()); 
    iv_fill.setPadding(10, 10, 10, 10); 
    iv_fill.setImageDrawable(image2); 

    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    root.addView(iv_normal, params); 
    root.addView(iv_shadow, params); 
    root.addView(iv_fill, params); 
    root.setGravity(Gravity.CENTER); 
} 

图片:Screenshot

+0

陈述明显,这将只适用于矩形图像?而不是像,例如,一个圆的PNG? – user3453281 2015-04-14 14:28:05

+0

是的。这项工作只是与rects。但是您更改此代码以绘制循环或使用阴影图层的圆角矩形 注意:为了获得更好的性能,请将draw()的新XYZ()调用移动到init() – Mario 2015-05-28 10:18:29