3

enter code here我想从openx服务器在android中做一个广告程序。但是我在有限的区域内获得了更大的广告。我希望广告适合所有 android 手机尺寸的所有 android 手机中的特定区域。

我试过这段代码:: 在 Xml 中,我给了::

<LinearLayout 
                     android:layout_alignParentBottom="true" 
                     android:background="@color/black" 
                     android:gravity="center_horizontal|center_vertical"
                     android:id="@+id/lLayout_ua_Ads" 
                     android:layout_height="38dip" 

                     android:layout_gravity="center_horizontal|center_vertical"
                     android:layout_width="300dip" 
                     android:orientation="vertical">

                     <xstream.android.loyalone.openxads.OpenxAdView
                          android:id="@+id/openAdView"
                          android:layout_width="wrap_content"
                           android:layout_height="wrap_content"  
                            />  
             </LinearLayout>   

在主程序中,我给了::

class AdManager{

        Context mContext;
        LinearLayout mLinearLayoutAds;
        Timer mReloadAdTimer;
         OpenxAdView openAd;

        public AdManager(Context context, LinearLayout linearLayoutAds){
            this.mContext = context;
            this.mLinearLayoutAds = linearLayoutAds;
            mReloadAdTimer = new Timer();

              openAd = (OpenxAdView)findViewById(R.id.openAdView);
              openAd.setDeliveryURL("openx.marzar.net/www/delivery");
              openAd.setZoneID(1);
                 }

        private void startShowing(){
            openAd.load();
            mReloadAdTimer.schedule(timerTaskAdUpdate, 10000, 10000);
        }

        TimerTask timerTaskAdUpdate = new TimerTask() {

            @Override
            public void run() {

                  openAd.load();
            }
        };


        protected void removeImage() {
            // TODO Auto-generated method stub
            this.mLinearLayoutAds.removeAllViews();
        }

        private void setAdWidjet(){         

        }

        public void onUpdating() {

            setAdWidjet();
        }

        protected void cleanUp() {

            try{
                mReloadAdTimer.cancel();
            }catch (Exception e) {

            }
        }
    }

为了获得 openx 广告,我给了 ::

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Random;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;


public class OpenxAdView extends ViewGroup {

    private static final String ATTRS_NS = "http://denivip.ru/schemas/android/openx/0.1";

    private static final String LOGTAG = "OpenXAd";

    private static final String PARAMETER_DELIVERY_URL = "delivery_url";
    private static final String PARAMETER_JS_TAG_URL = "js_tag_url";
    private static final String PARAMETER_ZONE_ID = "zone_id";
    private static final String PARAMETER_HAS_HTTPS = "has_https";
    private static final String PARAMETER_SOURCE = "source";

    private static final String HTML_DOCUMENT_TEMPLATE = "<html><head><style>* {padding: 0; margin: 0; background-color: transparent;}</style></head>\n"
        + "<body>%s</pre></body></html>";

    private static final String JS_TAG = "" 
        + "<script type='text/javascript' src='%1$s?zoneid=%2$d&amp;charset=UTF-8"
        + "&amp;cb=%4$d&amp;charset=UTF-8&amp;source=%3$s'></script>";

    private WebView webView;

    private String deliveryURL;

    private String jsTagURL = "ajs.php";
    //private String jsTagURL = "avw.php";

    private Integer zoneID;

    private boolean hasHTTPS = false;

    private String source;

    private Random prng = new Random();

    private Resources res;

    /**
     * Initialize widget.
     * 
     * @param context
     */
    public OpenxAdView(Context context) {
        super(context);
        this.res = context.getResources();
        this.webView = new WebView(context);
        initWebView();
    }

