Android 给scrollView添加毛玻璃效果的标题栏
2015-12-25 18:00
369 查看
效果如下
![](http://img.blog.csdn.net/20151225180032353?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
首先是布局文件,头部的标题栏是个压在scrollview上面的自定义控件
下面是自定义的scrollVIew,其实就是把里面探测scrollView滚动的方法给暴露到监听器上去
然后是最主要的activity
这里用到的自定义图片控件和模糊工具类请到我之前的博客去查看
首先是布局文件,头部的标题栏是个压在scrollview上面的自定义控件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/window" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.npi.blureffect.ScrollViewActivity" > <com.npi.blureffect.CustomScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="Button" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" /> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="RadioButton" /> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" /> <ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="ToggleButton" /> <SeekBar android:id="@+id/seekBar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" /> <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <CalendarView android:id="@+id/calendarView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="50dp" /> </LinearLayout> </com.npi.blureffect.CustomScrollView> <com.npi.blureffect.ScrollableImageView android:id="@+id/blurred_image_header" android:layout_width="match_parent" android:layout_height="50dp" android:background="#e8e8e8" /> </RelativeLayout>
下面是自定义的scrollVIew,其实就是把里面探测scrollView滚动的方法给暴露到监听器上去
package com.npi.blureffect; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.widget.ScrollView; public class CustomScrollView extends ScrollView { private ScrollViewListener scrollViewListener; public CustomScrollView(Context context) { super(context); // TODO Auto-generated constructor stub } public interface ScrollViewListener { void onScrollChanged(CustomScrollView scrollView, int x, int y, int oldx, int oldy); } public CustomScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public CustomScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public void setScrollViewListener(ScrollViewListener scrollViewListener) { this.scrollViewListener = scrollViewListener; } @Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { super.onScrollChanged(x, y, oldx, oldy); if (scrollViewListener != null) { scrollViewListener.onScrollChanged(this, x, y, oldx, oldy); } // Log.i("Alex", "$$$x是:"+x+" oldx是:"+oldx+" Y是"+y+"oldY是"+oldy); } }
然后是最主要的activity
package com.npi.blureffect; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import com.npi.blureffect.CustomScrollView.ScrollViewListener; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ScrollView; public class ScrollViewActivity extends Activity { ScrollableImageView header; CustomScrollView scrollView ; Bitmap screen; <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState) { <span style="white-space:pre"> </span>super.onCreate(savedInstanceState); <span style="white-space:pre"> </span>setContentView(R.layout.activity_scroll_view); <span style="white-space:pre"> </span>header = (ScrollableImageView) findViewById(R.id.blurred_image_header); <span style="white-space:pre"> </span>scrollView = (CustomScrollView) findViewById(R.id.scrollView1); <span style="white-space:pre"> </span>initScrollView(header, scrollView); } <span style="white-space:pre"> </span>public void initScrollView(final ScrollableImageView header,final CustomScrollView scrollView){ <span style="white-space:pre"> </span>scrollView.setScrollViewListener(new ScrollViewListener() { <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onScrollChanged(CustomScrollView scrollView, int x, int y, <span style="white-space:pre"> </span>int oldx, int oldy) { <span style="white-space:pre"> </span>// TODO Auto-generated method stub <span style="white-space:pre"> </span>Log.i("Alex", "x是:"+x+" oldx是:"+oldx+" Y是"+y+"oldY是"+oldy); <span style="white-space:pre"> </span>header.handleScroll(y, 0); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>scrollView.post(new Runnable() { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void run() { <span style="white-space:pre"> </span>// TODO Auto-generated method stub <span style="white-space:pre"> </span>screen = getBitmapByView(scrollView); <span style="white-space:pre"> </span>header.setoriginalImage(screen); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); } <span style="white-space:pre"> </span>/** * 截取scrollview的屏幕 * **/ public static Bitmap getBitmapByView(ScrollView scrollView) { int h = 0; Bitmap bitmap = null; // 获取listView实际高度 for (int i = 0; i < scrollView.getChildCount(); i++) { h += scrollView.getChildAt(i).getHeight(); } Log.d("Alex", "实际高度:" + h); Log.d("Alex", " 高度:" + scrollView.getHeight()); // 创建对应大小的bitmap bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); scrollView.draw(canvas); return bitmap; } }
这里用到的自定义图片控件和模糊工具类请到我之前的博客去查看
相关文章推荐
- Android Studio导入第三方类库的方法
- Android调用系统相机拍照并且裁剪指定大小
- Android 混淆后在友盟中无定位错误位置 Unknown Source
- Android学习 之 ColorStateList按钮文字变色
- Android Studio中如何打JAR包
- Android 悬浮窗口
- 从高德 SDK 学习 Android 动态加载资源
- android开发之路-----------转自任玉刚大神
- Android编程技巧之最佳Activity数据传递
- RecyclerView-FlexibleDivider—RecyclerView分割线的Android类库
- AndroidStudio怎样导入jar包
- sharesdk for android集成调试的几个问题
- [转]Android之Context和Activity互相转换
- Android开发之触摸事件处理机制详解
- Battery workflow in Android 2.3
- Android Activity 和 ViewGroup中事件触发和传递机制
- Android开发学习:[15]自动导入包
- Android事件机制:事件传递和消费
- android selector 使用中常见的问题
- 总结android日常用到的注意点