好的,我一直在阅读和搜索,现在我把头撞在墙上试图解决这个问题。这是我到目前为止的内容:
package com.pockdroid.sandbox;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.widget.ImageView;
public class ShadowImageView extends ImageView {
private Rect mRect;
private Paint mPaint;
public ShadowImageView(Context context)
{
    super(context);
    mRect = new Rect();
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setShadowLayer(2f, 1f, 1f, Color.BLACK);
}
@Override
protected void onDraw(Canvas canvas) 
{
    Rect r = mRect;
    Paint paint = mPaint;
    canvas.drawRect(r, paint);
    super.onDraw(canvas);
}
@Override
protected void onMeasure(int w, int h)
{
    super.onMeasure(w,h);
    int mH, mW;
    mW = getSuggestedMinimumWidth() < getMeasuredWidth()? getMeasuredWidth() : getSuggestedMinimumWidth();
    mH = getSuggestedMinimumHeight() < getMeasuredHeight()? getMeasuredHeight() : getSuggestedMinimumHeight();
    setMeasuredDimension(mW + 5, mH + 5);
}
}
测量中的“ +5”是暂时的;据我了解,我需要做一些数学运算以确定投影阴影添加到画布上的大小,对吗?
但是当我使用这个:
public View getView(int position, View convertView, ViewGroup parent) {
    ShadowImageView sImageView;
    if (convertView == null) {
        sImageView = new ShadowImageView(mContext);
        GridView.LayoutParams lp = new GridView.LayoutParams(85, 85);
        sImageView.setLayoutParams(lp);
        sImageView.setScaleType(ImageView.ScaleType.CENTER);
        sImageView.setPadding(5,5,5,5);
    } else {
        sImageView = (ShadowImageView) convertView;
    }
    sImageView.setImageBitmap(bitmapList.get(position));
    return sImageView;
}
在我的ImageView中,当我运行该程序时,仍然只能得到普通的ImageView。
有什么想法吗?谢谢。
编辑:因此,我在IRC频道中与RomainGuy进行了交谈,并且我现在使用下面的代码来处理纯矩形图像。但是,它仍然不会直接将阴影绘制到位图的透明度上,因此我仍在努力。
@Override
protected void onDraw(Canvas canvas) 
{
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.omen);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShadowLayer(5.5f, 6.0f, 6.0f, Color.BLACK);
    canvas.drawColor(Color.GRAY);
    canvas.drawRect(50, 50, 50 + bmp.getWidth(), 50 + bmp.getHeight(), paint);
    canvas.drawBitmap(bmp, 50, 50, null);       
}
          
                  “它现在适用于普通的矩形图像” ...所以它不适用于非矩形图像,然后我认为它也不适用于9patch图像,对吗?您是否同时使它正常工作?因为Romain Guy的这种方法在我的测试中还不适用于我。
                
                
                  
                    —
                    Mathias Conradt 
                    
                  
                
              
                  嗯,有趣的问题。我认为您可能会使用9修补程序的View,并将其包装在FrameLayout中,并为FrameLayout设置阴影9修补程序背景。但是,是的,它仅适用于矩形图像,因为9色块无法遵循透明度轮廓。不幸的是,我还没有找到更好的解决方案,但是,此后我还没有真正尝试过。
                
                
                  
                    —
                    凯文·科波克
                    
                  
                
              


