头像预览帮助类,使用方法 new AvatarScanHelper(context, url).show();
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.drawee.view.SimpleDraweeView;
import com.sunland.app.R;
/**
* 头像预览帮助类,使用方法 new AvatarScanHelper(context, url).show();
* 作者: FebMaple on 10/10/2016.
* 邮箱: febmaple219@gmail.com
* 版权: FebMaple
* ====================================================
*/
public class AvatarScanHelper extends Dialog {
private String avatarUrl;
private Context mContext;
private SimpleDraweeView mSimpleDraweeView;
public AvatarScanHelper(Context context, String avatarUrl) {
// 设置自定义样式
super(context, R.style.CustomDialog_fill);
this.mContext = context;
this.avatarUrl = avatarUrl;
initImageView(avatarUrl);
}
//直接使用imageview展示头像图片
private void initImageView(String avatarUrl) {
//重点在于用setContentView()加载自定义布局
setContentView(R.layout.dialog_avatar_scan);
mSimpleDraweeView = (SimpleDraweeView) findViewById(R.id.simple_image);
//fresco加载图片 mSimpleDraweeView.setImageURI(Uri.parse(avatarUrl));
//点击取消对话框
mSimpleDraweeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
//todo:长按图片保存图片到相册
setParams();
}
//设置对话框的宽高适应全屏
private void setParams() {
ViewGroup.LayoutParams lay = this.getWindow().getAttributes();
DisplayMetrics dm = new DisplayMetrics();
this.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
Rect rect = new Rect();
View view = getWindow().getDecorView();
view.getWindowVisibleDisplayFrame(rect);
lay.height = dm.heightPixels - rect.top;
lay.width = dm.widthPixels;
}
}
自定义的dialog布局:只有一个imageview用来展示图片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"//命名空间记得写
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/simple_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
fresco:actualImageScaleType="fitCenter"//图片显示方式必须写,不写不显示图片
fresco:failureImage="@drawable/button_avatar_default"
fresco:failureImageScaleType="fitCenter" />
</LinearLayout>
配置dialog的style
<style name="CustomDialog_fill" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowBackground">@color/black</item>
</style>
参考:fresco的使用http://www.kaelli.com/14.html