欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 倒影算法的实现代码

程序员文章站 2023-11-16 17:42:58
复制代码 代码如下:public class mirrorview extends view { paint m_paint; int m_nshado...
复制代码 代码如下:

public class mirrorview extends view {
 paint m_paint;
 int m_nshadowh;
 drawable m_dw;
 bitmap m_bitmap;
 //xxxx
 matrix mmatrix;
 int shadowheight;
 public mirrorview(context context, bitmap bitmap) {
  super(context);
  m_bitmap = bitmap;
  _init();
  // todo auto-generated constructor stub
 }
 private void _init() {
  //m_dw = new bitmapdrawable(bitmapfactory.decoderesource(getresources(),  r.drawable.icon));
  m_dw = new bitmapdrawable(m_bitmap);
  m_dw.setbounds(0,0,m_dw.getintrinsicwidth(),m_dw.getintrinsicheight());
  m_nshadowh = m_dw.getintrinsicheight()/1;
  m_paint = new paint(paint.anti_alias_flag);
  lineargradient lg = new lineargradient(0, 0, 0, m_nshadowh, 0xb0ffffff, 0x00000000, shader.tilemode.clamp);
  m_paint.setshader(lg);
  m_paint.setxfermode(new porterduffxfermode(porterduff.mode.multiply)); 
  //xxxx
  mmatrix = new matrix();
 }
 @override
 public void ondraw(canvas canvas) {
  super.ondraw(canvas);
  int nx = 0;
  int ny = 0;
  _drawnormalimg(canvas, nx, ny);
  _drawmirror(canvas, nx, ny);
 }
 private void _drawnormalimg(canvas canvas, int nx, int ny) {
  canvas.save(canvas.matrix_save_flag);
  canvas.translate(nx, ny);
  m_dw.draw(canvas);
  canvas.restore();
 }
 private void _drawmirror(canvas canvas, int nx, int ny) {
  int nw = m_dw.getintrinsicwidth();
  int nh = m_dw.getintrinsicheight();
//  u.dout("nw="+nw+",nh="+nh);
  //xxxx
  shadowheight=nh/2;
  //xxxx
  float[] src={0, nh, nw, nh, nw,nh - m_nshadowh, 0, nh - m_nshadowh};
  float[] dst={ 0, nh, nw, nh,nw, shadowheight, 0, shadowheight };
  //xxxx
  canvas.save();
  mmatrix.setpolytopoly(src, 0, dst, 0, src.length >> 1);
  canvas.concat(mmatrix);
  //draw mirror image 
  canvas.save(canvas.matrix_save_flag);
  canvas.scale(1.0f, -1.0f);
  canvas.translate(nx, -(ny + nh * 2));
  canvas.cliprect(0, nh, nw, nh - m_nshadowh);
  m_dw.draw(canvas);
  canvas.restore();
  //draw mask 
  canvas.save();
  canvas.translate(nx, ny + nh);
  canvas.drawrect(0, 0, nw, m_nshadowh, m_paint);
  canvas.restore();
  //xxxx
  canvas.restore();
 }
}