android反射作用,Android对View进行包裹, 实现镜面反射效果

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

效果图

32b29cd5c0cfeeea40c90ba9ec564164.png

/**

*

* @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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值