android 廣告位的Timer定時重新整理和按鈕的浮動顯示(3秒消失)

來源:互聯網
上載者:User

最近做項目要做廣告位的展示,所以就簡單的做了這個小項目。

代碼很簡單:

1、實現功能代碼:

package com.my.zx;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.LinearLayout.LayoutParams;import android.widget.ViewSwitcher.ViewFactory;/** *  * @author bravo_lover@sina.com * */public class zx extends Activity {    /** Called when the activity is first created. */private Context mActivity;private Timer adTimer;private AdTimerTask adTask;private Timer hideTimer ;private HideTimerTask hideTask; private static final Integer[] imagelist = { R.drawable.new_home_normal, R.drawable.new_rank_normal, R.drawable.new_user_normal, R.drawable.new_search_normal, R.drawable.new_manage_normal};private ImageSwitcher m_Switcher;private Button next, pre ;private static int index = 0;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mActivity = this;        initAd();           hideTimer = new Timer(true);        adTimer = new Timer(true);//廣告的        hideBtn();    }    private void initAd() {m_Switcher = (ImageSwitcher) findViewById(R.id.imageswitcher);m_Switcher.setFactory(new ViewFactoryImpl());m_Switcher.setOnTouchListener(new mSwitcherOnTouchListener());m_Switcher.setImageResource(imagelist[index]);// 因為要旋轉所以我們需要儲存視圖的緩衝資訊  m_Switcher.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE); next = (Button) findViewById(R.id.next);//下一張next.setOnClickListener(new OnClickListener() {// 從右拖到左,即看後一張@Overridepublic void onClick(View v) {// TODO Auto-generated method stubbtnNext();}});pre = (Button) findViewById(R.id.pre);pre.setOnClickListener(new OnClickListener() {//從左拖到右,看前一張@Overridepublic void onClick(View v) {// TODO Auto-generated method stubbtnPre();}});}private class ViewFactoryImpl implements ViewFactory {@Overridepublic View makeView() {// TODO Auto-generated method stub// 執行個體化圖片顯示ImageView img = new ImageView(mActivity); // 設定背景顏色img.setBackgroundColor(0xFFFFFFFF); // 置中顯示img.setScaleType(ImageView.ScaleType.CENTER); // 自適應圖片大小//image.setMinimumHeight(200); //image.setMinimumWidth(200); img.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // 定義組件return img;}}class mSwitcherOnTouchListener implements View.OnTouchListener {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_DOWN:pre.setVisibility(View.VISIBLE);next.setVisibility(View.VISIBLE);if (adTimer != null) {if (adTask != null) {adTask.cancel();}}break;case MotionEvent.ACTION_UP:hideBtn();break;default :break;}return true;}}private void hideBtn() {if (hideTimer != null){if (hideTask != null){hideTask.cancel();  //將原任務從隊列中移除}}hideTask = new HideTimerTask();hideTimer.schedule(hideTask, 3000); }private void cancelAdTask() {adTask = new AdTimerTask();adTimer.schedule(adTask, 3000, 3000);}class HideTimerTask extends TimerTask {@Overridepublic void run() {// TODO Auto-generated method stubMessage message = new Message();message.what = 1;handler.sendMessage(message);}}Handler handler = new Handler(){public void handleMessage(Message msg) {switch (msg.what) {case 1:pre.setVisibility(View.GONE);next.setVisibility(View.GONE);cancelAdTask();break;case 2://廣告頁重新整理btnNext();default:break;}};}; class AdTimerTask extends TimerTask {@Overridepublic void run() {// TODO Auto-generated method stubMessage message = new Message();message.what = 2;handler.sendMessage(message);}}private void btnNext() {index++;if (index >= imagelist.length) {index = 0;}m_Switcher.setImageResource(imagelist[index]);}private void btnPre() {index--;if (index < 0) {index = imagelist.length - 1;}m_Switcher.setImageResource(imagelist[index]);}}

2、布局main.xml

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="72.0sp" ><ImageSwitcher android:id="@+id/imageswitcher" android:background="@drawable/ad_idex_imageswitcher_bg" android:layout_width="fill_parent" android:layout_height="72.0sp" android:layout_gravity="center" android:layout_marginLeft="5.0dip" android:layout_marginRight="5.0dip" android:layout_weight="1.0" /><RelativeLayout android:layout_width="fill_parent" android:layout_height="72.0sp" ><Button android:id="@+id/pre" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="pre" android:layout_alignParentLeft="true" /><Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="next" android:layout_alignParentRight="true" /></RelativeLayout></FrameLayout>

如上就是全部代碼:這裡要寫幾點注意的地方:

1、FrameLayout布局中,誰在底部,就先布局誰。

2、定時器Timer,我遇到了 java.lang.IllegalStateException: TimerTask is scheduled already。這個錯誤,這是由於每個TimerTask只能被schedule一次,第二次會拋出異常。

3、hideTimer.schedule(hideTask, 3000);   和  adTimer.schedule(adTask, 3000, 3000);的區別

第一種是:沒有設定延遲,也就是立即執行,以後每3000秒執行一次,相當於schedule(adTask, 0, 3000)

第二種是:延遲3000秒之後執行,以後每3000秒執行一次.


原始碼地址:源檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.