Home | History | Annotate | Download | only in expandingcells
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.expandingcells;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.graphics.Bitmap;
     22 import android.graphics.Bitmap.Config;
     23 import android.graphics.BitmapFactory;
     24 import android.graphics.Canvas;
     25 import android.graphics.Paint;
     26 import android.graphics.PorterDuff.Mode;
     27 import android.graphics.PorterDuffXfermode;
     28 import android.graphics.Rect;
     29 import android.view.LayoutInflater;
     30 import android.view.View;
     31 import android.view.ViewGroup;
     32 import android.widget.AbsListView;
     33 import android.widget.ArrayAdapter;
     34 import android.widget.ImageView;
     35 import android.widget.LinearLayout;
     36 import android.widget.ListView;
     37 import android.widget.TextView;
     38 
     39 import java.util.List;
     40 
     41 /**
     42  * This is a custom array adapter used to populate the listview whose items will
     43  * expand to display extra content in addition to the default display.
     44  */
     45 public class CustomArrayAdapter extends ArrayAdapter<ExpandableListItem> {
     46 
     47     private List<ExpandableListItem> mData;
     48     private int mLayoutViewResourceId;
     49 
     50     public CustomArrayAdapter(Context context, int layoutViewResourceId,
     51                               List<ExpandableListItem> data) {
     52         super(context, layoutViewResourceId, data);
     53         mData = data;
     54         mLayoutViewResourceId = layoutViewResourceId;
     55     }
     56 
     57     /**
     58      * Populates the item in the listview cell with the appropriate data. This method
     59      * sets the thumbnail image, the title and the extra text. This method also updates
     60      * the layout parameters of the item's view so that the image and title are centered
     61      * in the bounds of the collapsed view, and such that the extra text is not displayed
     62      * in the collapsed state of the cell.
     63      */
     64     @Override
     65     public View getView(int position, View convertView, ViewGroup parent) {
     66 
     67         final ExpandableListItem object = mData.get(position);
     68 
     69         if(convertView == null) {
     70             LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
     71             convertView = inflater.inflate(mLayoutViewResourceId, parent, false);
     72         }
     73 
     74         LinearLayout linearLayout = (LinearLayout)(convertView.findViewById(
     75                 R.id.item_linear_layout));
     76         LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams
     77                 (AbsListView.LayoutParams.MATCH_PARENT, object.getCollapsedHeight());
     78         linearLayout.setLayoutParams(linearLayoutParams);
     79 
     80         ImageView imgView = (ImageView)convertView.findViewById(R.id.image_view);
     81         TextView titleView = (TextView)convertView.findViewById(R.id.title_view);
     82         TextView textView = (TextView)convertView.findViewById(R.id.text_view);
     83 
     84         titleView.setText(object.getTitle());
     85         imgView.setImageBitmap(getCroppedBitmap(BitmapFactory.decodeResource(getContext()
     86                 .getResources(), object.getImgResource(), null)));
     87         textView.setText(object.getText());
     88 
     89         convertView.setLayoutParams(new ListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
     90                 AbsListView.LayoutParams.WRAP_CONTENT));
     91 
     92         ExpandingLayout expandingLayout = (ExpandingLayout)convertView.findViewById(R.id
     93                 .expanding_layout);
     94         expandingLayout.setExpandedHeight(object.getExpandedHeight());
     95         expandingLayout.setSizeChangedListener(object);
     96 
     97         if (!object.isExpanded()) {
     98             expandingLayout.setVisibility(View.GONE);
     99         } else {
    100             expandingLayout.setVisibility(View.VISIBLE);
    101         }
    102 
    103         return convertView;
    104     }
    105 
    106     /**
    107      * Crops a circle out of the thumbnail photo.
    108      */
    109     public Bitmap getCroppedBitmap(Bitmap bitmap) {
    110         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
    111                 Config.ARGB_8888);
    112 
    113         final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    114 
    115         Canvas canvas = new Canvas(output);
    116 
    117         final Paint paint = new Paint();
    118         paint.setAntiAlias(true);
    119 
    120         int halfWidth = bitmap.getWidth()/2;
    121         int halfHeight = bitmap.getHeight()/2;
    122 
    123         canvas.drawCircle(halfWidth, halfHeight, Math.max(halfWidth, halfHeight), paint);
    124 
    125         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    126 
    127         canvas.drawBitmap(bitmap, rect, rect, paint);
    128 
    129         return output;
    130     }
    131 
    132 
    133 }