仿QQ头部渐变

2017-02-24  本文已影响40人  往事一块六毛八

布局代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.tongchengdemo.MainActivity$PlaceholderFragment" >
    <ScrollView 
        android:id="@+id/sc"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/black"
        > 
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"           
            >     
            <LinearLayout
                android:orientation="vertical" 
                android:background="#ffffff"
                android:layout_width="fill_parent"
                android:layout_height="150dip">
                <LinearLayout 
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff"
                >
            <ImageView 
                android:layout_width="fill_parent"
                android:layout_height="70dip"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher"/>  
              <ImageView 
                android:layout_width="fill_parent"
                android:layout_height="70dip"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher"/>  
                 <ImageView 
                android:layout_width="fill_parent"
                android:layout_height="70dip"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher"/>  
                 <ImageView 
                android:layout_width="fill_parent"
                android:layout_height="70dip"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher"/>                       
                
                
            </LinearLayout>  
             <LinearLayout 
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff"
                >
            <ImageView 
                android:layout_width="fill_parent"
                android:layout_height="70dip"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher"/>  
              <ImageView 
                android:layout_width="fill_parent"
                android:layout_height="70dip"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher"/>  
                 <ImageView 
                android:layout_width="fill_parent"
                android:layout_height="70dip"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher"/>  
                 <ImageView 
                android:layout_width="fill_parent"
                android:layout_height="70dip"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher"/>  
                     
                
                
            </LinearLayout>    
            </LinearLayout>
            
         <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
          <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
          <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
          <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
          <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
          <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
          <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
          <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
          <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
          <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
          <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
          <TextView 
             android:layout_width="fill_parent"
             android:layout_height="50dip"
             android:textColor="#ffffff"
             android:text="1234567"/>
                       
        </LinearLayout>
    </ScrollView>
    <LinearLayout 
        android:id="@+id/bottomLinearlayout"
        android:orientation="horizontal"
        android:background="#4ed2e3"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:layout_gravity="top">   
        
        
        <!-- <ImageView
            android:layout_weight="5"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="#4ed2e3"
            android:padding="10dip"
            
            /> 
        <ImageView
            android:layout_weight="5"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"          
            android:padding="10dip"
            />  -->
                
    </LinearLayout>  
</FrameLayout>

核心代码

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;
    }

    
}

上一篇下一篇

猜你喜欢

热点阅读