您的位置:首页 > 移动开发 > Android开发

Android毛玻璃效果简单实现

2016-11-28 10:57 441 查看
实现思路:将两张图片(一张原图,一张原图模糊后得到的图片)重叠放在相同位置,当手指按下后记录手指位置,根据手势改变上层图片透明度即可

xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/bg" />

<ImageView
android:id="@+id/main_blur_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="100dp"
android:src="@drawable/blur_bg" />

<TextView
android:id="@+id/main_alpha_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>


实现代码

private ImageView mBlurImage;
private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mTextView = (TextView) findViewById(R.id.main_alpha_text);

mBlurImage = (ImageView) findViewById(R.id.main_blur_bg);
mBlurImage.setOnTouchListener(new OnTouchListener() {

private float mLastY;

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
float y = event.getY();
float alphaDelt = (y - mLastY) / 1000;
float alpha = mBlurImage.getAlpha() + alphaDelt;
if (alpha > 1.0) {
alpha = 1.0f;
} else if (alpha < 0.0) {
alpha = 0.0f;
}
mTextView.setText(String.valueOf(alpha));
mBlurImage.setAlpha(alpha);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
});

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