美文网首页
自定义ScrollView实现

自定义ScrollView实现

作者: OlivineVip | 来源:发表于2016-06-12 20:51 被阅读1140次

    用于监听ScrollView的滚动,来改变某一布局的透明度

    引用
    (http://blog.csdn.net/sinat_21283073/article/details/51315926)

    1 自定义scrollView

    `
    public class ObservableScrollView extends ScrollView {

    public interface ScrollViewListener {  
        void onScrollChanged(ObservableScrollView scrollView, int x, int y,  
                int oldx, int oldy);  
    
    }  
    
    private ScrollViewListener scrollViewListener = null;  
    
    public ObservableScrollView(Context context) {  
        super(context);  
    }  
    
    public ObservableScrollView(Context context, AttributeSet attrs,  
            int defStyle) {  
        super(context, attrs, defStyle);  
    }  
    
    public ObservableScrollView(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);  
        }  
    } 
    

    }
    `

    2 使用

    `
    public class MainActivity extends Activity implements ScrollViewListener{

    private ObservableScrollView scrollView;  
      
    private ListView listView;  
      
    private ImageView imageView;  
      
    private TextView textView;  
      
    private int imageHeight;  
    
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        scrollView = (ObservableScrollView) findViewById(R.id.scrollview);  
        listView = (ListView) findViewById(R.id.listview);  
        imageView = (ImageView) findViewById(R.id.imageview);  
        textView = (TextView) findViewById(R.id.textview);  
        initListeners();  
        initData();  
    }  
      
    private void initListeners() {  
        // 获取顶部图片高度后,设置滚动监听  
        ViewTreeObserver vto = imageView.getViewTreeObserver();  
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
            @Override  
            public void onGlobalLayout() {  
                imageView.getViewTreeObserver().removeGlobalOnLayoutListener(  
                        this);  
                imageHeight = imageView.getHeight();  
    
                scrollView.setScrollViewListener(MainActivity.this);  
            }  
        });  
    }  
    
    
    
    private void initData() {  
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.data));  
        listView.setAdapter(adapter);  
    }  
    
    
    
    @Override  
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y,  
            int oldx, int oldy) {  
        // TODO Auto-generated method stub  
        // Log.i("TAG", "y--->" + y + "    height-->" + height);  
        if (y <= 0) {  
            textView.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相关工具获得,或者美工提供  
        } else if (y > 0 && y <= imageHeight) {  
            float scale = (float) y / imageHeight;  
            float alpha = (255 * scale);  
            // 只是layout背景透明(仿知乎滑动效果)  
            textView.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));  
        } else {  
            textView.setBackgroundColor(Color.argb((int) 255, 227, 29, 26));  
        }  
    }  
    

    }
    `

    相关文章

      网友评论

          本文标题:自定义ScrollView实现

          本文链接:https://www.haomeiwen.com/subject/yxaddttx.html