package com.example.dzy; import android.app.Dialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import java.io.InputStream; public class Fragment_1 extends Fragment { private ImageView imageView_1,imageView_2; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_shouye, container, false); imageView_1=(ImageView)view.findViewById(R.id.image_show1); imageView_1.setImageBitmap(bitmap); return view; } Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.show1); bigImageLoader(bitmap); //方法里直接实例化一个imageView不用xml文件,传入bitmap设置图片 private void bigImageLoader(Bitmap bitmap){ final Dialog dialog = new Dialog(getActivity()); ImageView image = new ImageView(getContext()); image.setImageBitmap(bitmap); dialog.setContentView(image); //将dialog周围的白块设置为透明 dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); //显示 dialog.show(); //点击图片取消 image.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ dialog.cancel(); } }); } }这段代码应如何修改
时间: 2024-04-29 21:19:05 浏览: 165
DZY.exe
这段代码存在一个错误,`bigImageLoader(bitmap)`方法应该在`onCreateView`方法内部调用,而不是在类声明处调用。修改后的代码如下:
```java
package com.example.dzy;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.io.InputStream;
public class Fragment_1 extends Fragment {
private ImageView imageView_1,imageView_2;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_shouye, container, false);
imageView_1=(ImageView)view.findViewById(R.id.image_show1);
imageView_1.setImageBitmap(bitmap);
imageView_1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
bigImageLoader(bitmap);
}
});
return view;
}
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.show1);
//方法里直接实例化一个imageView不用xml文件,传入bitmap设置图片
private void bigImageLoader(Bitmap bitmap){
final Dialog dialog = new Dialog(getActivity());
ImageView image = new ImageView(getContext());
image.setImageBitmap(bitmap);
dialog.setContentView(image);
//将dialog周围的白块设置为透明
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
//显示
dialog.show();
//点击图片取消
image.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
dialog.cancel();
}
});
}
}
```
这里将`bigImageLoader`方法改为在`imageView_1`的点击事件中调用,这样点击图片时才会调用显示大图的方法。同时,也将`bigImageLoader`方法移到了类的内部,以方便调用。
阅读全文