import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import com.idroid.R;
import java.lang.ref.WeakReference;
/**
* @author
* @time
* @desc Dialog方式显示蒙版
*/
public class ShowDialogUtils implements View.OnClickListener {
private Dialog mDialog;
private WeakReference mWeakReference;
private ImageView mViewById;
public ShowDialogUtils(Activity act){
mWeakReference=new WeakReference(act);
initView(act);
}
private void initView(Activity act) {
mDialog = new Dialog(mWeakReference.get(), R.style.dialog);
mDialog.setContentView(R.layout.show_dialog);
mViewById = (ImageView) mDialog.findViewById(R.id.iv);
mViewById.setScaleType(ImageView.ScaleType.FIT_XY);
mViewById.setOnClickListener(this);
//给窗体设置大小
Window win = mDialog.getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
//设置宽高在as中没有作用,在Eclipse中要设置才能填充屏幕
// lp.width=ScreenUtils.getScreenWidth(act);
// lp.height = ScreenUtils.getScreenHeight(act);
win.setAttributes(lp);
}
public void show(int id){
if(null != mDialog && !mDialog.isShowing()){
mViewById.setImageResource(id);
mDialog.show();
}
}
public void dismiss(){
if(null != mDialog && mDialog.isShowing()){
mDialog.dismiss();
}
}
@Override
public void onClick(View v) {
dismiss();
}
}
<!-- 蒙版dialog样式 -->
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/transparent</item>
</style>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<color name="transparent">#00000000</color>
ShowDialogUtils showDialog = new ShowDialogUtils(this);
showDialog.show(R.drawable.homeguide);