ImageView的倒影效果

<span style="font-family: Arial, Helvetica, sans-serif;">public class ReflectedImgView extends ImageView {</span>
 private Paint mPaint;
 private Matrix matrix = new Matrix();

 public ReflectedImgView(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs,defStyle);
   init();
 }
 
 public ReflectedImgView(Context context, AttributeSet attrs) {
     super(context,attrs);
     init();
 }
 
 public ReflectedImgView(Context context) {
     super(context);
     init();
 }
 
 public void init(){
   
    setScaleType(ScaleType.MATRIX);
   
    // Setup the paint.
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setColor(0xFFFF0000);
    mPaint.setStrokeWidth(0.0f);
 
    // Destination (DST) is drawn by the parent, and should be retained.
    mPaint.setXfermode(new PorterDuffXfermode(
      android.graphics.PorterDuff.Mode.DST_IN));
 
 }

 @Override
 protected void onLayout(boolean changed, int left, int top, int right,
   int bottom) {
  super.onLayout(changed, left, top, right, bottom);

  // It's recommended not to create a Shader in the basic layout calls.
  // Linear gradient from Transparent to Black, from top to bottom.
  // Note: We rotated the image using a transformation.
  // Hence the colors will be opposite.
  LinearGradient gradient = new LinearGradient(0, 0, 0, bottom - top,
    0x0, 0xFF000000, TileMode.CLAMP);

  // Set the gradient as the shader.
  mPaint.setShader(gradient);
 
  matrix.reset();
  matrix.postScale(1, -1);
  matrix.postTranslate(0, bottom-top);
 
  updateImgMatrix();
 }

 @Override
 public void setImageDrawable(Drawable drawable) {
  super.setImageDrawable(drawable);
  updateImgMatrix();
 }

 private void updateImgMatrix() {
  if(getDrawable() instanceof BitmapDrawable && getWidth() > 0){
   BitmapDrawable bd = (BitmapDrawable) getDrawable();
   if(bd.getIntrinsicWidth()>0){
    float scale = (float)getWidth()/bd.getIntrinsicWidth();
    Matrix matrix = new Matrix();
    //matrix.reset();
    matrix.postScale(scale, scale);
    if(bd.getIntrinsicHeight()* scale > getHeight()){
     matrix.postTranslate(0, getHeight() - bd.getIntrinsicHeight()*scale);
    }
    setImageMatrix(matrix);
   }
  }
 
 }

 @Override
 protected void onDraw(Canvas canvas) {
  // Save the canvas. All PorterDuff operations should be done in a
  // offscreen bitmap.
  int count = canvas.saveLayer(0, 0, canvas.getWidth(),
    canvas.getHeight(), null, Canvas.MATRIX_SAVE_FLAG
      | Canvas.CLIP_SAVE_FLAG
      | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
      | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
      | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
 
  canvas.concat(matrix);
  // Do a default draw.
  super.onDraw(canvas);

  // Draw the paint (that has a shader set), on top of the image
  // drawn by the parent (ImageView).
  // Note: This works only on ICS. For pre-ICS phones, create a bitmap and
  // draw on it, like mentioned in CanvasDelegate linked below.
  canvas.drawPaint(mPaint);

  // Restore the canvas.
  canvas.restoreToCount(count);
 }

}</span>


XML中的引用

<span style="font-size:14px;"><com.example.daoying.ReflectedImgView
        android:id="@+id/img_tv_reflect"
        android:layout_width="150dp"
        android:layout_height="55dp"
        android:layout_below="@id/img_tv_name"
        android:layout_marginTop="4px"
        android:alpha="0.7"
        android:focusable="false" /></span>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值