在ListView

时间:2015-07-10 09:49:54

标签: android image android-listview android-adapter

我试图调整通过相机接收的图像的大小,以适合将成为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()ann getHeight()返回0,因此图像不会在ListView中绘制。

我如何获得ImageView的宽度和高度,以便我可以正确缩放图像以适合ImageView?

谢谢。

2 个答案:

答案 0 :(得分:0)

使用此公式:

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

由于我知道dos中ImageView的大小,现在显示的图像已经确定了。

答案 1 :(得分:0)

您正在获取图像高度宽度ZERO,因为您在将位图应用于ImageView之前进行提取

&#13;
&#13;
 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);
&#13;
&#13;
&#13;

在此代码中,首先需要获取位图高度,然后首先将应用位图到imageview,然后获取其高度宽度,然后调整其大小。

因为当您获取其高度宽度时,Imageview为空,因此您将获得0。 我希望它能奏效......