图片阴影效果和影子效果


先上实现效果图:

图片阴影效果和影子效果_第1张图片

直接上代码:

[java]  view plain copy print ?
  1. <span style="font-size:16px;">import android.app.Activity;  
  2. import android.content.Context;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapFactory;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Paint;  
  8. import android.graphics.PorterDuff;  
  9. import android.graphics.PorterDuff.Mode;  
  10. import android.graphics.PorterDuffXfermode;  
  11. import android.graphics.Rect;  
  12. import android.graphics.RectF;  
  13. import android.graphics.drawable.Drawable;  
  14. import android.os.Bundle;  
  15. import android.view.View;  
  16.   
  17. public class VirtualKeycode extends Activity {  
  18.     /** Called when the activity is first created. */  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(new ShadeEffect(this));  
  23.     }  
  24.     class ShadeEffect extends View{  
  25.         Paint paint;   
  26.         public ShadeEffect (Context context){  
  27.             super(context);  
  28.             paint = new Paint();//初始化画笔,为后面阴影效果使用。  
  29.             paint.setAntiAlias(true);//去除锯齿。  
  30.             paint.setShadowLayer(5f, 5.0f, 5.0f, Color.BLACK);//设置阴影层,这是关键。  
  31.             paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  32.         }  
  33.         @Override  
  34.         public void onDraw(Canvas canvas){  
  35.             super.onDraw(canvas);  
  36.             int posX = 20;  
  37.             int posY = 50;  
  38.             int PicWidth,PicHegiht;   
  39.               
  40.             Drawable drawable = getResources().getDrawable(R.drawable.button);  
  41.             Drawable dbe = getResources().getDrawable(R.drawable.button).mutate();//如果不调用mutate方法,则原图也会被改变,因为调用的资源是同一个,所有对象是共享状态的。  
  42.             Drawable drawTest = getResources().getDrawable(R.drawable.button);  
  43.             Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.button);  
  44.               
  45.             PicWidth = drawable.getIntrinsicWidth();  
  46.             PicHegiht = drawable.getIntrinsicHeight();  
  47.               
  48.             drawTest.setBounds(posX, (2 * posY) + PicHegiht, posX + PicWidth, (2 * posY) + 2 * PicHegiht );  
  49.             drawable.setBounds(posX,posY,posX+PicWidth,posY+PicHegiht);  
  50.             dbe.setBounds(00, PicWidth, PicHegiht);  
  51.               
  52.             canvas.drawColor(Color.WHITE);//设置画布颜色  
  53.             canvas.save(Canvas.MATRIX_SAVE_FLAG);  
  54.             dbe.setColorFilter(0x7f000000,PorterDuff.Mode.SRC_IN);  
  55.             canvas.translate(posX + (int)(0.9 * PicWidth/2), posY + PicHegiht/2);//图像平移为了刚好在原图后形成影子效果。  
  56.             canvas.skew(-0.9F, 0.0F);//图像倾斜效果。  
  57.             canvas.scale(1.0f, 0.5f);//图像(其实是画布)缩放,Y方向缩小为1/2。  
  58.             dbe.draw(canvas);//此处为画原图像影子效果图,比原图先画,则会在下层。  
  59.             drawable.clearColorFilter();  
  60.             canvas.restore();  
  61.               
  62.             canvas.save(Canvas.MATRIX_SAVE_FLAG);  
  63.             drawable.draw(canvas);//此处为画原图像,由于canvas有层次效果,因此会盖在影子之上。  
  64.             canvas.restore();  
  65.               
  66.             //默认无效果原图  
  67.             canvas.save(Canvas.MATRIX_SAVE_FLAG);  
  68.             drawTest.draw(canvas);  
  69.             canvas.restore();  
  70.               
  71.             //图片阴影效果  
  72.             canvas.save(Canvas.MATRIX_SAVE_FLAG);  
  73.             //Rect rect = new Rect(2*posX + PicWidth, 2*posY + PicHegiht, 2*posX + 2*PicWidth, 2*posY + 2*PicHegiht);//此为理论上的阴影图坐标  
  74.             Rect rect = new Rect(2*posX + PicWidth + 32*posY + PicHegiht + 32*posX + 2*PicWidth - 22*posY + 2*PicHegiht - 2);  
  75.             //由于图片的实际尺寸比显示出来的图像要大一些,因此需要适当更改下大小,以达到较好的效果  
  76.               
  77.             RectF rectF = new RectF(rect);  
  78.             canvas.drawRoundRect(rectF, 10f, 10f, paint);//在原有矩形基础上,画成圆角矩形,同时带有阴影层。  
  79.             canvas.drawBitmap(bmp, 2*posX + PicWidth, 2*posY + PicHegiht, null);//画上原图。  
  80.             canvas.restore();  
  81.         }  
  82.     }  
  83. }</span>  

你可能感兴趣的:(图片阴影效果和影子效果)