Android的阴影图像效果 [英] android shadow image effect

查看:206
本文介绍了Android的阴影图像效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试添加阴影效果做一个形象。我使用遮罩绘制图像(我需要为我的图像中的特定形状)。你能告诉我怎么阴影效果添加到我的形象?我想是这样paint.setShadowLayer(10,10,10,Color.RED),但它并没有奏效。这里是源$ C ​​$ C:

  @覆盖
公共无效画(油画画布){
    矩形矩形=新的Rect(0,0,的getWidth() -  1,的getHeight() -  1);
    NinePatchDrawable面膜=(NinePatchDrawable)的getContext()getResources()getDrawable(maskResId)。
    mask.setBounds(RECT);
    位图的内容= Bitmap.createBitmap(rect.width(),rect.height(),Bitmap.Config.ARGB_8888);
    帆布contentCanvas =新的Canvas(内容);
    super.draw(contentCanvas);
    涂料粉刷=新的油漆();
    paint.setXfermode(新AvoidXfermode(Color.BLACK,255,AvoidXfermode.Mode.TARGET));
    mask.draw(画布);
    canvas.drawBitmap(内容为空,矩形,油漆);
}
 

解决方案

我的解决办法:

类的ShadowImage

 公共类的ShadowImage扩展BitmapDrawable {

位图BM;
静浮shadowRadius = 4F;
静态的PointF shadowDirection =新的PointF(2F,2F);
INT填充颜色= 0;

@覆盖
公共无效画(油画画布){

    矩形矩形=新的Rect(0,0,bm.getWidth(),bm.getHeight());
    Log.i(TEST,rect.toString());
    的setBounds(RECT);

    油漆mShadow =新的油漆();
    mShadow.setAntiAlias​​(真正的);
    mShadow.setShadowLayer(shadowRadius,shadowDirection.x,shadowDirection.y,Color.BLACK);

    canvas.drawRect(矩形,mShadow);
    如果(填充颜色!= 0){
        油漆mFill =新的油漆();
        mFill.setColor(填充颜色);
        canvas.drawRect(矩形,mFill);
    }
    canvas.drawBitmap(BM,0.0,0.0,NULL);

}

公众的ShadowImage(资源资源,位图位图){
    超(RES,Bitmap.createScaledBitmap(位图,(INT)(bitmap.getWidth()+ shadowRadius * shadowDirection.x),(INT)(bitmap.getHeight()+ shadowRadius * shadowDirection.y),FALSE));
    this.bm =位图;
}
公众的ShadowImage(资源资源,位图位图,诠释填充颜色){
    超(RES,Bitmap.createScaledBitmap(位图,(INT)(bitmap.getWidth()+ shadowRadius * shadowDirection.x),(INT)(bitmap.getHeight()+ shadowRadius * shadowDirection.y),FALSE));
    this.bm =位图;
    this.fillColor =填充颜色;
}
 

}

活动:

 保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    的LinearLayout根=(的LinearLayout)findViewById(R.id.root_layout);

    BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
    的ShadowImage形象=新的ShadowImage(getResources(),BMP);
    的ShadowImage IMAGE2 =新的ShadowImage(getResources(),BMP,Color.WHITE);

    ImageView的iv_normal =新ImageView的(getApplicationContext());
    iv_normal.setPadding(10,10,10,10);
    iv_normal.setImageBitmap(BMP);

    ImageView的iv_shadow =新ImageView的(getApplicationContext());
    iv_shadow.setPadding(10,10,10,10);
    iv_shadow.setImageDrawable(图像);

    ImageView的iv_fill =新ImageView的(getApplicationContext());
    iv_fill.setPadding(10,10,10,10);
    iv_fill.setImageDrawable(IMAGE2);

    的LayoutParams PARAMS =新的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);
}
 

图片:

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

解决方案

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:

这篇关于Android的阴影图像效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