首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

Android开发:ImageView影子和图层效果

2012-09-28 
Android开发:ImageView阴影和图层效果Android开发:ImageView阴影和图层效果:import?Android.app.Activity

Android开发:ImageView阴影和图层效果

Android开发:ImageView阴影和图层效果:

Android开发:ImageView影子和图层效果

  1. import?Android.app.Activity;??import?Android.content.Context;??
  2. import?Android.graphics.Bitmap;??import?Android.graphics.BitmapFactory;??
  3. import?Android.graphics.Canvas;??import?Android.graphics.Color;??
  4. import?Android.graphics.Paint;??import?Android.graphics.PorterDuff;??
  5. import?Android.graphics.PorterDuff.Mode;??import?Android.graphics.PorterDuffXfermode;??
  6. import?Android.graphics.Rect;??import?Android.graphics.RectF;??
  7. import?Android.graphics.drawable.Drawable;??import?Android.os.Bundle;??
  8. import?Android.view.View;????
  9. public?class?MyCanvasActivity?extends?Activity?{??????/**?Called?when?the?activity?is?first?created.?*/??
  10. ????@Override??????public?void?onCreate(Bundle?savedInstanceState)?{??
  11. ????????super.onCreate(savedInstanceState);??????????setContentView(new?ImageEffect(this));??
  12. ????}????????
  13. ??class?ImageEffect?extends?View{????????Paint?paint;??
  14. ?????public?ImageEffect(Context?context){???????????super(context);??
  15. ?????????paint=?new?Paint();?//初始化画笔,为后面阴影效果使用。????????????paint.setAntiAlias(true);//去除锯齿。???
  16. ?????????paint.setShadowLayer(5f,?5.0f,?5.0f,?Color.BLACK);?//设置阴影层,这是关键。????????????paint.setXfermode(new?PorterDuffXfermode(Mode.SRC_IN));??
  17. ????????????????}??
  18. ?????public?void?onDraw(Canvas?canvas){???????????super.onDraw(canvas);??
  19. ?????????int?posX??=?20;???????????int?posY?=?50;??
  20. ?????????int?PicWidth,PicHeight;?????????????
  21. ????????Drawable?drawable?=?getResources().getDrawable(R.drawable.pic);??????????Drawable?dbe????=?getResources().getDrawable(R.drawable.pic).mutate();//如果不调用mutate方法,则原图也会被改变,因为调用的资源是同一个,所有对象是共享状态的。???
  22. ????????Drawable?drawTest?=?getResources().getDrawable(R.drawable.pic);??????????Bitmap?bmp?=?BitmapFactory.decodeResource(getResources(),?R.drawable.pic);??
  23. ????????PicWidth?=?drawable.getIntrinsicWidth();??????????PicHeight?=?drawable.getIntrinsicHeight();??
  24. ??????????????????drawTest.setBounds(posX,?(2?*?posY)?+?PicHeight,?posX?+?PicWidth,?(2?*?posY)?+?2?*?PicHeight?);??
  25. ????????drawable.setBounds(posX,posY,posX+PicWidth,posY+PicHeight);??????????dbe.setBounds(0,?0,?PicWidth,?PicHeight);??
  26. ??????????????????canvas.drawColor(Color.WHITE);//设置画布颜色???
  27. ????????canvas.save(Canvas.MATRIX_SAVE_FLAG);??????????dbe.setColorFilter(0x7f000000,PorterDuff.Mode.SRC_IN);??
  28. ????????canvas.translate(posX?+?(int)(0.9?*?PicWidth/2),?posY?+?PicHeight/2);//图像平移为了刚好在原图后形成影子效果。???????????canvas.skew(-0.9F,?0.0F);//图像倾斜效果。???
  29. ????????canvas.scale(1.0f,?0.5f);//图像(其实是画布)缩放,Y方向缩小为1/2。???????????dbe.draw(canvas);//此处为画原图像影子效果图,比原图先画,则会在下层。???
  30. ????????drawable.clearColorFilter();??????????canvas.restore();??
  31. ?????????????????canvas.save(Canvas.MATRIX_SAVE_FLAG);??
  32. ????????drawable.draw(canvas);//此处为画原图像,由于canvas有层次效果,因此会盖在影子之上。???????????canvas.restore();??
  33. ??????????????????//默认无效果原图???
  34. ????????canvas.save(Canvas.MATRIX_SAVE_FLAG);??????????drawTest.draw(canvas);??
  35. ????????canvas.restore();????????????
  36. ????????//图片阴影效果???????????Rect?rect?=?new?Rect(2*posX?+?PicWidth?+?3,?2*posY?+?PicHeight?+?3,?2*posX?+?2*PicWidth?-?2,?2*posY?+?2*PicHeight?-?2);??
  37. ????????//由于图片的实际尺寸比显示出来的图像要大一些,因此需要适当更改下大小,以达到较好的效果?????????????
  38. ????????RectF?rectF?=?new?RectF(rect);??????????canvas.drawRoundRect(rectF,?10f,?10f,?paint);//在原有矩形基础上,画成圆角矩形,同时带有阴影层。???
  39. ????????canvas.drawBitmap(bmp,?2*posX?+?PicWidth,?2*posY?+?PicHeight,?null);//画上原图。???????????canvas.restore();??
  40. ????????}????}??
  41. } ?

热点排行