在android中自定义图像的大小和尺寸

时间:2014-05-17 14:10:51

标签: java android imageview android-gallery picasso

我在做什么 ::

  • 我正在使用Picasso Library来显示galleryView的图像
  • 我正在运行时下载图像并显示它们

正在发生的事情 ::

  • 由于图像尺寸巨大且尺寸不同
  • 它们未正确显示在imageview中

问题 ::

  • 如何在填充到imageview之前缩小图像的大小 使用毕加索
  • 如何在所有图像中保持统一的图像尺寸

GridViewAdapter.java

public class GridViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public GridViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        //imageLoader = new ImageLoader(context);
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView flag;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.grid_view_image, parent, false);
        resultp = data.get(position);
        flag = (ImageView) itemView.findViewById(R.id.flag);
        Picasso.with(context).load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim()).into(flag);
        return itemView;
    }
}

FragBuffetContentsGallery.java

public class FragBuffetContentsGallery extends SherlockFragment{

    FragmentTransaction ft;
    static String sendBuffetId;
    static String sendRestName;
    static String sendDistance;
    static String sendBufTime;
    static String sendOnlinePrice;
    static String sendReservePrice;
    static String sendDescription;
    static String sendRating;
    static String sendBufType;

    TextView txtBufTime;
    TextView txtBufDistance;
    TextView txtOnlinePrice;
    TextView txtReservePrice;
    TextView txtBuffetDesc;
    TextView txtBufType;
    ImageView restRatingBar;

    GridView listview;
    GridViewAdapter adapter;
    ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
    String finalurl;
    JSONObject jsonobject;
    JSONArray jsonarray;

    private static String url8 = "http://54.218.73.244:7005/RestaurantDescImages/";


    //Constructor declaration on type newInstance
    //Constructor declaration on type newInstance
    public static FragBuffetContentsGallery newInstance(String _sendBuffetId,
            String _sendRestName, String _sendDistance, String _sendBufTime,
            String _sendOnlinePrice, String _sendReservePrice,
            String _sendDescription, String _sendRating, String _sendBufType) {
        FragBuffetContentsGallery fragment = new FragBuffetContentsGallery();

        sendBuffetId=_sendBuffetId;
        sendRestName=_sendRestName;
        sendDistance=_sendDistance;
        sendBufTime=_sendBufTime;
        sendOnlinePrice=_sendOnlinePrice;
        sendReservePrice=_sendReservePrice;
        sendDescription=_sendDescription;
        sendBufType=_sendBufType;
        sendRating=_sendRating;

        return fragment;
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.buffet_contents_gallery, container,false);
        setHasOptionsMenu(true);///For using actionbar menu from fragment

        txtBufTime=(TextView) rootView.findViewById(R.id.txtBufTime);
        txtBufDistance=(TextView) rootView.findViewById(R.id.txtBufDistance);
        txtOnlinePrice=(TextView) rootView.findViewById(R.id.txtOnlinePrice);
        txtReservePrice=(TextView) rootView.findViewById(R.id.txtReservePrice);
        txtBuffetDesc=(TextView) rootView.findViewById(R.id.txtBuffetDesc);
        txtBufType=(TextView) rootView.findViewById(R.id.txtBufType);
        restRatingBar=(ImageView) rootView.findViewById(R.id.restRatingBar);

        listview = (GridView) rootView.findViewById(R.id.listview);

        return rootView;
    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
///
        txtBufTime.setText(sendBufTime);
        txtBufDistance.setText(sendDistance);
        txtOnlinePrice.setText(sendOnlinePrice);
        txtReservePrice.setText(sendReservePrice);
        txtBuffetDesc.setText(sendDescription);
        txtBufType.setText(sendBufType);
        setRating(sendRating);

        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array

            Cursor cursor = null;
            DatabaseHandler mHelper = new DatabaseHandler(getActivity().getApplicationContext());
            SQLiteDatabase db = mHelper.getWritableDatabase();
            //String queryString="select * from buffets" ;
            HashMap<String, String> mapObj;

            String queryString="select item_image from buffets_gallery where buf_off_id = "+ sendBuffetId;
            Log.d("queryString", queryString.toString());

            try {
                cursor = db.rawQuery(queryString, null);
                Log.d("queryString", cursor.getCount()+"");

                if(cursor.moveToFirst()){
                    do{
                        mapObj = new HashMap<String, String>();
                        mapObj.put("item_image",cursor.getString(0));
                        arraylist.add(mapObj);//Add to arraylist onject(One row i each iteration)
                    }while(cursor.moveToNext());
                }
            }catch (Exception e) {
                e.printStackTrace();
                //ErrorDialog(e);
            } finally {
                cursor.close();
            }


