android 阴影图像效果

发布于 2024-10-24 04:57:53 字数 765 浏览 3 评论 0原文

我正在尝试为图像添加阴影效果。我使用蒙版来绘制图像(我的图像需要特定的形状)。你能告诉我如何为我的图像添加阴影效果吗?我尝试过类似 Paint.setShadowLayer(10, 10, 10, Color.RED) 的方法,但没有成功。这是源代码:

 @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);
}

I'm trying to add shadow effect do an image. I use a mask to draw the image (I need a specific shape for my image). Can you please tell me how to add shadow effect to my image? I've tried something like paint.setShadowLayer(10, 10, 10, Color.RED) but it didn't worked. Here is the source code:

 @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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫弦 2024-10-31 04:57:53

我的解决方案:

class 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;
}

}

Activity:

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);
}

Image: Screenshot

my solution:

class 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;
}

}

Activity:

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);
}

Image: Screenshot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文