Android中的Viewflipper并非一直在翻转

时间:2011-09-13 21:46:24

标签: android viewflipper ads

我有两个观看次数(广告),当一个广告网络没有广告显示时,视图应该翻转为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>

1 个答案:

答案 0 :(得分:0)

我还没有看到这个问题。但是如果你需要在Layouts之间翻转,我可以提供我自己的代码如何使用翻转功能。首先,您应该创建未来布局的单独页面,例如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页面的 main.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;
}

为其余的进/出翻转方向写相似的内容。