2014-05-02 2 views
8

एंड्रॉइड में क्रॉसफ़ेडिंग के संबंध में बहुत सारे प्रश्न हैं, लेकिन उनमें सभी एनिमेशन शामिल हैं। मेरा प्रश्न ViewPager के OnPageChangeListener का उपयोग करके क्रॉसफ़ेडिंग के बारे में है।OnPageChangeListener अल्फा क्रॉसफ़ेडिंग

मेरे पास एक व्यूपेजर है जिसमें असीमित दृश्य हो सकते हैं, लेकिन व्यवहार में लगभग 6 या 7 विचारों का उपयोग किया जाता है। वहां बहुत कुछ नहीं चल रहा है।

व्यूपेजर में प्रत्येक दृश्य में पृष्ठभूमि बिटमैप होता है जिसे शेष दृश्य के साथ स्क्रॉल करने के बजाय अगले (या पिछले) दृश्य की पृष्ठभूमि के साथ तय और क्रॉसफ़ेड किया जाना चाहिए।

इसे प्राप्त करने के लिए मैंने पृष्ठभूमि को डीकॉप्ल किया और एक ऐरेलिस्ट में जोड़ा और उन्हें बाद में ImageViews को असाइन किया। लेकिन चूंकि मैं संभव है कि मेरी गतिविधि कई ImageViews मैं निम्नलिखित संरचना के बारे में सोचा के साथ खत्म हो जाएगा जोखिम नहीं करना चाहती:

<FrameLayout 
    android:id="@+id/backgroundContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/bottomImage" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:scaleType="center" /> 

    <ImageView 
     android:id="@+id/middleImage" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:scaleType="center" /> 

    <ImageView 
     android:id="@+id/topImage" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:scaleType="center" /> 

</FrameLayout> 

फिर एक OnPageChangeListener ImageViews को पृष्ठभूमि आवंटित करने के लिए ViewPager को सौंपा गया है।

@Override 
public void onPageSelected(int position) { 
    MyLog.i(TAG, "PAGE SELECTED: " + position); 

    if(position == 0) { 
     _bottomBackground.setImageBitmap(null); 
     _topBackground.setImageBitmap(_backgroundStack.get(position+1)); 
    } else if (position == NUM_ITEMS-1) { 
     _bottomBackground.setImageBitmap(_backgroundStack.get(position-1)); 
     _topBackground.setImageBitmap(null); 
    } else { 
     _bottomBackground.setImageBitmap(_backgroundStack.get(position-1)); 
     _topBackground.setImageBitmap(_backgroundStack.get(position+1)); 
    } 

    _middleBackground.setImageBitmap(_backgroundStack.get(position));    

    // Make the top front background transparent 
    _topBackground.setAlpha(0f); 
    _currentBackgroundPosition = position; 
} 

यह ठीक काम करता है अगर मुझे पृष्ठभूमि को स्वैप करना पसंद होता। मैं पृष्ठभूमि को एक दूसरे में फीका करना चाहता हूं जबकि उपयोगकर्ता ViewPager को स्वाइप करता है। मुझे आगे की स्क्रॉल काम करने के लिए फीका मिला है, लेकिन मुझे समझ में नहीं आ रहा है कि पिछड़े स्क्रॉल के लिए फीका किसी भी तरह का अच्छा परिणाम नहीं देता है। पिछड़े स्क्रॉल के दौरान मध्य पृष्ठभूमि को नीचे की पृष्ठभूमि में फीका जाना चाहिए।

मुझे डर है कि मुझे कुछ याद आ रहा है। मैं नीचे की पृष्ठभूमि के अल्फा को कभी नहीं बदल रहा हूं, लेकिन लॉग परिणाम हमेशा मध्य पृष्ठभूमि के लिए getAlpha() के लिए सटीक समान मान दिखाते हैं।

@Override 
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
    if(_currentBackgroundPosition == position) { 
     // scroll forward 
     _topBackground.setAlpha(positionOffset) 
    } else { 
     //scroll backward 
     _middleBackground.setAlpha(positionOffset);    
    } 

    MyLog.i(TAG, "Bottom BackgroundAlpha: " + _bottomBackground.getAlpha()); 
    MyLog.i(TAG, "Middle BackgroundAlpha: " + _middleBackground.getAlpha()); 
    MyLog.i(TAG, "Top BackgroundAlpha: " + _topBackground.getAlpha()); 
} 

और प्रतीक्षा करें! एक और बात है कि मैं वास्तव में कैसे ठीक करना है यह समझने में सक्षम नहीं हूं। हालांकि आगे स्क्रॉल फीका काम कर रहा है। पृष्ठभूमि में एक सुपर शॉर्ट झिलमिलाहट है। मुझे लगता है कि यह हो रहा है क्योंकि मैंने onPageSelected विधि स्थापित की है।

क्या कोई अन्य तरीका है कि मैं इस व्यवहार को कैसे बना/ठीक कर सकता हूं?

उत्तर

25

ViewPager.PageTransformer आपका मित्र है। मैं आपके द्वारा किए गए प्रयासों के लिए एक अलग दृष्टिकोण लेने जा रहा हूं, लेकिन इसके परिणामस्वरूप मैं वांछित परिणाम को बिस्तर पर समझने के लिए समझता हूं - बाएं/दाएं स्वाइप करने से सामग्री स्वाइप हो जाती है, लेकिन दो पृष्ठभूमि छवियों के बीच फीका होता है जो हिलते नहीं हैं।

प्रत्येक FragmentViewPager में इतनी तरह एक लेआउट होगा:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:id="@+id/image_view" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:scaleType="center" /> 
    <LinearLayout 
     android:id="@+id/content_area" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <!-- content goes here --> 
    </LinearLayout> 
</FrameLayout> 

और आप एक PageTransformer उस स्थिति यह स्वाइप कर दिया गया है के आधार लेआउट manipulates पैदा करेगा:

public class CustomPageTransformer implements ViewPager.PageTransformer { 
    public void transformPage(View view, float position) { 
     int pageWidth = view.getWidth(); 
     View imageView = view.findViewById(R.id.image_view); 
     View contentView = view.findViewById(R.id.content_area); 

     if (position < -1) { // [-Infinity,-1) 
      // This page is way off-screen to the left 
     } else if (position <= 0) { // [-1,0] 
      // This page is moving out to the left 

      // Counteract the default swipe 
      view.setTranslationX(pageWidth * -position); 
      if (contentView != null) { 
       // But swipe the contentView 
       contentView.setTranslationX(pageWidth * position); 
      } 
      if (imageView != null) { 
       // Fade the image in 
       imageView.setAlpha(1 + position); 
      } 

     } else if (position <= 1) { // (0,1] 
      // This page is moving in from the right 

      // Counteract the default swipe 
      view.setTranslationX(pageWidth * -position); 
      if (contentView != null) { 
       // But swipe the contentView 
       contentView.setTranslationX(pageWidth * position); 
      } 
      if (imageView != null) { 
       // Fade the image out 
       imageView.setAlpha(1 - position); 
      } 
     } else { // (1,+Infinity] 
      // This page is way off-screen to the right 
     } 
    } 
} 

और अंत में इस PageTransformer को अपने ViewPager तक बढ़ाएं:

mViewPager.setPageTransformer(true, new CustomPageTransformer()); 

मैंने इसे किसी मौजूदा ऐप में परीक्षण किया है और यह तब तक अच्छी तरह से काम करता है जब तक कि टुकड़े लेआउट में पारदर्शी पृष्ठभूमि हो।

+0

आपने मेरा दिन बनाया! अगर यह अकेले खड़े होकर काम करता, तो अब इसे मेरे प्रोजेक्ट में लागू कर देगा। धन्यवाद! – StingRay5

+0

बढ़िया, खुशी है कि यह आपकी मदद की। बक्षीस के लिए धन्यवाद। – myanimal

+0

आपने मुझे बहुत समय बचाया। ग्राहक मुझे दबा रहा है! धन्यवाद दोस्त! –

संबंधित मुद्दे