    /**
     * Initialize widget. If delivery_url and zone_id attributes were set in
     * layout file, ad will be loaded automatically.
     * 
     * @param context
     * @param attrs
     * @param defStyle
     */
    public OpenxAdView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.res = context.getResources();
        initAttributes(attrs);
        this.webView = new WebView(context, attrs, defStyle);
        initWebView();
    }

    /**
     * Initialize widget. If delivery_url and zone_id attributes were set in
     * layout file, ad will be loaded automatically.
     * 
     * @param context
     * @param attrs
     */
    public OpenxAdView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.res = context.getResources();
        initAttributes(attrs);
        this.webView = new WebView(context, attrs);
        initWebView();
    }

    private void initAttributes(AttributeSet attrs) {
        setDeliveryURL(attrs);
        setJsTagURL(attrs);
        setZoneID(attrs);
        setHasHTTPS(attrs);
        setSource(attrs);
    }

    private void initWebView() {
          DisplayMetrics d=this.res.getDisplayMetrics();

        int wndWidth= d.widthPixels;
        int wndHeight= d.heightPixels;

         if(wndHeight<=480)
        webView.setInitialScale(42); 
         else
         webView.setInitialScale(83);

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setPluginsEnabled(true);
        settings.setAllowFileAccess(false);
       // settings.setPluginState(WebSettings.PluginState.ON);

        webView.setBackgroundColor(0x00000000); // transparent
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setWebChromeClient(new WebChromeClient());

        addView(webView);
    }

    protected String getZoneTemplate(int zoneID) {
        String raw;
        try {
            String zoneTag = String.format(JS_TAG, 
                    (hasHTTPS ? "https://" : "http://") + deliveryURL + '/' + jsTagURL, 
                    zoneID,
                    source == null ? "" : URLEncoder.encode(source, "utf-8"),
                    prng.nextLong());
            raw = String.format(HTML_DOCUMENT_TEMPLATE, zoneTag);
            return raw;
        }
        catch (UnsupportedEncodingException e) {
        //  Log.wtf(LOGTAG, "UTF-8 not supported?!", e);
        }

        return null;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        webView.layout(left, top, right, bottom);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        load();
    }

    /**
     * Load ad from OpenX server using the parameters that were set previously.
     * This will not work if the following minimum required parameters were not
     * set: delivery_url and zone_id.
     */
    public void load() {
        //webView.clearView();
        if (zoneID != null) {
            load(zoneID);
        }
        else {
            Log.w(LOGTAG, "zoneID is empty");
        }
    }

    /**
     * Load ad from OpenX server using the parameters that were set previously
     * and the supplied zoneID. This will not work if the required parameter
     * delivery_url was not set.
     * 
     * @see #load()
     * @param zoneID ID of OpenX zone to load ads from.
     */
    public void load(int zoneID) {
        // check required parameters
        if (deliveryURL != null) {
        //  webView.clearView();
            webView.loadDataWithBaseURL(null, getZoneTemplate(zoneID), "text/html", "utf-8", null);
        }
        else {
            Log.w(LOGTAG, "deliveryURL is empty");
        }
    }

    public String getDeliveryURL() {
        return deliveryURL;
    }

    /**
     * The path to server and directory containing OpenX delivery scripts in the
     * form servername/path. This parameter is required. Example:
     * openx.example.com/delivery.
     * 
     * @param deliveryURL
     */
    public void setDeliveryURL(String deliveryURL) {
        this.deliveryURL = deliveryURL;
    }

    private void setDeliveryURL(AttributeSet attrs) {
        int delivery_url = attrs.getAttributeResourceValue(ATTRS_NS, PARAMETER_DELIVERY_URL, -1);
        if (delivery_url != -1) {
            this.deliveryURL = res.getString(delivery_url);
        }
        else {
            this.deliveryURL = attrs.getAttributeValue(ATTRS_NS, PARAMETER_DELIVERY_URL);
        }
    }

    public String getJsTagURL() {
        return jsTagURL;
    }

    /**
     * The name of OpenX script that serves ad code for simple JavaScript type
     * tag. Default: ajs.php. This parameter usually does not need to be
     * changed.
     * 
     * @param jsTagURL
     */
    public void setJsTagURL(String jsTagURL) {
        this.jsTagURL = jsTagURL;
    }

    private void setJsTagURL(AttributeSet attrs) {
        int js_tag_url_id = attrs.getAttributeResourceValue(ATTRS_NS, PARAMETER_JS_TAG_URL, -1);
        if (js_tag_url_id != -1) {
            this.jsTagURL = res.getString(js_tag_url_id);
        }
        else {
            String js_tag_url = attrs.getAttributeValue(ATTRS_NS, PARAMETER_JS_TAG_URL);
            if (js_tag_url != null) {
                this.jsTagURL = js_tag_url;
            }
        }
    }

    public Integer getZoneID() {
        return zoneID;
    }

    /**
     * The ID of OpenX zone from which ads should be selected to display inside
     * the widget. This parameter is required unless you use load(int) method.
     * 
     * @param zoneID
     */
    public void setZoneID(Integer zoneID) {
        this.zoneID = zoneID;
    }

    private void setZoneID(AttributeSet attrs) {
        int zone_id_rs = attrs.getAttributeResourceValue(ATTRS_NS, PARAMETER_ZONE_ID, -1);
        if (zone_id_rs != -1) {
            this.zoneID = new Integer(res.getInteger(zone_id_rs));
        }
        else {
            int zone_id = attrs.getAttributeIntValue(ATTRS_NS, PARAMETER_ZONE_ID, -1);
            if (zone_id != -1) {
                this.zoneID = new Integer(zone_id);
            }
        }
    }

    public boolean hasHTTPS() {
        return hasHTTPS;
    }

    /**
     * Set this to true if ads should be served over HTTPS protocol. Default:
     * false.
     * 
     * @param hasHTTPS
     */
    public void setHasHTTPS(boolean hasHTTPS) {
        this.hasHTTPS = hasHTTPS;
    }

    private void setHasHTTPS(AttributeSet attrs) {
        int has_https = attrs.getAttributeResourceValue(ATTRS_NS, PARAMETER_HAS_HTTPS, -1);
        if (has_https != -1) {
            this.hasHTTPS = res.getBoolean(has_https);
        }
        else {
            this.hasHTTPS = attrs.getAttributeBooleanValue(ATTRS_NS, PARAMETER_HAS_HTTPS, false);
        }
    }

    public String getSource() {
        return source;
    }

    /**
     * This parameter can be used to target ads by its value. It is optional.
     * 
     * @param source
     */
    public void setSource(String source) {
        this.source = source;
    }

    private void setSource(AttributeSet attrs) {
        int source_id = attrs.getAttributeResourceValue(ATTRS_NS, PARAMETER_SOURCE, -1);
        if (source_id != -1) {
            this.source = res.getString(source_id);
        }
        else {
            this.source = attrs.getAttributeValue(ATTRS_NS, PARAMETER_SOURCE);
        }
    }
}

请给出你对这个问题的解决方案。提前致谢。

4

0 回答 0