2016-09-20 72 views
0

我想要一個視圖應該是浮動在應用程序的活動,而不是其他應用程序。如何繪製一個視圖,該視圖應該在同一應用中的所有活動上浮動?

我在活動c.Activity中使用圖像視圖c在兩個活動a,b之上。我有一個按鈕來最小化活動c中的活動。一旦我最小化,imageview應該浮動在活動b上。即使我關閉了活動b,它應該覆蓋活動a。如何實現這一目標?

+0

您可以添加圖片,以便更快掌握? –

+0

你不能有相同的按鈕浮動在不同的活動,但你可以克隆每個活動的按鈕,並顯示它 –

回答

0

好的,這裏是工作流程: 創建另一個活動d。讓我們打電話給FloatActivity。

第一:從最顯着的角度改變活動的主題。

<activity 
    android:name=".FloatActivity" 
    android:label="Float Button" 
    <!-- Use Translucent theme to get transparent activity background 
    and NoTitleBar to avoid super old style title bar ;) --> 
    android:theme="@android:style/Theme.Translucent.NoTitleBar"> 
</activity> 

第二:現在編輯的FloatActivity

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/pop" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/transparent"> 

    <ImageButton 
     android:id="@+id/popButton" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:background="#000"/> 
</RelativeLayout> 

3日的.xml:現在代碼的Java現在

public class FloatActivity extends Activity implements View.OnTouchListener{ 

    private ImageButton floatButton; 

    private int previousFingerPosition = 0; 
    private int baseLayoutPosition = 0; 
    private int defaultViewHeight; 

    private boolean isClosing = false; 
    private boolean isScrollingUp = false; 
    private boolean isScrollingDown = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_popup); 
     floatButton = (ImageButton) findViewById(R.id.popButton); 
     floatButton.setOnTouchListener(this); 
    } 


    public boolean onTouch(View view, MotionEvent event) { 

     // Get finger position on screen 
     final int Y = (int) event.getRawY(); 

     // Switch on motion event type 
     switch (event.getAction() & MotionEvent.ACTION_MASK) { 

      case MotionEvent.ACTION_DOWN: 
       // save default base layout height 
       defaultViewHeight = baseLayout.getHeight(); 

       // Init finger and view position 
       previousFingerPosition = Y; 
       baseLayoutPosition = (int) baseLayout.getY(); 
       break; 

      case MotionEvent.ACTION_UP: 
       // If user was doing a scroll up 
       if(isScrollingUp){ 
        // Reset baselayout position 
        baseLayout.setY(0); 
        // We are not in scrolling up mode anymore 
        isScrollingUp = false; 
       } 

       // If user was doing a scroll down 
       if(isScrollingDown){ 
        // Reset baselayout position 
        baseLayout.setY(0); 
        // Reset base layout size 
        baseLayout.getLayoutParams().height = defaultViewHeight; 
        baseLayout.requestLayout(); 
        // We are not in scrolling down mode anymore 
        isScrollingDown = false; 
       } 
       break; 
      case MotionEvent.ACTION_MOVE: 
       if(!isClosing){ 
        int currentYPosition = (int) baseLayout.getY(); 

        // If we scroll up 
        if(previousFingerPosition >Y){ 
         // First time android rise an event for "up" move 
         if(!isScrollingUp){ 
          isScrollingUp = true; 
         } 

        // Has user scroll down before -> view is smaller than it's default size -> resize it instead of change it position 
        if(baseLayout.getHeight()<defaultViewHeight){ 
         baseLayout.getLayoutParams().height = baseLayout.getHeight() - (Y - previousFingerPosition); 
         baseLayout.requestLayout(); 
        } 
        else { 
         // Has user scroll enough to "auto close" popup ? 
         if ((baseLayoutPosition - currentYPosition) > defaultViewHeight/4) { 
          closeUpAndDismissDialog(currentYPosition); 
          return true; 
         } 

         // 
        } 
        baseLayout.setY(baseLayout.getY() + (Y - previousFingerPosition)); 

       } 
       // If we scroll down 
       else{ 

        // First time android rise an event for "down" move 
        if(!isScrollingDown){ 
         isScrollingDown = true; 
        } 

        // Has user scroll enough to "auto close" popup ? 
        if (Math.abs(baseLayoutPosition - currentYPosition) > defaultViewHeight/2) 
        { 
         closeDownAndDismissDialog(currentYPosition); 
         return true; 
        } 

        // Change base layout size and position (must change position because view anchor is top left corner) 
        baseLayout.setY(baseLayout.getY() + (Y - previousFingerPosition)); 
        baseLayout.getLayoutParams().height = baseLayout.getHeight() - (Y - previousFingerPosition); 
        baseLayout.requestLayout(); 
       } 

       // Update position 
       previousFingerPosition = Y; 
      } 
      break; 
     } 
     return true; 
    } 
} 

,最大限度地減少您的活動剛開始活動之前,d(FloatActivity )。

+0

我需要像facebook聊天頭部頭像。但它應該覆蓋在我的應用程序活動中,而不是其他人。 – Ayyappan

相關問題