最近做項目要做廣告位的展示,所以就簡單的做了這個小項目。
代碼很簡單:
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 [email protected]
*
*/
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() {
// 從右拖到左,即看後一張
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btnNext();
}
});
pre = (Button) findViewById(R.id.pre);
pre.setOnClickListener(new OnClickListener() {
//從左拖到右,看前一張
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btnPre();
}
});
}
private class ViewFactoryImpl implements ViewFactory {
@Override
public 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 {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (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 {
@Override
public void run() {
// TODO Auto-generated method stub
Message 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 {
@Override
public void run() {
// TODO Auto-generated method stub
Message 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秒執行一次.
源代碼地址:源文件下載地址