在 ListView 的适配器中获取 ImageView 的维度

Get dimens of an ImageView inside an adapter for a ListView

我正在尝试调整通过相机接收到的图像的大小,以适应将成为 ListView 中项目一部​​分的 ImageView。

所以,在我的 onActivityResult 中,我得到了图像...

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == Activity.RESULT_OK) {

        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/imageneslpi");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-" + n + ".jpg";
        file = new File(myDir, fname);
        try {
            out = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        byte[] byteArray = stream.toByteArray();
        lat=Double.longBitsToDouble(prefs.getLong("latitud", 0));
        lon=Double.longBitsToDouble(prefs.getLong("longitud", 0));

        try {
            ExifInterface exif=new ExifInterface(file.getCanonicalPath());
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, convert(lat));
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, convert(lon));
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, latitudeRef(lat));
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, longitudeRef(lon));
            exif.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }

        arrayFotos.add(new BeanFotos("", imageBitmap, lat, lon));
        adapter.notifyDataSetChanged();


    }
}

我正在通过构造函数将照片添加到数组中,这是 bean:

public class BeanFotos {
private Bitmap foto;
private String expediente;
private Double longitud;
private Double latitud;

public BeanFotos(String expediente, Bitmap foto, Double latitud, Double longitud) {
    this.expediente = expediente;
    this.foto = foto;
    this.latitud = latitud;
    this.longitud = longitud;
}

public String getExpediente() {
    return expediente;
}

public void setExpediente(String expediente) {
    this.expediente = expediente;
}

public Bitmap getFoto() {
    return foto;
}

public void setFoto(Bitmap foto) {
    this.foto = foto;
}

public Double getLatitud() {
    return latitud;
}

public void setLatitud(Double latitud) {
    this.latitud = latitud;
}

public Double getLongitud() {
    return longitud;
}

public void setLongitud(Double longitud) {
    this.longitud = longitud;
}
}

这是适配器:

public class FotoAdapter extends BaseAdapter {
private ArrayList<BeanFotos> fotos;
private LayoutInflater inflater=null;

public FotoAdapter(Context c, ArrayList<BeanFotos> fotos){
    this.fotos=fotos;
    inflater=LayoutInflater.from(c);

}
@Override
public int getCount() {
    return fotos.size();
}

@Override
public Object getItem(int position) {
    return fotos.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        int bmWidth=fotos.get(position).getFoto().getWidth();
        int bmHeight=fotos.get(position).getFoto().getHeight();
        int ivWidth;
        int ivHeigth;
        int new_width;
        int new_height;
        convertView=inflater.inflate(R.layout.foto_layout, null);
        TextView lat=(TextView)convertView.findViewById(R.id.textlat);
        TextView lon=(TextView)convertView.findViewById(R.id.textlon);
        ImageView foto=(ImageView)convertView.findViewById(R.id.foto);
        ivWidth=foto.getWidth();
        ivHeigth=foto.getHeight();
        new_width=ivWidth;
        new_height = (int) Math.floor((double) bmHeight *( (double) new_width / (double) bmWidth));
        Bitmap newbitMap = Bitmap.createScaledBitmap(fotos.get(position).getFoto(), new_width, new_height, true);
        lat.setText(fotos.get(position).getLatitud().toString());
        lon.setText(fotos.get(position).getLongitud().toString());
        //foto.setImageBitmap(fotos.get(position).getFoto().getDrawingCache());
        foto.setImageBitmap(newbitMap);
    }
    return convertView;
}
}

适配器中使用的布局:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/lon"
    android:id="@+id/textView5"

    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:id="@+id/foto"
    android:layout_alignParentTop="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/lat"
    android:id="@+id/textView6"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/textlon"
    android:layout_alignBottom="@+id/foto"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/textlat"
    android:layout_alignTop="@+id/textlon"
    android:layout_alignParentEnd="true" />

这样,当我调整图像大小以适应 ImageView 时,getWidth() 和 getHeight() returns 0,因此图像不会在 ListView 中绘制。

我怎样才能获得 ImageView 的宽度和高度,以便我可以适当地缩放图像以适合 ImageView?

谢谢。

使用了这个公式:

private int dpToPx(int dp)
{
    float density = c.getResources().getDisplayMetrics().density;
    return Math.round((float)dp * density);
}

因为我知道 ImageView 在 dos 中的大小,所以现在可以正常显示图像了。

您得到的图像高度宽度为零,因为您在将位图应用到 ImageView 之前进行提取

 int bmWidth=fotos.get(position).getFoto().getWidth();
        int bmHeight=fotos.get(position).getFoto().getHeight();
        int ivWidth;
        int ivHeigth;
        int new_width;
        int new_height;
        convertView=inflater.inflate(R.layout.foto_layout, null);
        TextView lat=(TextView)convertView.findViewById(R.id.textlat);
        TextView lon=(TextView)convertView.findViewById(R.id.textlon);
        ImageView foto=(ImageView)convertView.findViewById(R.id.foto);
        ivWidth=foto.getWidth();
        ivHeigth=foto.getHeight();
        new_width=ivWidth;
        new_height = (int) Math.floor((double) bmHeight *( (double) new_width / (double) bmWidth));
        Bitmap newbitMap = Bitmap.createScaledBitmap(fotos.get(position).getFoto(), new_width, new_height, true);
        lat.setText(fotos.get(position).getLatitud().toString());
        lon.setText(fotos.get(position).getLongitud().toString());
        //foto.setImageBitmap(fotos.get(position).getFoto().getDrawingCache());
        foto.setImageBitmap(newbitMap);

在这段代码中,您首先需要获取位图的高度宽度 首先将位图应用于图像视图,然后获取其高度宽度,然后调整其大小。

因为当你获取它的高度宽度时,Imageview 是空的所以你会得到 0。 我希望它会起作用...