如何不根据大小正确地将下载的图像显示到警报对话框中?
How to display downloaded image into alert dialog correctly not based on the size?
我想做的是展示一些我从 url 下载的图片。我设法下载并显示了图像,但我不知道如何根据尺寸正确显示,尺寸是指高度和宽度,而不是 kb。
我正在使用 volley 下载图片。在片段视图中单击后会显示警报对话框。
这是布局
<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));
}
}, 0, 0, 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
}
我想做的是展示一些我从 url 下载的图片。我设法下载并显示了图像,但我不知道如何根据尺寸正确显示,尺寸是指高度和宽度,而不是 kb。
我正在使用 volley 下载图片。在片段视图中单击后会显示警报对话框。
这是布局
<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));
}
}, 0, 0, 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
}