Android 倒影實現演算法【搜集】

來源:互聯網
上載者:User

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

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.