Android 向ImageView添加阴影
是否可以在ImageView的内容上放置阴影 不是正方形,而是作用于ImageView的不透明内容上的对象阴影Android 向ImageView添加阴影,android,layout,bitmap,imageview,Android,Layout,Bitmap,Imageview,是否可以在ImageView的内容上放置阴影 不是正方形,而是作用于ImageView的不透明内容上的对象阴影 这是罗曼·盖伊在Devxx的演讲,pdf格式 由于您已经将图像转换为位图,请尝试以下操作 Paint mShadow = new Paint(); // radius=10, y-offset=2, color=black mShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000); // in onDraw(Canvas) ca
这是罗曼·盖伊在Devxx的演讲,pdf格式 由于您已经将图像转换为位图,请尝试以下操作
Paint mShadow = new Paint();
// radius=10, y-offset=2, color=black
mShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);
// in onDraw(Canvas)
canvas.drawBitmap(bitmap, 0.0f, 0.0f, mShadow);
希望这有帮助
有关更多信息,请遵循以下步骤
注释
这是罗曼·盖伊在Devxx的演讲,pdf格式 由于您已经将图像转换为位图,请尝试以下操作
Paint mShadow = new Paint();
// radius=10, y-offset=2, color=black
mShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);
// in onDraw(Canvas)
canvas.drawBitmap(bitmap, 0.0f, 0.0f, mShadow);
希望这有帮助
有关更多信息,请遵循以下步骤
注释
我面临着同样的问题,必须迅速想出办法。这可能不是最好的解决方案,但我的解决办法是添加
android:adjustViewBounds="true"
在图像视图上,然后将与源相同的图像设置为背景。这样,背景的大小和形状与源图像相同,因此阴影看起来很自然,因为android中的阴影是基于ui元素背景的。我也遇到了同样的问题,必须尽快解决。这可能不是最好的解决方案,但我的解决办法是添加
android:adjustViewBounds="true"
在图像视图上,然后将与源相同的图像设置为背景。这样,背景的大小和形状与源图像相同,因此阴影看起来很自然,因为android中的阴影基于ui元素背景。使用此类在位图上绘制阴影
public class ShadowGenerator {
// Percent of actual icon size
private static final float HALF_DISTANCE = 0.5f;
public static final float BLUR_FACTOR = 0.5f/48;
// Percent of actual icon size
private static final float KEY_SHADOW_DISTANCE = 1f/48;
public static final int KEY_SHADOW_ALPHA = 61;
public static final int AMBIENT_SHADOW_ALPHA = 30;
private static final Object LOCK = new Object();
// Singleton object guarded by {@link #LOCK}
private static ShadowGenerator sShadowGenerator;
private int mIconSize;
private final Canvas mCanvas;
private final Paint mBlurPaint;
private final Paint mDrawPaint;
private final Context mContext;
private ShadowGenerator(Context context) {
mContext = context;
mCanvas = new Canvas();
mBlurPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
mBlurPaint.setMaskFilter(new BlurMaskFilter(mIconSize * BLUR_FACTOR, Blur.NORMAL));
mDrawPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
}
public synchronized Bitmap recreateIcon(Bitmap icon) {
mIconSize = Utils.convertDpToPixel(mContext,3)+icon.getWidth();
int[] offset = new int[2];
Bitmap shadow = icon.extractAlpha(mBlurPaint, offset);
Bitmap result = Bitmap.createBitmap(mIconSize, mIconSize, Config.ARGB_8888);
mCanvas.setBitmap(result);
// Draw ambient shadow
mDrawPaint.setAlpha(AMBIENT_SHADOW_ALPHA);
mCanvas.drawBitmap(shadow, offset[0], offset[1], mDrawPaint);
// Draw key shadow
mDrawPaint.setAlpha(KEY_SHADOW_ALPHA);
mCanvas.drawBitmap(shadow, offset[0], offset[1] + KEY_SHADOW_DISTANCE * mIconSize, mDrawPaint);
// Draw the icon
mDrawPaint.setAlpha(255);
mCanvas.drawBitmap(icon, 0, 0, mDrawPaint);
mCanvas.setBitmap(null);
return result;
}
public static ShadowGenerator getInstance(Context context) {
synchronized (LOCK) {
if (sShadowGenerator == null) {
sShadowGenerator = new ShadowGenerator(context);
}
}
return sShadowGenerator;
}
}
使用此类在位图上绘制阴影
public class ShadowGenerator {
// Percent of actual icon size
private static final float HALF_DISTANCE = 0.5f;
public static final float BLUR_FACTOR = 0.5f/48;
// Percent of actual icon size
private static final float KEY_SHADOW_DISTANCE = 1f/48;
public static final int KEY_SHADOW_ALPHA = 61;
public static final int AMBIENT_SHADOW_ALPHA = 30;
private static final Object LOCK = new Object();
// Singleton object guarded by {@link #LOCK}
private static ShadowGenerator sShadowGenerator;
private int mIconSize;
private final Canvas mCanvas;
private final Paint mBlurPaint;
private final Paint mDrawPaint;
private final Context mContext;
private ShadowGenerator(Context context) {
mContext = context;
mCanvas = new Canvas();
mBlurPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
mBlurPaint.setMaskFilter(new BlurMaskFilter(mIconSize * BLUR_FACTOR, Blur.NORMAL));
mDrawPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
}
public synchronized Bitmap recreateIcon(Bitmap icon) {
mIconSize = Utils.convertDpToPixel(mContext,3)+icon.getWidth();
int[] offset = new int[2];
Bitmap shadow = icon.extractAlpha(mBlurPaint, offset);
Bitmap result = Bitmap.createBitmap(mIconSize, mIconSize, Config.ARGB_8888);
mCanvas.setBitmap(result);
// Draw ambient shadow
mDrawPaint.setAlpha(AMBIENT_SHADOW_ALPHA);
mCanvas.drawBitmap(shadow, offset[0], offset[1], mDrawPaint);
// Draw key shadow
mDrawPaint.setAlpha(KEY_SHADOW_ALPHA);
mCanvas.drawBitmap(shadow, offset[0], offset[1] + KEY_SHADOW_DISTANCE * mIconSize, mDrawPaint);
// Draw the icon
mDrawPaint.setAlpha(255);
mCanvas.drawBitmap(icon, 0, 0, mDrawPaint);
mCanvas.setBitmap(null);
return result;
}
public static ShadowGenerator getInstance(Context context) {
synchronized (LOCK) {
if (sShadowGenerator == null) {
sShadowGenerator = new ShadowGenerator(context);
}
}
return sShadowGenerator;
}
}
这是可能的。您可以为此使用图层列表。您所需要的只是图像视图和放置的图像。然后使用图层列表将它们堆叠为一个。@ThomasR。你所说的水滴图像是什么意思?我以编程方式生成位图,并可以在ImageView中使用它们。到目前为止我只知道了。啊,好的。那么我认为群众之王的回答应该对你有所帮助。这是可能的。您可以为此使用图层列表。您所需要的只是图像视图和放置的图像。然后使用图层列表将它们堆叠为一个。@ThomasR。你所说的水滴图像是什么意思?我以编程方式生成位图,并可以在ImageView中使用它们。到目前为止我只知道了。啊,好的。那么我认为@King of Mass的答案应该对你有所帮助。在我的自定义图像视图课程中尝试过,但只显示了图像<代码>@Override public void onDraw(Canvas Canvas){Bitmap piece=((BitmapDrawable)this.getDrawable()).getBitmap();Paint mShadow=new Paint();mShadow.setShadowLayer(10.0f,0.0f,2.0f,0xFF000000);this.setLayerType(LAYER_TYPE_软件,mShadow);Canvas.drawBitmap(piece,0,0,mShadow);}您需要更改收音机和偏移值以获得更好的效果现在效果更好,但它不显示任何颜色,将图像的像素显示为阴影层。如果我的回答有助于解决问题,请将其标记为已接受,这可能对将来的某个人有所帮助:)抱歉,我无法解决问题。因此,我不能将其标记为已接受。我希望能够设置阴影颜色。在自定义图像视图类中尝试过,但仅显示图像<代码>@Override public void onDraw(Canvas Canvas){Bitmap piece=((BitmapDrawable)this.getDrawable()).getBitmap();Paint mShadow=new Paint();mShadow.setShadowLayer(10.0f,0.0f,2.0f,0xFF000000);this.setLayerType(LAYER_TYPE_软件,mShadow);Canvas.drawBitmap(piece,0,0,mShadow);}您需要更改收音机和偏移值以获得更好的效果现在效果更好,但它不显示任何颜色,将图像的像素显示为阴影层。如果我的回答有助于解决问题,请将其标记为已接受,这可能对将来的某个人有所帮助:)抱歉,我无法解决问题。因此,我不能将其标记为已接受。我希望能够设置阴影颜色。