android 弹窗DialogFragment使用
android DialogFragment使用。
·
public class AdvertisementFragment extends DialogFragment {
private ImageView imgMain;// 活动图背景图
private ImageView imgClose;// 关闭按钮
public GSRecommendList.Msg msg;// 数据源
private ClickEvent clickEvent;// 事件
public int devicewidth = 0;// 屏幕宽度
public int deviceheight = 0;// 屏幕高度
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.layout_dialog_fragment_advertisement, container);
initControl(view);// 初始化控件
initData();// 获取数据
setAnimation();// 设置动画
return view;
}
@Override
public void onStart() {
super.onStart();
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//宽度全屏的关键
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
getDialog().getWindow().setGravity(Gravity.CENTER);
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0.3f;
window.setAttributes(windowParams);
if (devicewidth > 0 ) {// 按照宽高比设置图片的大小
try {
// 设计图的宽高为260*322尺寸;
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int imgwidth = (int) ((devicewidth * 260) / 375);
int imgheight = (int) ((imgwidth * 322) / 260);
// 根据宽高比例设置活动背景图的宽高
layoutParams.width = imgwidth;
layoutParams.height = imgheight;
// 设置活动图片的左右边距使其居中显示;
layoutParams.leftMargin = (int) ((devicewidth - imgwidth) / 2);
layoutParams.rightMargin = (int) ((devicewidth - imgwidth) / 2);
imgMain.setLayoutParams(layoutParams);
} catch (Exception ex) {
ToastUtil.showCustomToast(ToastUtil.ToastType.FAIL, ex.getMessage());
}
}
}
/**
* todo 初始化控件
*/
private void initControl(View view) {
imgMain = (ImageView) view.findViewById(R.id.imgMain);
imgMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(null == msg)
return;
String adverstr = AppSPUtils.getString(getActivity(),"adverstrinfo","");
if(!TextUtils.isEmpty(adverstr)) {
GSRecommendList mBean = new Gson().fromJson(adverstr, GSRecommendList.class);
if(null != mBean && null != msg) {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
for (GSRecommendList.Msg msgs : mBean.getRetdata().getNoticelist()) {
if(msgs.getNoticeid().equals(msg.getNoticeid())) {
msgs.setReadtime(sdf.format(new Date()));
}
}
AppSPUtils.saveString(getActivity(),"adverstrinfo",new Gson().toJson(mBean));
}
}
if ("1".equals(msg.getGotofund())
&& !TextUtils.isEmpty(msg.getFundcode())) {
BusiHelper.gotoFundDetail(getActivity(), msg.getFundcode());
} else {
NoticeDetailActivity.startActivity(getActivity(), H5Helper.getNoticeDetailUrl(msg.getNoticeid()));
}
if(null != clickEvent) {
clickEvent.clickNotice();
}
dismissAllowingStateLoss();
}
});
imgClose = (ImageView) view.findViewById(R.id.imgClose);
imgClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(null == msg)
return;
String adverstr = AppSPUtils.getString(getActivity(),"adverstrinfo","");
if(!TextUtils.isEmpty(adverstr)) {
GSRecommendList mBean = new Gson().fromJson(adverstr, GSRecommendList.class);
if(null != mBean && null != msg) {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
for (GSRecommendList.Msg msgs : mBean.getRetdata().getNoticelist()) {
if(msgs.getNoticeid().equals(msg.getNoticeid())) {
msgs.setClosetime(sdf.format(new Date()));
}
}
AppSPUtils.saveString(getActivity(),"adverstrinfo",new Gson().toJson(mBean));
}
}
if(null != clickEvent) {
clickEvent.clickCancel();
}
dismissAllowingStateLoss();
}
});
}
/**
* 设置动画
*/
private void setAnimation() {
Objects.requireNonNull(getDialog().getWindow()).setWindowAnimations(R.style.animate_msg_dialog);
}
/**
* todo 获取数据
*/
private void initData() {
if(null != msg) {
loadingNetImg(imgMain,msg.getLittleimg());
}
}
public void loadingNetImg(final ImageView imgView, String url) {
com.nostra13.universalimageloader.core.ImageLoader.getInstance().loadImage(url, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
if (null != loadedImage) {
imgView.setImageBitmap(Bitmap.createScaledBitmap(loadedImage, loadedImage.getWidth(), loadedImage.getHeight(), false));
imgView.setTag("loadingsuccess");
}
}
@Override
public void onLoadingStarted(String imageUri, View view) {
super.onLoadingStarted(imageUri, view);
imgView.setImageBitmap(readBitMap(getActivity(), R.mipmap.loading_start));
imgView.setTag("loadingstart");
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
super.onLoadingFailed(imageUri, view, failReason);
imgView.setImageBitmap(readBitMap(getActivity(), R.mipmap.loading_failed));
imgView.setTag("loadingfailed");
}
});
}
public static Bitmap readBitMap(Context context, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_4444;
opt.inPurgeable = true;
opt.inInputShareable = true;
//获取资源图片
if (context != null) {
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}
return null;
}
public interface ClickEvent {
void clickCancel();
void clickNotice();
}
public ClickEvent getClickEvent() {
return clickEvent;
}
public void setClickEvent(ClickEvent clickEvent) {
this.clickEvent = clickEvent;
}
}
布局代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_ct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent">
<RelativeLayout
android:id="@+id/rl_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_adv_corner"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent">
<ImageView
android:id="@+id/imgMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
/>
</RelativeLayout>
<ImageView
android:id="@+id/imgClose"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginTop="20dp"
android:src="@mipmap/icon_ad_close"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/rl_main"
/>
</android.support.constraint.ConstraintLayout>
更多推荐
已为社区贡献8条内容
所有评论(0)