            //arraylist=getDataFromSqlite();
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Pass the results into ListViewAdapter.java
            adapter = new GridViewAdapter(getActivity(), arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);

        }
    }



    private void setRating(String sendRating) {
        if(Float.valueOf(sendRating)==0.5){
            restRatingBar.setImageResource(R.drawable.star_05);
        }else if(Float.valueOf(sendRating)==1.0){
            restRatingBar.setImageResource(R.drawable.star_1);
        }else if(Float.valueOf(sendRating)==1.5){
            restRatingBar.setImageResource(R.drawable.star_15);
        }else if(Float.valueOf(sendRating)==2.0){
            restRatingBar.setImageResource(R.drawable.star_2);
        }else if(Float.valueOf(sendRating)==2.5){
            restRatingBar.setImageResource(R.drawable.star_25);
        }else if(Float.valueOf(sendRating)==3.0){
            restRatingBar.setImageResource(R.drawable.star_3);
        }else if(Float.valueOf(sendRating)==3.5){
            restRatingBar.setImageResource(R.drawable.star_35);
        }else if(Float.valueOf(sendRating)==4.0){
            restRatingBar.setImageResource(R.drawable.star_4);
        }else if(Float.valueOf(sendRating)==4.5){
            restRatingBar.setImageResource(R.drawable.star_45);
        }else if(Float.valueOf(sendRating)==5.0){
            restRatingBar.setImageResource(R.drawable.star_5);
        }else{
            restRatingBar.setImageResource(R.drawable.star_default);
        }
    }

    public void ErrorDialog(Exception err){
        //GPS-Dialog
        FragmentTransaction ft;
        ft = getFragmentManager().beginTransaction();
        ErrorDialog errorAlert;
        Fragment error ;

        error = ErrorDialog.newInstance(err.toString());
        errorAlert = new ErrorDialog();
        errorAlert.show(getActivity().getSupportFragmentManager(), "GpsAlert_Tag");

    }
}

grid_view_image.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/flag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#000000"
        android:padding="1dp" />

</RelativeLayout>

自助contents_gallery.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:background="#222222"
    android:orientation="vertical"
    android:src="@drawable/bg_listview_thinner_border" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date:"
                android:textColor="#ffffff"
                android:textSize="14sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txtDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="25 Dec 2012"
                android:textColor="#ffffff"
                android:textSize="12sp"
                android:textStyle="normal" />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="15dp"
                android:layout_height="20dp"
                android:src="@drawable/distance_flag" />

            <TextView
                android:id="@+id/txtBufDistance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10"
                android:textColor="#ffffff"
                android:textSize="12sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=".km"
                android:textColor="#ffffff"
                android:textSize="12sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Time:"
                android:textColor="#ffffff"
                android:textSize="14sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txtBufTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="8pm to 11pm"
                android:textColor="#ffffff"
                android:textSize="12sp"
                android:textStyle="normal" />

            <ImageView
                android:id="@+id/restRatingBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/star_5" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:gravity="center"
                android:orientation="horizontal" >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/txtOnlinePrice"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Txt" />

                    <Button
                        android:id="@+id/btnOnlinePrice"
                        android:layout_width="100dp"
                        android:layout_height="48dp"
                        android:background="@drawable/button_online_selector"
                        android:gravity="center_horizontal|center"
                        android:text="  Buy Now"
                        android:textColor="#000000"
                        android:textSize="12sp" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/txtReservePrice"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Txt" />

                    <Button
                        android:id="@+id/btnReservePrice"
                        android:layout_width="100dp"
                        android:layout_height="48dp"
                        android:layout_marginLeft="2dp"
                        android:background="@drawable/button_reserve_selector"
                        android:gravity="center_horizontal|center"
                        android:text="  Reserve"
                        android:textColor="#000000"
                        android:textSize="12sp" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txtBuffetDesc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Description"
                android:textColor="#00aeef"
                android:textSize="14sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txtBufType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="(Buffet)"
                android:textColor="#ffffff"
                android:textSize="12sp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="vertical" >

        <GridView
                android:id="@+id/listview"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:columnWidth="80dp"
                android:numColumns="3"
                android:scrollbars="horizontal" >
            </GridView>
    </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您可以在grid_view_image文件中设置大小。请注意,这仍将占用相同的内存量,因为它只调整显示的内容,但实际上并没有调整图像的大小。

<ImageView
    android:id="@+id/flag"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:scaleType="centerCrop"
    android:layout_alignParentRight="true"
    android:background="#000000"
    android:padding="1dp" />

答案 1 :(得分:0)

您可以将resizeDimen(int, int)与其中一个选项结合使用,例如centerCrop()centerInside()fit()

示例:

 Picasso.with(context)
      .load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim())
      .resizeDimen(R.dimen.wanted_size,R.dimen.wanted_size)
      .centerCrop().into(flag);