一个广告控件

package com.my.example.advertisementdemo;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher.ViewFactory;

public class ADView extends FrameLayout implements ViewFactory {
    private LinearLayout mLinFocusIndicatorContainer;

    private ArrayList<Drawable> imgList = new ArrayList<Drawable>();

    private Gallery galleryAd = null;

    private int index = 0;

    private ImageView mImgViewColse;//广告关闭按钮
    
    private HideReceiver receiver = new HideReceiver();

    /** 广告栏位是显示还是隐藏*/
    public static boolean mAdUIIsVisible = true;

    /** 存储上一个选择项的Index*/
    private int preSelImgIndex = 0;

    public ADView(Context context) {
        super(context);
        init();
    }

    public ADView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ADView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private ImageSwitcher imageSwitcher;

    /** 初始化广告控件信息*/
    private void initAdUI() {
        InitImgList();
        mLinFocusIndicatorContainer = (LinearLayout) findViewById(R.id.focus_indicator_container_lin);
        galleryAd = (Gallery) findViewById(R.id.banner_gallery);
        galleryAd.setAdapter(new AdImageAdapter(getContext(), imgList));
        galleryAd.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int selIndex, long id) {
                ImageView preSelImg = (ImageView) mLinFocusIndicatorContainer.findViewById(preSelImgIndex);
                preSelImg.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_focus));
                ImageView curSelImg = (ImageView) mLinFocusIndicatorContainer.findViewById(selIndex);
                curSelImg.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_focus_select));
                preSelImgIndex = selIndex;
                imageSwitcher.setImageDrawable(imgList.get(selIndex));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
        imageSwitcher.setFactory(this);
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.slide_in_left));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.slide_out_right));
        mImgViewColse = (ImageView) findViewById(R.id.close_imgview);
        mImgViewColse.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ADView.this.setVisibility(View.GONE);
                mAdUIIsVisible = false;
                Intent intent = new Intent();
                intent.putExtra("isVisible", false);
                intent.setAction("com.my.action.AD_GONE");  
                ADView.this.getContext().sendBroadcast(intent); 
            }
        });
        InitFocusIndicatorContainer();
    }

    /** 添加任务每隔7s播放一张广告图片*/
    private TimerTask task = new TimerTask() {
        @Override
        public void run() {
            index = galleryAd.getSelectedItemPosition();
            index++;
            if (index >= imgList.size())
                index = 0;
            ADView.this.post(new Runnable() {
                
                @Override
                public void run() {
                    galleryAd.setSelection(index);
                }
            });
        }
    };

    /** 填充广告图片下面所选中圆点图片信息*/
    private void InitFocusIndicatorContainer() {
        for (int i = 0; i < imgList.size(); i++) {
            ImageView localImageView = new ImageView(getContext());
            localImageView.setId(i);
            ImageView.ScaleType localScaleType = ImageView.ScaleType.FIT_XY;
            localImageView.setScaleType(localScaleType);
            LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(24, 24);
            localImageView.setLayoutParams(localLayoutParams);
            localImageView.setPadding(5, 5, 5, 5);
            localImageView.setImageResource(R.drawable.ic_focus);
            this.mLinFocusIndicatorContainer.addView(localImageView);
        }
    }

    /** 加载广告图片*/
    private void InitImgList() {
        imgList.add(this.getResources().getDrawable(R.drawable.ad1));
        imgList.add(this.getResources().getDrawable(R.drawable.ad2));
    }

    @Override
    public View makeView() {
        ImageView imageView = new ImageView(getContext());
        imageView.setBackgroundColor(0xFF000000);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        return imageView;
    }

    private void init() {
        final LayoutInflater mLayoutInflater = LayoutInflater.from(getContext());
        View view = mLayoutInflater.inflate(R.layout.ad_layout, null);
        addView(view);
        initAdUI();
        if(mAdUIIsVisible)
            ADView.this.setVisibility(View.VISIBLE);
        else
            ADView.this.setVisibility(View.GONE);
    }
    
    public class HideReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            boolean flag = intent.getBooleanExtra("isVisible", false);
            if(flag)
                ADView.this.setVisibility(View.VISIBLE);
            else
                ADView.this.setVisibility(View.GONE);
        }
    }
    
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        final Timer timer = new Timer();
        timer.schedule(task, 3000, 3000);
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.my.action.AD_GONE");
        getContext().registerReceiver(receiver, filter);
    }
    
    @Override
    protected void onDetachedFromWindow() {
        getContext().unregisterReceiver(receiver);
        super.onDetachedFromWindow();
    }
}


package com.my.example.advertisementdemo;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView.ScaleType;

public class AdImageAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Drawable> imgList ;
	public AdImageAdapter(Context context ,ArrayList<Drawable> imgList) {		
		this.context=context;
		this.imgList=imgList;
	}
	private void InitImgList() {
		// 加载图片数据(本demo仅获取本地资源,实际应用中,可异步加载网络数据)
		imgList.add(context.getResources().getDrawable(R.drawable.ad1));
		imgList.add(context.getResources().getDrawable(R.drawable.ad2));
	}
	@Override
	public int getCount() {
		return imgList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView = new ImageView(context);
		imageView.setImageDrawable(imgList.get(position));
		imageView.setAdjustViewBounds(true);
		imageView.setScaleType(ScaleType.FIT_XY);
		imageView.setLayoutParams(new Gallery.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		return imageView;
	}

}

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ad_frame"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <Gallery
        android:id="@+id/banner_gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:fadingEdgeLength="0.0dp"
        android:spacing="0dp"
        android:visibility="invisible" />

    <ImageSwitcher
        android:id="@+id/imageswitcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/bottomnavpoint_lin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="0dp" >

        <LinearLayout
            android:id="@+id/focus_indicator_container_lin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal|bottom"
            android:orientation="horizontal" />
    </LinearLayout>
    <!-- 右侧删除按钮 -->

    <LinearLayout
        android:id="@+id/rightDelete_lin"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="right|center"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingRight="5dp" >

        <LinearLayout
            android:id="@+id/delete_lin"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/close_imgview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/close" />
        </LinearLayout>
    </LinearLayout>

</FrameLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值