仿QQ头部渐变

布局代码:


     
             
            
                
              
                
                   
                                        
                
                
              
             
              
                
                   
                   
                     
                
                
                
            
            
         
          
          
          
          
          
          
          
          
          
          
          
                       
        
    
       
        
        
        
                
      


核心代码

package com.example.qqactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class MainActivity extends Activity implements OnTouchListener {
    private ScrollView scrollView;
    private LinearLayout bottomLinearLayout;
    private int scrollY;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        scrollView = (ScrollView)findViewById(R.id.sc);
        scrollView.setOnTouchListener(this);
        bottomLinearLayout = (LinearLayout)findViewById(R.id.bottomLinearlayout);
        bottomLinearLayout.getBackground().setAlpha(0);  
      
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
//
//            break;
        case MotionEvent.ACTION_MOVE:
             scrollY=v.getScrollY();
             System.out.println(scrollY);
//             if(scrollY==0){
////                 Toast.makeText(MainActivity.this,"滑动到了顶端 view.getScrollY()="+scrollY, Toast.LENGTH_SHORT).show();
//                    System.out.println("滑动到了顶端 view.getScrollY()="+scrollY);
//                }
//             
//             if((scrollY+height)==scrollViewMeasuredHeight){
//                 Toast.makeText(MainActivity.this,"滑动到了底部 scrollY="+scrollY+"滑动到了底部 height="+height+"滑动到了底部 scrollViewMeasuredHeight="+scrollViewMeasuredHeight, Toast.LENGTH_SHORT).show();
//                    System.out.println("滑动到了底部 scrollY="+scrollY);
//                    System.out.println("滑动到了底部 height="+height);
//                    System.out.println("滑动到了底部 scrollViewMeasuredHeight="+scrollViewMeasuredHeight);
//                  thread.start();
//                  Toast.makeText(MainActivity.this, ""+scrollY, Toast.LENGTH_SHORT).show();
             if(scrollY<=255&&scrollY>=0){
                  bottomLinearLayout.getBackground().setAlpha(scrollY);
             }
             else {
                 bottomLinearLayout.getBackground().setAlpha(255);
            }
           
           
                
            
             
////                    bottomLinearLayout.setAnimation(alphaAnimation);
                    
//                }
            break;
        case MotionEvent.ACTION_UP:
            if(scrollY<=255&&scrollY>=0){
              bottomLinearLayout.getBackground().setAlpha(scrollY);
           }
           else {
             bottomLinearLayout.getBackground().setAlpha(255);
            }
             break;
        default:
            break;
        }
        return false;
    }

    
}

你可能感兴趣的:(仿QQ头部渐变)