码迷,mamicode.com
首页 > 移动开发 > 详细

Android对View进行包裹, 实现镜面发射效果

时间:2015-08-11 12:26:38      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

这个在TV上用的挺多的, 网上很多都是对ImageView的图片做的, 感觉不是很好, 这里实现了一个可以对任何View做倒影的.

效果图

技术分享

/**
 * 
 * @className: MirrorView
 * @description: 对普通View做封装, 让这个View出现倒影效果, 注意,使用这个View之后,
 *               会让原来的View的Height扩大REFHEIGHT高度, 用来显示反射镜面,
 * @author: gaoshuai
 * @date: 2015年8月11日 上午11:31:16
 */
public class MirrorView extends FrameLayout
{
    private View mContentView;
    protected boolean mHasReflection = true;
    private static int REFHEIGHT = -1;
    public static Paint RefPaint = null;
    
    private Bitmap mReflectBitmap;
    private Canvas mReflectCanvas;
    
    public MirrorView(Context context) {
        super(context);
        
        if (REFHEIGHT == -1)
            REFHEIGHT = 100;
        if (RefPaint == null) {
            RefPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            RefPaint.setShader(new LinearGradient(0, 0, 0, REFHEIGHT, new int[] { 0x77000000, 0x66AAAAAA, 0x0500000, 0x00000000 }, new float[] { 0.0f, 0.1f, 0.9f, 1.0f }, Shader.TileMode.CLAMP));
            RefPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        }
        
        this.setClickable(true);
    }
    
    public void setContentView(View view) {
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.bottomMargin = REFHEIGHT;
        mContentView = view;
        addView(view, lp);
    }
    
    public View getContentView() {
        return mContentView;
    }
    
    public void setReflection(boolean ref) {
        mHasReflection = ref;
    }
    
    @Override
    public boolean performClick() {
        return mContentView.performClick();
    }
    
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (mHasReflection) {
            if (mReflectBitmap == null) {
                mReflectBitmap = Bitmap.createBitmap(mContentView.getWidth(), REFHEIGHT, Bitmap.Config.ARGB_8888);
                mReflectCanvas = new Canvas(mReflectBitmap);
            }
            drawReflection(mReflectCanvas, mContentView); //绘制这个View的反射效果
            canvas.save();
            int dy = mContentView.getBottom();
            int dx = mContentView.getLeft();
            canvas.translate(dx, dy);
            canvas.drawBitmap(mReflectBitmap, 0, 0, null);
            canvas.restore();
        }
    }
    
    public Bitmap getReflectBitmap() {
        return mReflectBitmap;
    }
    
    public void drawReflection(Canvas canvas, View view) {
        canvas.save();
        canvas.clipRect(0, 0, view.getWidth(), REFHEIGHT);
        canvas.save();
        canvas.scale(1, -1);
        canvas.translate(0, -view.getHeight());
        view.draw(canvas);
        canvas.restore();
        canvas.drawRect(0, 0, view.getWidth(), REFHEIGHT, RefPaint);
        canvas.restore();
    }
}


测试Activity


public class MainActivity extends Activity
{
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FrameLayout container = (FrameLayout) findViewById(R.id.test);
        MirrorView mirrorItemView = new MirrorView(this);
        TextView textView = new TextView(this);
        textView.setWidth(300);
        textView.setText("我就是想测试一下镜子View");
        textView.setBackgroundResource(R.drawable.ic_launcher);
        mirrorItemView.setContentView(textView);
        container.addView(mirrorItemView);
    }
}



Android对View进行包裹, 实现镜面发射效果

标签:

原文地址:http://my.oschina.net/sfshine/blog/490626

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!