CanPHP框架技术讨论

导航

 
  1. 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 MyCanvasActivity 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 ImageEffect(this));  
  23.     }  
  24.       
  25.   class ImageEffect extends View{  
  26.       Paint paint;  
  27.      public ImageEffect(Context context){  
  28.          super(context);  
  29.          paint= new Paint(); //初始化画笔,为后面阴影效果使用。   
  30.          paint.setAntiAlias(true);//去除锯齿。   
  31.          paint.setShadowLayer(5f, 5.0f, 5.0f, Color.BLACK); //设置阴影层,这是关键。   
  32.          paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  33.            
  34.      }  
  35.      public void onDraw(Canvas canvas){  
  36.          super.onDraw(canvas);  
  37.          int posX  = 20;  
  38.          int posY = 50;  
  39.          int PicWidth,PicHeight;  
  40.            
  41.         Drawable drawable = getResources().getDrawable(R.drawable.pic);  
  42.         Drawable dbe    = getResources().getDrawable(R.drawable.pic).mutate();//如果不调用mutate方法,则原图也会被改变,因为调用的资源是同一个,所有对象是共享状态的。   
  43.         Drawable drawTest = getResources().getDrawable(R.drawable.pic);  
  44.         Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic);  
  45.         PicWidth = drawable.getIntrinsicWidth();  
  46.         PicHeight = drawable.getIntrinsicHeight();  
  47.           
  48.         drawTest.setBounds(posX, (2 * posY) + PicHeight, posX + PicWidth, (2 * posY) + 2 * PicHeight );  
  49.         drawable.setBounds(posX,posY,posX+PicWidth,posY+PicHeight);  
  50.         dbe.setBounds(00, PicWidth, PicHeight);  
  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 + PicHeight/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.         Rect rect = new Rect(2*posX + PicWidth + 32*posY + PicHeight + 32*posX + 2*PicWidth - 22*posY + 2*PicHeight - 2);  
  73.         //由于图片的实际尺寸比显示出来的图像要大一些,因此需要适当更改下大小,以达到较好的效果   
  74.           
  75.         RectF rectF = new RectF(rect);  
  76.         canvas.drawRoundRect(rectF, 10f, 10f, paint);//在原有矩形基础上,画成圆角矩形,同时带有阴影层。   
  77.         canvas.drawBitmap(bmp, 2*posX + PicWidth, 2*posY + PicHeight, null);//画上原图。   
  78.         canvas.restore();  
  79.         }  
  80.   }  
  81. }  
posted on 2013-08-16 13:59  創丗仼  阅读(3426)  评论(0编辑  收藏  举报