如何解决如何根据大小正确地将下载的图像显示在警报对话框中?
我要尝试的是显示一些我从URL下载的图像。我设法下载并显示了图像,但是我无法确定如何根据大小正确显示图像,所谓大小,是指高度,宽度不是kb。
我正在使用凌空下载图像。在片段视图中单击后,将显示警报对话框。
这是布局
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imgProduct"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
这就是我显示警报对话框的方式
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View imageDialog = layoutInflater.inflate(R.layout.popup_product_image,null);
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(imageDialog);
dialog.setCanceledOnTouchOutside(true);
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
ImageView imgImage = imageDialog.findViewById(R.id.imgProduct);
String url = product.urlImage.replaceAll("\\s+","");
ImageRequest imgRequest = new ImageRequest(url,new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
imgImage.setImageBitmap(response);
imgImage.startAnimation(AnimationUtils.loadAnimation(context,android.R.anim.fade_in));
}
},ImageView.ScaleType.CENTER_CROP,Bitmap.Config.ARGB_8888,new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
imgImage.setImageResource(RES_ERROR);
Toast.makeText(context,VolleyManager.getInstance(context)
.checkError(error),Toast.LENGTH_LONG).show();
}
});
imgRequest.setTag(CANCELABLE_REQUEST_TAG);
VolleyManager.getInstance(context).getRequestQ().add(imgRequest);
// Set your image
dialog.show();
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
任何帮助或建议都很好,谢谢
解决方法
使用上面的注释,请使用Piccaso。尝试此操作,然后从“ ImageView imgImage = imageDialog.findViewById(R.id.imgProduct);”中修改
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap,Picasso.LoadedFrom from) {
imgImage.setImageBitmap(bitmap);
doFoo();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
imgImage.setImageDrawable(errorDrawable);
Toast.makeText(context,"download failed",Toast.LENGTH_SHORT).show();
doBar();
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
imgImage.setImageDrawable(placeHolderDrawable);
}
};
//this will help us to avoid the Target being gc'd
imgImage.setTag(target);
Picasso.with(context)
.load(url)
.placeholder(RES_PLACEHOLDER)
.error(RES_ERROR)
.resize(400,400)
.into(target);
// Set your image
dialog.show();
dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
然后使用伪函数
private void doFoo() {
//dummy
}
private void doBar() {
//dummy
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。