Created
July 29, 2013 15:38
-
-
Save eveliotc/6105241 to your computer and use it in GitHub Desktop.
A variant of https://gist.github.com/eveliotc/6051367 to show a drop shadow, should not be used for performance in mind views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final ImageView imageView = (ImageView) view.findViewById(R.id.image); | |
final int shadowSize = getResources().getDimensionPixelSize(R.dimen.shadow_size); | |
final int shadowColor = getResources().getColor(R.color.shadow_color); | |
imageView.setImageDrawable(new RoundedAvatarDrawable(mBitmap, shadowSize, shadowColor)); | |
if (SDK_INT >= HONEYCOMB) { | |
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2013 Evelio Tarazona Cáceres <[email protected]> | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package info.evelio.drawable; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapShader; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.ColorFilter; | |
import android.graphics.Paint; | |
import android.graphics.PixelFormat; | |
import android.graphics.Rect; | |
import android.graphics.RectF; | |
import android.graphics.Shader; | |
import android.graphics.drawable.Drawable; | |
/** | |
* A Drawable that draws an oval with given {@link android.graphics.Bitmap} | |
*/ | |
public class RoundedAvatarDrawable extends Drawable { | |
private final Bitmap mBitmap; | |
private final Paint mPaint; | |
private final RectF mRectF; | |
private final int mBitmapWidth; | |
private final int mBitmapHeight; | |
private final float mShadowOffset; | |
private final Paint mShadowPaint; | |
public RoundedAvatarDrawable(Bitmap bitmap, float shadowSize, int shadowColor) { | |
mBitmap = bitmap; | |
mRectF = new RectF(); | |
mPaint = new Paint(); | |
mPaint.setAntiAlias(true); | |
mPaint.setDither(true); | |
mShadowPaint = new Paint(mPaint); | |
final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); | |
mPaint.setShader(shader); | |
// Reason to use another paint for this is explained below | |
mShadowPaint.setColor(Color.WHITE); | |
mShadowPaint.setShadowLayer(shadowSize, 0.0f, 0.0f, shadowColor); | |
// NOTE: we assume bitmap is properly scaled to current density | |
mShadowOffset = shadowSize; | |
mBitmapWidth = (int) (mBitmap.getWidth() + mShadowOffset + 0.5f); | |
mBitmapHeight = (int) (mBitmap.getHeight() + mShadowOffset + 0.5f); | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
// Overdraw dragons ahead | |
// We draw another oval below our actual bitmap so shadow is drawn properly | |
canvas.drawOval(mRectF, mShadowPaint); | |
canvas.drawOval(mRectF, mPaint); | |
} | |
@Override | |
protected void onBoundsChange(Rect bounds) { | |
super.onBoundsChange(bounds); | |
mRectF.set(bounds); | |
mRectF.inset(mShadowOffset, mShadowOffset); | |
} | |
@Override | |
public void setAlpha(int alpha) { | |
if (mPaint.getAlpha() != alpha) { | |
mPaint.setAlpha(alpha); | |
invalidateSelf(); | |
} | |
} | |
@Override | |
public void setColorFilter(ColorFilter cf) { | |
mPaint.setColorFilter(cf); | |
} | |
@Override | |
public int getOpacity() { | |
return PixelFormat.TRANSLUCENT; | |
} | |
@Override | |
public int getIntrinsicWidth() { | |
return mBitmapWidth; | |
} | |
@Override | |
public int getIntrinsicHeight() { | |
return mBitmapHeight; | |
} | |
public void setAntiAlias(boolean aa) { | |
mPaint.setAntiAlias(aa); | |
invalidateSelf(); | |
} | |
@Override | |
public void setFilterBitmap(boolean filter) { | |
mPaint.setFilterBitmap(filter); | |
invalidateSelf(); | |
} | |
@Override | |
public void setDither(boolean dither) { | |
mPaint.setDither(dither); | |
invalidateSelf(); | |
} | |
public Bitmap getBitmap() { | |
return mBitmap; | |
} | |
// TODO allow set and use target density, mutate, constant state, changing configurations, etc. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<dimen name="shadow_size">1.5dip</dimen> | |
<color name="shadow_color">#ff9f9f9f</color> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment