Android 中的 Viewflipper 不会一直翻转

发布于 2024-12-04 05:13:42 字数 1517 浏览 2 评论 0原文

我有两个视图(广告),当一个广告网络没有广告显示时,该视图应该翻转到 admob 作为备份。第一个视图是 inmobi(不确定这是否有任何相关性)。如果我通过打开测试广告来强制 inmobi 广告失败,视图上的广告将会翻转并显示 admob 广告,但是当打开实时广告时,即使收到失败的广告回调,视图也不会翻转。请帮忙。

@Override
public void adRequestFailed(InMobiAdView arg0) {
    // TODO Auto-generated method stub
    Log.v("","inmobi ad request failed");

    // Initiate a generic request to load it with an ad
    loadAdmob();
    ViewFlipper vf = (ViewFlipper) findViewById(R.id.ads);

    vf.showNext();
}

private void loadAdmob() {

    AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxx");

    LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
    // Add the adView to it
    layout.addView(adView);
    adView.loadAd(new AdRequest());

}

和 XML

<ViewFlipper android:id="@+id/ads"
    android:layout_width="320dip"
    android:layout_height="50dip">

    <LinearLayout
           android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <com.inmobi.androidsdk.impl.InMobiAdView
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id = "@+id/adview"
     />
     </LinearLayout>


     <LinearLayout
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_gravity="center"
    android:id="@+id/mainLayout" ></LinearLayout>

    </ViewFlipper>

I have two views (ads) and when one ad network has no ad to show the view is supposed to flip to admob as a backup. The first view is inmobi (not sure if that has any relevance). If I force the inmobi ad to fail by turning test ads on the view will flip and show the admob ad but when live ads are turned on, the view never flips even when the failed ad callback is received. Please help.

@Override
public void adRequestFailed(InMobiAdView arg0) {
    // TODO Auto-generated method stub
    Log.v("","inmobi ad request failed");

    // Initiate a generic request to load it with an ad
    loadAdmob();
    ViewFlipper vf = (ViewFlipper) findViewById(R.id.ads);

    vf.showNext();
}

private void loadAdmob() {

    AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxx");

    LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
    // Add the adView to it
    layout.addView(adView);
    adView.loadAd(new AdRequest());

}

And the XML

<ViewFlipper android:id="@+id/ads"
    android:layout_width="320dip"
    android:layout_height="50dip">

    <LinearLayout
           android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <com.inmobi.androidsdk.impl.InMobiAdView
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id = "@+id/adview"
     />
     </LinearLayout>


     <LinearLayout
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_gravity="center"
    android:id="@+id/mainLayout" ></LinearLayout>

    </ViewFlipper>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

じее 2024-12-11 05:13:42

我还没有看到这个问题。但如果您需要在布局之间翻转,我可以提供我自己的代码如何使用翻转功能。首先,您应该为未来的布局创建一个单独的页面,例如 res.layout 文件夹中的 page_1.xml

 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@color/GreenColor"
  >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="1"
        android:textSize="120dip">
    </TextView>
</LinearLayout>

完成后,描述一个包含外部 xml 页面的 ma​​in.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ViewFlipper android:id="@+id/ViewFlipper01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    >
    <include android:id="@+id/libraryView1"  layout="@layout/page_1" />
    <include android:id="@+id/libraryView2"  layout="@layout/page_2" />
    <include android:id="@+id/libraryView3"  layout="@layout/page_3" />
</ViewFlipper>
</RelativeLayout>

不要忘记在类文件中声明 vf=(ViewFlipper)findViewById(R.id.ViewFlipper01);
下一步,您应该重写 OnTouchEvent 来计算新坐标并显示翻转动画:

   @Override
    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                oldTouchValueX = touchevent.getX();
                oldTouchValueY = touchevent.getY();
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                //if(this.searchOk==false) return false;
                float currentX = touchevent.getX();
                float currentY = touchevent.getY();

                float deltaX = getAbs(oldTouchValueX - currentX);
                float deltaY = getAbs(oldTouchValueY - currentY);

                if (deltaX >= deltaY)
                {
                    if (oldTouchValueX < currentX)
                    {
                        vf.setInAnimation(inFromLeftAnimation());
                        vf.setOutAnimation(outToRightAnimation());
                        vf.showNext();
                    }
                    if (oldTouchValueX > currentX)
                    {
                        vf.setInAnimation(inFromRightAnimation());
                        vf.setOutAnimation(outToLeftAnimation());
                        vf.showPrevious();
                    } 
                } //if deltaX
                else
                    {
                    if (oldTouchValueY < currentY)
                    {
                        vf.setInAnimation(inFromUpAnimation());
                        vf.setOutAnimation(outToDownAnimation());
                        vf.showNext();

                    }
                    if (oldTouchValueY > currentY)
                    {
                        vf.setInAnimation(inFromDownAnimation());
                        vf.setOutAnimation(outToUpAnimation());
                        vf.showPrevious();
                    }
                } //Else
            break;
            }
        }
        return false;
    }

最后一个 - 描述 OnTouchEvent 中提到的动画方法:

public static Animation inFromRightAnimation() 
{
    Animation inFromRight = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    inFromRight.setDuration(350);
    inFromRight.setInterpolator(new AccelerateInterpolator());
    return inFromRight;
}

public static Animation outToLeftAnimation() 
{
    Animation outtoLeft = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    outtoLeft.setDuration(350);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
}

为其余的进/出翻转方向编写类似的方法。

I have no see the question yet. But if you need to flip just between Layouts I can provide my own code how to use the flip functionality. First you should create a separate pages of your future Layouts, like page_1.xml in res.layout folder.

 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@color/GreenColor"
  >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="1"
        android:textSize="120dip">
    </TextView>
</LinearLayout>

After you have done, describe a main.xml file including external xml-pages:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ViewFlipper android:id="@+id/ViewFlipper01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    >
    <include android:id="@+id/libraryView1"  layout="@layout/page_1" />
    <include android:id="@+id/libraryView2"  layout="@layout/page_2" />
    <include android:id="@+id/libraryView3"  layout="@layout/page_3" />
</ViewFlipper>
</RelativeLayout>

Than don't forget in class file to declare vf=(ViewFlipper)findViewById(R.id.ViewFlipper01);
Next step you should to override OnTouchEvent to calculate new coordinates and showing the flip-animation:

   @Override
    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                oldTouchValueX = touchevent.getX();
                oldTouchValueY = touchevent.getY();
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                //if(this.searchOk==false) return false;
                float currentX = touchevent.getX();
                float currentY = touchevent.getY();

                float deltaX = getAbs(oldTouchValueX - currentX);
                float deltaY = getAbs(oldTouchValueY - currentY);

                if (deltaX >= deltaY)
                {
                    if (oldTouchValueX < currentX)
                    {
                        vf.setInAnimation(inFromLeftAnimation());
                        vf.setOutAnimation(outToRightAnimation());
                        vf.showNext();
                    }
                    if (oldTouchValueX > currentX)
                    {
                        vf.setInAnimation(inFromRightAnimation());
                        vf.setOutAnimation(outToLeftAnimation());
                        vf.showPrevious();
                    } 
                } //if deltaX
                else
                    {
                    if (oldTouchValueY < currentY)
                    {
                        vf.setInAnimation(inFromUpAnimation());
                        vf.setOutAnimation(outToDownAnimation());
                        vf.showNext();

                    }
                    if (oldTouchValueY > currentY)
                    {
                        vf.setInAnimation(inFromDownAnimation());
                        vf.setOutAnimation(outToUpAnimation());
                        vf.showPrevious();
                    }
                } //Else
            break;
            }
        }
        return false;
    }

And the last one - describe an animation methods which mentioned in OnTouchEvent:

public static Animation inFromRightAnimation() 
{
    Animation inFromRight = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    inFromRight.setDuration(350);
    inFromRight.setInterpolator(new AccelerateInterpolator());
    return inFromRight;
}

public static Animation outToLeftAnimation() 
{
    Animation outtoLeft = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    );
    outtoLeft.setDuration(350);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
}

Write similar for the rest in/out flip directions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文