【问题标题】:Get dimens of an ImageView inside an adapter for a ListView获取 ListView 适配器内 ImageView 的尺寸
【发布时间】:2015-07-10 09:49:53
【问题描述】:

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

所以,在我的 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() 返回 0,因此图像不会在 ListView 中绘制。

如何获取 ImageView 的宽度和高度,以便正确缩放图像以适应 ImageView?

谢谢。

【问题讨论】:

    标签: android image android-listview android-adapter


    【解决方案1】:

    使用了这个公式:

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

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

    【讨论】:

      【解决方案2】:

      图像高度宽度为零,因为您在将位图应用于 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。 我希望它会工作......

      【讨论】:

        猜你喜欢
        • 2017-04-24
        • 2011-06-06
        • 2012-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多