最近在做项目,需要设置图片阴影,发现ImageView根本没有直接设置阴影的方法和属性,只能自己自定义。以下资源来源于网,整理下方便以后查看。
1. TextView、Button在代码中添加文字阴影
mTextView.setShadowLayer(12f, 7F,8F, Color.RED); 第一个参数为模糊度,越大越模糊。 第二个参数是阴影离开文字的x横向距离。 第三个参数是阴影离开文字的Y横向距离。 第四个参数是阴影颜色。(如果模糊度为0是看不到阴影效果的)
2.在配置文件中添加文字阴影
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5.0dip"
android:layout_marginLeft="10.0dip"
android:shadowColor="@color/black"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="10"
android:textSize="16dp" />
android:shadowColor 阴影的颜色
android:shadowDx 阴影的水平偏移量
android:shadowDy 阴影的垂直偏移量
android:shadowRadius 阴影的范围
需要注意的地方 :将android:shadowRadius=0 的时候是看不到阴影的
Button类似。
3.简单的图片阴影
利用BlurMaskFilter进行处理, BlurMaskFilter指定了一个模糊的样式和半径来处理Paint的边缘。
public class ButtonShadow extends Button {
private Bitmap mBmp;
private Paint mPaint;
public ButtonShadow(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,new int[]{android.R.attr.background});
mBmp = BitmapFactory.decodeResource(context.getResources(), a.getResourceId(0, R.drawable.ic_launcher));
setBackgroundDrawable(null);
init(context);
}
private void init(Context context){
mPaint = new Paint();
BlurMaskFilter bf = new BlurMaskFilter(20,BlurMaskFilter.Blur.INNER);
mPaint.setColor(context.getResources().getColor(R.color.gray));
mPaint.setMaskFilter(bf);
}
public void setBitmap(Bitmap bm) {
mBmp = bm;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBmp.extractAlpha(mPaint, null), 0,
10, mPaint);
canvas.drawBitmap(mBmp,0,0,null);
}
}
4.自定义图片
- class ImageEffect extends View{
- Paint paint;
- public ImageEffect(Context context){
- super(context);
- paint= new Paint();
- paint.setAntiAlias(true);
- paint.setShadowLayer(5f, 5.0f, 5.0f, Color.BLACK);
- paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
-
- }
- public void onDraw(Canvas canvas){
- super.onDraw(canvas);
- int posX = 20;
- int posY = 50;
- int PicWidth,PicHeight;
-
- Drawable drawable = getResources().getDrawable(R.drawable.pic);
- Drawable dbe = getResources().getDrawable(R.drawable.pic).mutate();
- Drawable drawTest = getResources().getDrawable(R.drawable.pic);
- Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
- PicWidth = drawable.getIntrinsicWidth();
- PicHeight = drawable.getIntrinsicHeight();
-
- drawTest.setBounds(posX, (2 * posY) + PicHeight, posX + PicWidth, (2 * posY) + 2 * PicHeight );
- drawable.setBounds(posX,posY,posX+PicWidth,posY+PicHeight);
- dbe.setBounds(0, 0, PicWidth, PicHeight);
-
- canvas.drawColor(Color.WHITE);
- canvas.save(Canvas.MATRIX_SAVE_FLAG);
- dbe.setColorFilter(0x7f000000,PorterDuff.Mode.SRC_IN);
- canvas.translate(posX + (int)(0.9 * PicWidth/2), posY + PicHeight/2);
- canvas.skew(-0.9F, 0.0F);
- canvas.scale(1.0f, 0.5f);
- dbe.draw(canvas);
- drawable.clearColorFilter();
- canvas.restore();
-
- canvas.save(Canvas.MATRIX_SAVE_FLAG);
- drawable.draw(canvas);
- canvas.restore();
-
- }
- }