转载请注明出处王亟亟的大牛之路
平时我们会遇到各种各样的小广告推送形式今天上一个实现比较典型的自定义View,不过很值得称道的是作者对封装和代码解耦的理解。
效果图:
一个延迟弹出自定义对话框并实现其逻辑的一个自定义控件。
项目结构:
代码相当轻量级,用起来也很方便。
MainActivity:
public class MainActivity extends AppCompatActivity {
private Context mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
BannerTime.init(this)
.shownTime(5000)
.hiddenTime(3000)
.imageBanner("http://campbellriver.whatsondigest.com/sites/default/files/event_photographs/Try%20it%20.png", false)
.withOKButton(R.color.def, "Default", android.R.color.white)
.withKOButton(R.color._ok_bg, "Custom", android.R.color.white)
.background(R.color.fav)
.imageBannerPlaceholder(0)
.show(new BannerTime.BannerTimeListener() {
@Override
public void onShow() {
}
@Override
public void onDismiss() {
}
@Override
public void onClickOk() {
DefActivity.launch(mContext);
}
@Override
public void onClickKO() {
CustomActivity.launch(mContext);
}
@Override
public void onClickImage() {
Toast.makeText(mContext, "onClickImage", Toast.LENGTH_SHORT).show();
}
}); }
@Override
protected void onPause() {
super.onPause();
BannerTime.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity_def in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
流程分析:
public static BannerTime init(Context context) {
if (sInstance == null) {
sInstance = new BannerTime(context);
}
mContext = context;
init();
return sInstance;
}
↑首先先初始化一个BannerTime的对象如果为空就new 一个实例,应用程序整个生命周期过程中只有这么一个实例。
private static void init() {
dialog = null;
contentView = null;
requestCreator = null;
mGravity = BannerTime.BOTTOM;
mView = LayoutInflater.from(mContext)
.inflate(R.layout.layout_banner, null, false);
mPlaceHolder = R.drawable.loading;
okVisibility = View.GONE;
koVisibility = View.GONE;
SHOW_TIME_BANNER = 10000;
HIDE_TIME_BANNER = 2 * 60000;
timerShow = new Timer();
timerHide = new Timer();
}
↑之后再初始化一些内部变量并对计时器进行声明等操作。
public static BannerTime shownTime(int time) {
SHOW_TIME_BANNER = time;
return sInstance;
}
↑设置显示时间
public static BannerTime hiddenTime(int time) {
HIDE_TIME_BANNER = time;
return sInstance;
}
↑设置消失时间
public static BannerTime imageBanner(String path, boolean crop) {
requestCreator = Picasso.with(mContext).load(path);
if (crop)
requestCreator.fit().centerCrop();
else
requestCreator.fit().centerInside();
return sInstance;
}
↑异步加载图片(第三方库)
public static BannerTime withOKButton(int bgResource, String text, int textColor) {
Button ok = (Button) mView.findViewById(R.id.btnOk);
okVisibility = View.VISIBLE;
ok.setText(text);
ok.setTextColor(mContext.getResources().getColor(textColor));
ok.setBackgroundResource(bgResource);
return sInstance;
}
↑对按钮进行设置
public static BannerTime background(int resourceId) {
mView.findViewById(R.id.parent).setBackgroundResource(resourceId);
return sInstance;
}
设置整个Dialog的背景
public static BannerTime imageBannerPlaceholder(int resId) {
mPlaceHolder = resId;
return sInstance;
}
↑设置图片标识
public static void show(BannerTimeListener listener) {
if (sInstance == null)
throw new RuntimeException("It´ necessary call '.init()' method");
mListener = listener;
ImageView img = (ImageView) mView.findViewById(R.id.imageAd);
if (requestCreator != null) {
if (mPlaceHolder != 0) {
requestCreator.placeholder(mPlaceHolder)
.into(img);
} else {
requestCreator.into(img);
}
} else {
Picasso.with(mContext)
.load(mPlaceHolder)
.placeholder(mPlaceHolder)
.fit().centerCrop()
.into(img);
}
createDialog();
}
↑根据传来的图片标识,进行不同的处理
private static void createDialog() {
if (mGravity == BannerTime.BOTTOM) {
dialog = new Dialog(mContext, R.style.DialogSlideAnimBottom);
} else if (mGravity == BannerTime.TOP) {
dialog = new Dialog(mContext, R.style.DialogSlideAnimTop);
} else if (mGravity == BannerTime.LEFT) {
dialog = new Dialog(mContext, R.style.DialogSlideAnimLeft);
} else if (mGravity == BannerTime.RIGHT) {
dialog = new Dialog(mContext, R.style.DialogSlideAnimRight);
} else {
dialog = new Dialog(mContext, R.style.DialogSlideAnimBottom);
}
dialog.getWindow().setGravity(mGravity);
dialog.setCanceledOnTouchOutside(false);
if (contentView != null) {
if (contentView.getParent() == null)
dialog.setContentView(contentView);
} else {
if (mView.getParent() == null)
dialog.setContentView(mView);
}
ImageView img = (ImageView) mView.findViewById(R.id.imageAd);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null)
mListener.onClickImage();
}
});
mView.findViewById(R.id.btnOk).setVisibility(okVisibility);
mView.findViewById(R.id.btnOk).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (dialog.isShowing()) {
dialog.dismiss();
if (mListener != null)
mListener.onClickOk();
}
}
});
mView.findViewById(R.id.btnKO).setVisibility(koVisibility);
mView.findViewById(R.id.btnKO).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (dialog.isShowing()) {
dialog.dismiss();
if (mListener != null)
mListener.onClickKO();
}
}
});
scheduleHideBanner(dialog);
}
↑具体画View的方法
private static void scheduleHideBanner(final Dialog dialog) {
dismissDialog(dialog);
timerTaskHide = new TimerTask() {
@Override
public void run() {
scheduleShowBanner(dialog);
}
};
timerHide.schedule(timerTaskHide, HIDE_TIME_BANNER);
}
↑异步隐藏Dialog
public interface BannerTimeListener {
void onShow();
void onDismiss();
void onClickOk();
void onClickKO();
void onClickImage();
}
↑各个状态触发的事件
源码地址:点我下载!!!!!
时间: 2024-10-31 20:00:22